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

Last updated at Posted at 2019-03-31

AtCoder ABC 073 A&B&C

AtCoder - 073

2019/05/27
 問題名修正
 B問題のコードの書き方を修正 int[][]の生成部分

A - September 9

  • $mod10$の結果が9かどうか?
	private void solveA() {
		int numN = nextInt();

		while (numN != 0) {
			int wk = numN % 10;
			if (wk == 9) {
				out.println("Yes");
				return;
			}
			numN /= 10;
		}

		out.println("No");
	}

B - Theater

  • 同じ席に複数人が座ることはないため、団体の占有している席の総数を調べる
	/*
	 * Streamですべてを組み立てたみた
	 */
	private void solveB() {
		int numN = nextInt();

		int[][] wk = Stream.generate(() -> new int[] { nextInt(), nextInt() }).limit(numN).toArray(int[][]::new);
		//		int[][] wk = IntStream.range(0, numN).collect(() -> new int[numN][2], (t, i) -> {
		//			t[i][0] = nextInt();
		//			t[i][1] = nextInt();
		//		}, (t, u) -> {
		//			Stream.concat(Arrays.stream(t), Arrays.stream(u)).toArray(int[][]::new);
		//		});

		int res = Arrays.stream(wk).mapToInt(i -> (i[1] - i[0]) + 1).sum();

		out.println(res);
	}

C - Write and Erase

setを使った解法

  • すでに取得していた数値ならremoveする
  • まだsetに格納していない数値ならaddする
	private void solveC2() {
		int numN = nextInt();
		long[] wk = LongStream.range(0, numN).map(i -> nextLong()).toArray();

		Set<Long> wkSet = new HashSet<Long>();
		for (long l : wk) {
			if (wkSet.contains(l)) {
				wkSet.remove(l);
			} else {
				wkSet.add(l);
			}
		}

		out.println(wkSet.size());
	}

最初に入力を配列にしないで直接処理にまわしてみた。こっちのほうが100ms以上速い。

	private void solveC() {
		int numN = nextInt();

		Set<Long> wkSet = new HashSet<Long>();
		for (int i = 0; i < numN; i++) {
			long key = nextLong();
			if (wkSet.contains(key)) {
				wkSet.remove(key);
			} else {
				wkSet.add(key);
			}
		}

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