0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ABC - 081- A&B&C

Posted at

AtCoder ABC 081 B&C

AtCoder - 081

A問題

  • 1のカウントを実施する
	private void solveA() {
		Scanner scanner = null;
		char[] wk;

		try {
			scanner = new Scanner(System.in);
			wk = scanner.nextLine().toCharArray();

			int res = 0;
			for (int i = 0; i < wk.length; i++) {
				if (wk[i] == '1') {
					res++;
				}
			}

			System.out.println(res);

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

B問題

  • 奇数になったらそこでstop
  • 0になったらそこでstop
	private void solveB() {
		Scanner scanner = null;
		int n = 0;

		try {
			scanner = new Scanner(System.in);
			n = scanner.nextInt();

			int[] wk = new int[n];

			for (int i = 0; i < wk.length; i++) {
				wk[i] = scanner.nextInt();
			}

			System.out.println(divide(wk));

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

	private int divide(int[] wk) {
		boolean divide = true;
		int res = 0;
		for (int i = 0; i < wk.length && divide; i++) {
			if (wk[i] % 2 == 0 && wk[i] > 0) {
				wk[i] = wk[i] / 2;
			} else {
				divide = false;
			}
		}
		if (divide) {
			res = divide(wk) + 1;
		}
		return res;
	}

C問題

  • $N$個のボールに書かれている整数の種類と個数をMapに入れる
    • ${1=2,2=1,3=5,4=5,5=5, ・・・ N=5}$
  • 書き換えを最小にしたいので、個数が少ない整数を選択して書き換える
    • $(整数全体 - 書き換えたい整数)=N$ にすれば良い
    • そのために、個数でソートして書き換え量が少ない整数を選択し、個数を合計する
	private void solveC() {
		Scanner scanner = null;
		int n = 0;
		int k = 0;

		try {
			scanner = new Scanner(System.in);
			n = scanner.nextInt();
			k = scanner.nextInt();

			Map<Integer, Integer> wk = new HashMap<Integer, Integer>();
			for (int i = 0; i < n; i++) {
				int wkKey = scanner.nextInt();
				if (!wk.containsKey(wkKey)) {
					wk.put(wkKey, 1);
				} else {
					wk.put(wkKey, wk.get(wkKey) + 1);
				}
			}

			List<Integer> wkList = getSortedKeyArray(wk);

			int res = 0;
			for (int i = 0; i < wkList.size() - k; i++) {
				res += wkList.get(i);
			}

			System.out.println(res);

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

	private List<Integer> getSortedKeyArray(Map<Integer, Integer> wk) {
		List<Integer> wkList = new ArrayList<Integer>();

		for (Iterator<Integer> itr = wk.values().iterator(); itr.hasNext();) {
			wkList.add(itr.next());
		}

		Collections.sort(wkList);
		return wkList;
	}

例によってC問題をStreamで書き換える練習

そしてStreamに直した後のコードをテストケースに通すとforよりも明らかに速度遅いんだよな。
&
if-else/for/while 見慣れすぎてて可読性が上がった気がしない。。。。

	private void solveC2() {
		final Scanner scanner = new Scanner(System.in);
		int n = 0;
		int k = 0;

		try {
			n = scanner.nextInt();
			k = scanner.nextInt();

			Map<Integer, Integer> wk = IntStream.range(0, n).collect(() -> new HashMap<Integer, Integer>(), (t, i) -> {
				int wkKey = scanner.nextInt();
				t.merge(wkKey, 1, (oldV, newV) -> oldV + newV);
			},
					(t, u) -> {
						t.putAll(u);
					});

			//			Map<Integer, Integer> wk = new HashMap<Integer, Integer>();
			//			for (int i = 0; i < n; i++) {
			//				int wkKey = scanner.nextInt();
			//				if (!wk.containsKey(wkKey)) {
			//					wk.put(wkKey, 1);
			//				} else {
			//					wk.put(wkKey, wk.get(wkKey) + 1);
			//				}
			//			}

			List<Integer> wkList = getSortedKeyArray(wk);

			int res = wkList.stream().limit(wkList.size() - k < 0 ? 0 : wkList.size() - k).reduce(0, (sum, i) -> sum + i);
			//			for (int i = 0; i < wkList.size() - k; i++) {
			//				res += wkList.get(i);
			//			}

			System.out.println(res);

		} finally {
			if (scanner != null) {
				scanner.close();
			}
		}
	}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?