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 - 028 - A&B&C

Last updated at Posted at 2019-05-02

AtCoder ABC 028 A&B&C

AtCoder - 028

微妙にA-Cが簡単な回が続いている。。。

2019/05/03 追記
 B問題のコード修正

A問題

	private void solveA() {
		int numN = nextInt();
		if (numN <= 59) {
			out.println("Bad");
		} else if (numN <= 89) {
			out.println("Good");
		} else if (numN <= 99) {
			out.println("Great");
		} else {
			out.println("Perfect");
		}
	}

B問題

2019/05/03コード修正

  • Collectors調べないと
	private void solveB() {
		String wk = next();

		Map<String, Long> tmp = Arrays.stream(wk.split(""))
				.collect(Collectors.groupingBy(s -> s, Collectors.counting()));

		String[] ref = { "A", "B", "C", "D", "E", "F" };

		String res = Arrays.stream(ref).map(i -> tmp.getOrDefault(i, 0L)
				.toString()).collect(Collectors.joining(" "));

		/*
		 * この問題は、空白が最後についているとWAになってしまうのでtrim
		 */
		out.println(res.trim());

	}

C問題:総当たりした解法

  • 全部足すのを試す
    • 重複を省くためにsetを使う
      • sortしておいてほしいのでtreeset
    • 最後から三番目の要素を取り出して出力
	private void solveC2() {
		int[] wk = IntStream.range(0, 5).map(i -> nextInt()).toArray();

		Set<Integer> set = new TreeSet<Integer>();
		for (int i = 0; i < wk.length; i++) {
			for (int j = i + 1; j < wk.length; j++) {
				for (int k = j + 1; k < wk.length; k++) {
					set.add(wk[i] + wk[j] + wk[k]);
				}
			}
		}

		List<Integer> list = new ArrayList<Integer>(set);

		out.println(list.get(list.size() - 3));
	}

C問題:editorialの解法

  • editorialのP.9から解説

  • $a<b<c<d<e$であるならば

    • 最大の値は
      • $C+D+E$
    • 次に大きいのは
      • $B+D+E$
    • その次に大きいのは次のどちらか
      • $A+D+E$
      • $B+C+E$
	private void solveC() {
		int[] wk = IntStream.range(0, 5).map(i -> nextInt()).toArray();

		int res = Integer.max(wk[4] + wk[3]+ wk[0], wk[4] + wk[2]+ wk[1]);
		out.println(res);
	}
0
0
2

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?