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

Posted at

AtCoder ABC 107 A&B&C

AtCoder - 107

A問題

		Scanner scanner = null;

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

			System.out.println((scanner.nextInt() - scanner.nextInt()) + 1);

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

B問題

  • 白い部分を省く=黒い部分があったら省かない
    • 縦横で黒い部分をマークして、交差する箇所のみを出力すれば白い部分を省ける
	private void solveB2() {

		try (Scanner scanner = new Scanner(System.in)) {
			int numH = scanner.nextInt();
			int numW = scanner.nextInt();

			char[][] wk = new char[numH][numW];

			int[] arrayH = new int[numH];
			int[] arrayW = new int[numW];
			/*
			 * 黒い部分をマーク
			 */
			for (int i = 0; i < wk.length; i++) {
				wk[i] = scanner.next().toCharArray();
				int cntBlak = 0;
				//今取り込んだ箇所に黒い部分があれば列を記録
				for (int j = 0; j < arrayW.length; j++) {
					if (wk[i][j] == '#') {
						arrayW[j] = 1;
						cntBlak++;
					}
				}
				//今の行に1列でも黒い部分があればこの行は黒を持つ
				if (cntBlak > 0) {
					arrayH[i] = 1;
				}
			}
			/*
			 * マークした黒い部分を判定
			 * 縦と横のマークが重なる部分を出力
			 */
			for (int i = 0; i < numH; i++) {
				if (arrayH[i] == 0) {
					continue;
				}
				StringBuilder builder = new StringBuilder();
				for (int j = 0; j < numW; j++) {
					if (arrayW[j] == 0) {
						continue;
					}
					builder.append(wk[i][j]);
				}
				if (builder.length() != 0) {
					System.out.println(builder.toString());
				}
			}

		}
	}

C問題

  • 最初のスタートが$位置0$から一番近いところを選択するのがいいように思ったけど、、、それよりは全探索で十分

並びの配列は3つ
そのため、0 -> n の探索と、n -> 0 の探索の両方を行う必要がある

  • パターン1:0(スタート)が数列の真ん中にある
    $-188, ; -187, ; -186, ; -185, ; -98, ; -90, ; 100, ; 110, ; 120$

  • パターン2:0(スタート)は一番最後より大きい
    $-190, ; -189, ; -188, ; -187, ; -186, ; -185, ; -98, ; -90, ; -50, ; -40$

  • パターン3:0(スタート)は一番最初より小さい
    $90, ; 100, ; 110, ; 120, ; 130, ; 140, ; 150, ; 160, ; 170, ; 180, ; 190, ; 200$

/**
	 * Cは、とりあえず|r|に移動して|r|+|r+numK|-|r|
	 * を右と左から実施する。
	 * |  1---N  | 0 |  1---N  |
	 * というように、0より大きい場合も0より小さい場合もあるので、
	 */
	private void solveC() {

		try (Scanner scanner = new Scanner(System.in)) {

			int numN = scanner.nextInt();
			int numK = scanner.nextInt();

			if (numK == 1) {
				System.out.println(0);
				return;
			}
			int[] wk = IntStream.range(0, numN).map(i -> scanner.nextInt()).toArray();

			int plusNumK = numK - 1;
			int res = Integer.MAX_VALUE;

			/*
			 * 0 -> N まで、K本連続してつけるのに必要な時間の最小を求める
			 */
			for (int i = 0; i < wk.length - plusNumK; i++) {
				res = Math.min(res, Math.abs(wk[i]) + Math.abs(wk[i] - wk[i + plusNumK]));
			}

			/*
			 * 上記同様に、N -> 0までK本連続してつけるのに必要な時間の最小を求める
			 */
			for (int i = wk.length - 1; i >= plusNumK; i--) {
				res = Math.min(res, Math.abs(wk[i]) + Math.abs(wk[i] - wk[i - plusNumK]));
			}
			System.out.println(res);

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