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

Last updated at Posted at 2019-04-13

AtCoder ABC 117 A&B&C

AtCoder - 117

解説放送

A問題

  • BigDecimalで四捨五入
	private static void solveA() {

		try (Scanner scanner = new Scanner(System.in)) {
			String numT = "";
			String numX = "";

			numT = scanner.next();
			numX = scanner.next();

			BigDecimal result = new BigDecimal(numT).divide(
					new BigDecimal(numX), 6, RoundingMode.HALF_UP);
			System.out.println(result.floatValue());

		}
	}

B問題

  • 定理 : 一番長い辺が他の N−1 辺の長さの合計よりも真に短い場合に限り、条件を満たす N 角形が描ける。

  • 定理を満たすかどうかは

    • 入力値をソートする
    • ソートした入力値を一番長い辺(ソート後に一番最後となった値)を除いて合算
    • 合算値とソート後の最後の値を比較
	private static void solveB() {

		try (Scanner scanner = new Scanner(System.in)) {
			int numN = 0;
			numN = scanner.nextInt();
			int[] wkArray = new int[numN];
			for (int i = 0; i < numN; i++) {
				wkArray[i] = scanner.nextInt();
			}

			Arrays.sort(wkArray);

			int max = 0;
			int others = 0;
			for (int i = numN - 1; i >= 0; i--) {
				if (i == numN - 1) {
					max = wkArray[i];
				} else {
					others += wkArray[i];
				}

			}
			System.out.println(max < others ? "Yes" : "No");

		}
	}

C問題

  • 入力値

    • $3, 7$
    • $-10, -3, 0, 9, -100, 2, 17$
  • 駒を置くべき場所

    • 座標間の移動距離が大きいところ
      • 移動距離が大きい=移動せずに済ませたい距離
    • 座標間の移動距離を算出しソートすれば、行かずに駒を置くだけで済ました居場所が判別できる
      • コード中にインラインでコメントを記述
  • サンプルについて

    • 3を置いた箇所が-10なら9まで移動、9に置いたら-10まで移動
座標 -100 -10 -3 0 2 9 17
座標間の差分 90 7 3 2 7 8
駒位置(3はどちらでも可) ↑1 ↑3 ↑3 ↑2
	private static void solveC2() {

		try (Scanner scanner = new Scanner(System.in)) {
			int numN = 0;
			int numM = 0;
			numN = scanner.nextInt();
			numM = scanner.nextInt();
			if (numM == 1 && numN >= numM) {
				System.out.println(0);
				return;
			}
			/*
			 * 各地点を取り込む
			 */
			int[] distArray = new int[numM];
			for (int i = 0; i < numM; i++) {
				distArray[i] = scanner.nextInt();
			}
			/*
			 * 地点を座標順に並び替える
			 */
			Arrays.sort(distArray);
			/*
			 * 座標間の距離を取得
			 */
			int[] sabunArray = new int[distArray.length - 1];
			for (int i = 0; i < sabunArray.length; i++) {
				sabunArray[i] = distArray[i + 1] - distArray[i];
			}

			/*
			 * 座標間の距離を並び替える
			 */
			Arrays.sort(sabunArray);

			int result = 0;
			/*
			 * 移動量が大きいところには駒を置いてしまうので
			 * 小さいところから合算すると移動しないといけない量を算出できる
			 */
			for (int i = 0; i < numM - numN; i++) {
				result += sabunArray[i];
			}

			System.out.println(result);

		}
	}

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?