C++の練習を兼ねて, AtCoder Grand Contest 023 の 問題A (Zero-Sum Ranges) ~ 問題B (Find Symmetries) を解いてみた.
■感想.
1. 問題Bは, 解答方針が見えなかったので, 解説を参考に実装して, ようやく, AC版に到達できた.
2. 時間を見つけて, 引き続き, 過去問を振り返っていきたいと思う.
本家のサイト AtCoder Grand Contest 023 解説 を ご覧下さい.
■C++版プログラム(問題A/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 |
// C++(GCC 9.2.1) #include <bits/stdc++.h> using namespace std; using LL = long long; #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--) #define a first #define b second int main(){ // 1. 入力情報. int N; scanf("%d", &N); map<LL, LL> m; LL cum = 0, a = 0, ans = 0; m[0]++; rep(i, N){ scanf("%lld", &a); cum += a; m[cum]++; } // 2. 数え上げる. for(auto &p : m) ans += p.b * (p.b - 1LL); // 3. 出力. printf("%lld\n", ans / 2LL); 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 |
[入力例] 6 1 3 -4 2 2 -2 [出力例] 3 ※AtCoderテストケースより [入力例] 7 1 -1 1 -1 1 -1 1 [出力例] 12 ※AtCoderテストケースより [入力例] 5 1 -2 3 -4 5 [出力例] 0 ※AtCoderテストケースより [入力例] 20 5 4 10 2 -11 -6 1 -7 6 -19 16 8 -5 13 12 -8 2 -10 5 7 [出力例] 6 [入力例] 100 77 0 -61 12 56 -57 17 54 73 -75 -27 95 98 43 -44 122 -99 75 101 -121 73 100 -3 -70 108 68 -29 -10 40 119 2 -99 -48 101 71 -100 -115 54 60 76 -87 15 0 -81 1 -80 93 51 -119 49 -49 0 33 18 -14 9 10 -110 71 100 56 -83 -93 53 22 68 -104 -40 64 92 -49 -80 77 52 -44 -76 -27 37 6 89 -55 8 82 122 -98 63 21 -46 54 -68 93 -19 8 117 -46 17 -19 62 -28 -44 [出力例] 16 |
■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 43 44 45 46 47 48 49 50 51 52 |
// 解き直し. // https://img.atcoder.jp/agc023/editorial.pdf // C++(GCC 9.2.1) #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--) char board[606][606]; int main(){ // 1. 入力情報. int N; scanf("%d", &N); rep(i, N){ char c[303]; scanf("%s", c); rep(j, N){ board[i + 0][j + 0] = c[j]; board[i + 0][j + N] = c[j]; board[i + N][j + 0] = c[j]; board[i + N][j + N] = c[j]; } } // 2. 数え上げる. // (0, 0), (0, 1), ... ,(0, N - 1) まで確認. int ans = 0; rep(b, N){ // 2-1. よい盤面であるかチェック. bool ok = true; rep(i, N){ repx(j, i + 1, N){ if(board[i][j + b] != board[j][i + b]){ ok = false; break; } } if(!ok) break; } // 2-2. よい盤面の場合は, カウント. if(ok) ans += N; } // 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 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
[入力例] 2 ab ca [出力例] 2 ※AtCoderテストケースより [入力例] 4 aaaa aaaa aaaa aaaa [出力例] 16 ※AtCoderテストケースより [入力例] 5 abcde fghij klmno pqrst uvwxy [出力例] 0 ※AtCoderテストケースより [入力例] 7 aaaaaaa aaaaaaa aaaaaaa aaaaaaa aaaaaaa aaaaaaa aaaaaaa [出力例] 49 [入力例] 15 rlhiwkwildrtijb lphgywtzxclyytw hhossacybbkxfqm igsnzwborecahgw wyszpmulrbvgooh kwawmydmkfgtsum wtcbudcnfcyqjjx izyolmnpwbbovxq lxbrrkfwrmjdmzl dcbebfcbmiphpnz rlkcvgybjpsnwwc tyxagtqodhnjccs iyfhosjvmpwcjum jtqgoujxznwcugo bwmwhmxqlzcsmov [出力例] 15 [入力例] 25 aaaabaaaabaaaaaaaaaaaaaaa aaabaaaabaaaaaaaaaaaaaaaa aabaaaabaaaaaaaaaaaaaaaaa abaaaabaaaaaaaaaaaaaaaaaa baaaabaaaaaaaaaaaaaaaaaaa aaaabaaaaaaaaaaaaaaaaaaaa aaabaaaaaaaaaaaaaaaaaaaaa aabaaaaaaaaaaaaaaaaaaaaaa abaaaaaaaaaaaaaaaaaaaaaaa baaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaab aaaaaaaaaaaaaaaaaaaaaaaba aaaaaaaaaaaaaaaaaaaaaabaa aaaaaaaaaaaaaaaaaaaaabaaa aaaaaaaaaaaaaaaaaaaabaaaa aaaaaaaaaaaaaaaaaaabaaaab aaaaaaaaaaaaaaaaaabaaaaba aaaaaaaaaaaaaaaaabaaaabaa aaaaaaaaaaaaaaaabaaaabaaa aaaaaaaaaaaaaaabaaaabaaaa [出力例] 25 |
■参照サイト
AtCoder Grand Contest 023