|
|
back to boardWA 5 I don't know, why answer is wrong. Suggest me a solution. On the other side, all checking that I had done was success. My java code : import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class task1005 { private static Scanner scanner; private static List<Long> mainHeap; public static void main(String[] args) { getData(); balancedTwoHeap(); } private static void getData() { scanner = new Scanner(System.in); mainHeap = new ArrayList<>(); int stones = scanner.nextInt(); for (int i = 0; i < stones; i++) { mainHeap.add(scanner.nextLong()); } } private static void balancedTwoHeap() { Long heap1 = 0L; Long heap2 = 0L; while (!mainHeap.isEmpty()) { Long max = mainHeap.stream().max(Long::compareTo).get(); if (heap1 > heap2) { heap2 += max; } else { heap1 += max; } mainHeap.remove(max); } System.out.println(Math.abs(heap1 - heap2)); } }
Re: WA 5 i meet the same situation.assume the input weight is 5 4 3 3 3. sort the input weight and arrange in order is not the suitable solution. Re: WA 5 5 5 5 4 3 3 должно вернуть 0 5+5 = 0 4+3+3 = 0 Edited by author 20.07.2016 00:14 Edited by author 20.07.2016 00:15 Re: WA 5 But... the ans is 2, isn't it? I have the same solution(arrange in order) and it return 2 from this test(5 4 3 3 3)... So, this test don't show that this way is wrong. Edited by author 11.12.2019 18:17 Edited by author 11.12.2019 18:18 |
|
|