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

Posted at

AtCoder ABC 040 A&B&C

AtCoder - 040

A問題

	private void solveA() {
		int numN = nextInt();
		int numX = nextInt();

		out.println(numN - numX > numX - 1 ? numX - 1 : numN - numX);
	}

B問題

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

		int res = 99999999;
		for (int i = 1; i <= numN; i++) {
			for (int j = 1; j <= numN; j++) {
				//タイルが何枚余るか
				int amari = numN - (i * j);
				//タイルを使い切っているならこれ以上の探索は無意味
				if (amari < 0) {
					break;
				}
				res = Math.min(Math.abs(i - j) + amari, res);
			}
		}

		out.println(res);
	}

C問題

  • DPの一番初歩の形
    • 貰うDPと配るDPの両方を試した
private void solveC() {
		int numN = nextInt();
		int[] wk = IntStream.range(0, numN).map(i -> nextInt()).toArray();

		int[] dp = new int[numN];
		Arrays.fill(dp, Integer.MAX_VALUE - 1);

		/*
		 * 貰うDP
		 */
		//		dp[0] = 0;
		//		dp[1] = Math.abs(wk[1] - wk[0]);
		//		for (int i = 2; i < dp.length; i++) {
		//			for (int j = 1; j <= 2; j++) {
		//				dp[i] = Math.min(Math.abs(wk[i] - wk[i - j]) + dp[i - j], dp[i]);
		//			}
		//		}

		/*
		 * 配るDP
		 */
		dp[0] = 0;
		for (int i = 0; i < dp.length; i++) {
			for (int j = 1; j <= 2; j++) {
				if (i + j >= dp.length) {
					continue;
				}
				dp[i + j] = Math.min(Math.abs(wk[i] - wk[i + j]) + dp[i], dp[i + j]);
			}
		}

		out.println(dp[numN - 1]);
	}
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?