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

Posted at

AtCoder ABC 109 A&B&C

AtCoder - 109

A問題

  • Cについて、$1-3$まで全部調べる
	private void solveA() {
		Scanner scanner = null;
		int numA = 0;
		int numB = 0;

		try {
			scanner = new Scanner(System.in);
			numA = scanner.nextInt();
			numB = scanner.nextInt();
			for (int i = 1; i < 4; i++) {
				if (numA * numB * i % 2 != 0) {
					System.out.println("Yes");
					return;
				}
			}

			System.out.println("No");

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

B問題

  • 文字$W_i$が既に発言されている文字ならNG
  • 文字$W_i$の先頭一文字が、文字$W_{i-1}$の最後一文字と違うならNG
	private void solveB() {
		Scanner scanner = null;
		int numN = 0;

		try {
			scanner = new Scanner(System.in);
			numN = scanner.nextInt();
			String[] wk = new String[numN];

			Set<String> wkSet = new HashSet<String>();
			for (int i = 0; i < wk.length; i++) {
				String wkI = scanner.next();
				wk[i] = wkI;
				if (wkSet.contains(wk[i])) {
					System.out.println("No");
					return;
				}
				wkSet.add(wk[i]);
				if (i != 0) {
					String wkIm1 = wk[i - 1];
					if (wkIm1.charAt(wkIm1.length() - 1) != wkI.charAt(0)) {
						System.out.println("No");
						return;
					}

				}
			}

			System.out.println("Yes");

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

C問題

  • 都市間を移動する

    • 初期座標は
    • 移動可能な距離は、$y+D ; || ; y-D$
    • $\pm ; D$ をして全ての都市を訪れる
    • 初期座標$X$がスタート位置
  • {$初期座標,; x_1,; x_2,; x_3,; x_4.......,; x_5$} という配列を作成し、ソートする

  • 全ての座標を訪れるには全ての座標間の差分の最大公約数がわかればよい

    • 最大公約数を求めた結果、$D=1$という可能性もありますが。。。
	private void solveC() {
		Scanner scanner = null;
		int numN = 0;
		int numX = 0;

		try {
			scanner = new Scanner(System.in);
			numN = scanner.nextInt();
			numX = scanner.nextInt();
			long[] wk = new long[numN + 1];

			wk[0] = numX;
			for (int i = 1; i < wk.length; i++) {
				wk[i] = scanner.nextInt();
			}

			Arrays.sort(wk);
			long[] wkSa = new long[numN];
			for (int i = 0; i < wk.length; i++) {
				if (i != 0) {
					wkSa[i - 1] = wk[i] - wk[i - 1];
				}
			}

			System.out.println(maxKouyakusu(wkSa));

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

	private long maxKouyakusu(long[] wk) {

		long res = 0;
		for (int i = 0; i < wk.length; i++) {
			if (i == 0) {
				res = wk[i];
				continue;
			}
			res = maxKouyakusu(res, wk[i]);
		}
		return res;

	}

	private long maxKouyakusu(long i1, long i2) {

		long w1 = Math.min(i1, i2);
		long w2 = Math.max(i1, i2);

		while (w2 > 0) {
			long temp = w1 % w2;
			w1 = w2;
			w2 = temp;
		}

		return w1;

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