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

Posted at

AtCoder ABC 085 B&C

AtCoder - 085

A問題

  • 2017を2018にreplaceする
	private void solveA() {
		Scanner scanner = null;
		String line = "";

		try {
			scanner = new Scanner(System.in);
			line = scanner.next();

			System.out.println(line.replaceAll("2017", "2018"));

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

B問題

コメントはfor文仕様

  • もちを小さい順にソートする
  • 同じ直径のもちは一つとしてカウントする
	private void solveB() {

		final Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int[] wk = IntStream.range(0, n).map(i -> scanner.nextInt()).toArray();
		//			int[] wk = new int[n];
		//
		//			for (int i = 0; i < n; i++) {
		//				wk[i] = scanner.nextInt();
		//			}

		Arrays.sort(wk);

		int res = IntStream.range(0, n).reduce(0, (sum, i) -> {
			if (i == 0) {
				sum++;
			} else if (wk[i] != wk[i - 1]) {
				sum++;
			}
			return sum;
		});
		//			int res = 0;
		//			for (int i = 0; i < wk.length; i++) {
		//				if (i == 0) {
		//					res++;
		//				} else if (wk[i] != wk[i - 1]) {
		//					res++;
		//				}
		//
		//			}

		System.out.println(res);
		if (scanner != null) {
			scanner.close();
		}
	}

C問題

計算量の削減の話

  • $i=10000,y=5000,k=1000$
    • Yが $\leqq2×10^7$ なので$i*10000$の最大値が決まる(continueではなくてbreak。。。)
    • $j$の最大値(5000円を使える枚数)は$\leqq n-i$枚まで
    • 今までの条件より、kの値は$k=n-i-j$と決まる
      • 上記以外のKは利用可能枚数の制限から外れるため計算不要。これでloopを削減できる
	private void solveC2() {
		Scanner scanner = null;
		int n = 0;
		int y = 0;

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

			for (int i = n; i >= 0; i--) {
				if (i * 10000 > y) {
					continue;
				}
				for (int j = 0; j <= n - i; j++) {
					int k = n - i - j;
					if (i * 10000 + j * 5000 + k * 1000 == y) {
						System.out.println(i + " " + j + " " + k);
						return;
					}
				}
			}

			System.out.println(-1 + " " + -1 + " " + -1);

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

C問題:遅い実装

  • for文を3回も重ねると間に合いません。
  • ただ、問題文どおりの素直な実装ではある。TLEだけど。
	private void solveC() {
		Scanner scanner = null;
		int n = 0;
		int y = 0;

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

			for (int i = n; i >= 0; i--) {
				if (i * 10000 > y) {
					continue;
				}
				for (int j = 0; j <= n - i; j++) {
					for (int k = 0; k <= n - i - j; k++) {
						if (n == i + j + k && i * 10000 + j * 5000 + k * 1000 == y) {
							System.out.println(i + " " + j + " " + k);
							return;
						}
					}
				}
			}

			System.out.println(-1 + " " + -1 + " " + -1);

		} 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?