C++の練習を兼ねて, AtCoder Regular Contest 041 の 問題A (コインの反転) ~ 問題B (アメーバ) を解いてみた.
■感想.
1. 問題B は, 個人的には, 非常に面白い問題に感じた.
2. 時間を見つけて, 引き続き, 過去問を振り返っていきたいと思う.
本家のサイトARC 041 解説をご覧下さい.
■C++版プログラム(問題A/AC版).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <bits/stdc++.h> using namespace std; int main(){ // 1. 入力情報. int x, y, k; scanf("%d %d %d", &x, &y, &k); // 2. 表向きのコインの最大枚数は? int ans = 0; if(k <= y) ans = x + k; else ans = x + y - (k - y); // 3. 出力. printf("%d\n", ans); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
[入力例] 3 2 1 [出力例] 4 ※AtCoderのテストケースより [入力例] 3 2 4 [出力例] 3 ※AtCoderのテストケースより [入力例] 3 2 5 [出力例] 2 ※AtCoderのテストケースより [入力例] 202020 303030 393939 [出力例] 414141 |
■C++版プログラム(問題B/AC版).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <bits/stdc++.h> using namespace std; #define repex(i, a, b, c) for(int i = a; i < b; i += c) #define repx(i, a, b) repex(i, a, b, 1) #define rep(i, n) repx(i, 0, n) #define repr(i, a, b) for(int i = a; i >= b; i--) int memo[555][555], board[555][555]; int main(){ // 1. 入力情報. int N, M; scanf("%d %d", &N, &M); rep(i, N){ char c[555]; scanf("%s", c); rep(j, M) memo[i][j] = c[j] - '0'; } // 2. アメーバの位置を計算. repx(i, 1, N - 1){ repx(j, 1, M - 1){ // 上下左右を見る. if(memo[i - 1][j] && memo[i + 1][j] && memo[i][j - 1] && memo[i][j + 1]){ int minC = min({memo[i - 1][j], memo[i + 1][j], memo[i][j - 1], memo[i][j + 1]}); board[i][j] += minC; memo[i - 1][j] -= minC; memo[i + 1][j] -= minC; memo[i][j - 1] -= minC; memo[i][j + 1] -= minC; } } } // 3. 出力. rep(i, N){ rep(j, M) printf("%d", board[i][j]); puts(""); } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
[入力例] 3 3 010 101 010 [出力例] 000 010 000 ※AtCoderのテストケースより [入力例] 3 4 0230 2323 0230 [出力例] 0000 0230 0000 ※AtCoderのテストケースより [入力例] 5 5 00100 03040 20903 05060 00300 [出力例] 00000 00100 02030 00300 00000 ※AtCoderのテストケースより [入力例] 12 7 0000050 0101505 1030450 0215030 0111590 0012929 0610790 6166250 0612520 0007210 0070801 0007010 [出力例] 0000000 0000050 0101000 0010300 0010000 0000290 0000000 0610500 0000200 0000000 0007010 0000000 |
■参照サイト
AtCoder Regular Contest 041