Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.HashMap;
- import java.util.Map;
- import static java.lang.Integer.parseInt;
- /**
- * Created by bugkiller on 09/06/19.
- */
- class Baloni {
- static int a[] = new int[1000000];
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int n = parseInt(br.readLine());
- String s[] = br.readLine().split("\\s");
- for (int i = 0; i < n; i++) {
- a[i] = Integer.parseInt(s[i]);
- }
- System.out.println(minimumArrows(a, n));
- }
- private static int minimumArrows(int[] a, int n) {
- Map<Integer, Integer> frequency = new HashMap<>();
- int arrows = 0;
- for (int i = 0; i < n; i++) {
- if (frequency.containsKey(a[i])) {
- frequency.compute(a[i], (key, value) -> value - 1);
- frequency.remove(a[i], 0);
- frequency.merge(a[i] - 1, 1, (ov, nv) -> ov + 1);
- } else {
- frequency.merge(a[i] - 1, 1, (ov, nv) -> ov + 1);
- arrows++;
- }
- }
- return arrows;
- }
- }
Add Comment
Please, Sign In to add comment