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

Posted at

AtCoder ABC 072 A&B&C

AtCoder - 072

A問題

  • Xグラムの砂が1秒当たり1g減っていく
  • T秒後に何g残っているのか?
  • ただし、T秒経過前に砂が0gになる場合がある
	private void solveA() {
		long numX = nextLong();
		long numT = nextLong();

		out.println(numX - numT > 0 ? numX - numT : 0);
	}

B問題

  • 先頭から奇数番目の文字だけ抜き出して文字列を生成
  • 先頭は1文字目
    • なので、配列としては偶数番目が文字列としての奇数番目に該当
	private void solveB() {
		String wk = next();

		StringBuilder builder = IntStream.range(0, wk.length()).filter(i -> (i % 2 == 0))
				.collect(() -> new StringBuilder(), (t, i) -> {
					t.append(wk.charAt(i));
				}, (t, u) -> t.append(u));

		out.println(builder.toString());
	}

C問題

  • 数列 {$1,2,3 ・・・ a_i$} 全ての数字に対して一度だけ下記操作を行う

    • $+1$する
    • $-1$する
    • 何もしない
  • 結果として得られる数列の内、一番数が多いものを出力する

  • 全探索しても何とかなりますがほぼTLE確定なので、

操作しなくてはいけない数列を表にすると下記になる。

1 2 3 4 5 6 7 INDEX
- - - - - - - -
3 1 4 1 5 9 2 この数列は、以下のように操作ができる
- - - - - - - -
4 2 5 2 6 10 3 全て+1
3 1 4 1 5 9 2 何もしない
2 0 3 0 4 8 1 全て-1

「全て+1、なにもしない、全て-1」の内、一番出現率が高い数字を出力すれば問題の解答となる。

  • 「全て+1、なにもしない、全て-1」をした結果の出現率をMapに変換する
  • Mapの中でvalueが最大のものが一番出現率が高い
	private void solveC() {
		int numN = nextInt();

		long[] wk = LongStream.range(0, numN).map(i -> nextLong()).toArray();

		Map<Long, Long> wkCount = Arrays.stream(wk).collect(() -> new HashMap<Long, Long>(),
				(t, i) -> {
					long constVal = 1;
					t.merge(i, constVal, (oldV, newV) -> oldV + newV);
					t.merge(i + 1, constVal, (oldV, newV) -> oldV + newV);
					t.merge(i - 1, constVal, (oldV, newV) -> oldV + newV);
				},
				(t, u) -> t.putAll(u));

		Entry<Long, Long> entryWk = null;
		for (Entry<Long, Long> entry : wkCount.entrySet()) {
			if (entryWk == null) {
				entryWk = entry;
			} else {
				if (entryWk.getValue() < entry.getValue()) {
					entryWk = entry;
				}
			}
		}

		out.println(entryWk.getValue());
	}
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?