C++の練習を兼ねて, AtCoder Regular Contest 140 の 問題A (Right String) ~ 問題B (Shorten ARC) を解いてみた.
■感想.
1. 問題A, Bは, 方針が見えなかったので, 解説を参考にして, ようやく, AC版に到達出来た.
2. 引き続き, 時間を見つけて, 過去問の学習を進めていきたいと思う.
本家のサイト AtCoder Regular Contest 140 解説 の 各リンク を ご覧下さい.
■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 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 |
// 解き直し. // https://atcoder.jp/contests/arc140/editorial/3964 // 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--) int main(){ // 1. 入力情報. int N, K; char c[202020]; scanf("%d %d %s", &N, &K, c); string s(c); s += s; // 2. 約数. auto div = [&](int X) -> set<int> { set<int> ret; repx(d, 1, (int)sqrt(X) + 1){ if(!(X % d)){ ret.insert(d); if(d * d != X) ret.insert(X / d); } } return ret; }; // 3. N の 約数. set<int> nDiv = div(N); // 4. f[S] の 有り得る最小値は? int ans = 2020202020; for(auto &m : nDiv){ string cur = s; int d = 0; // f[S] = m が 可能か? rep(i, m){ // 各文字の出現回数. int memo[26]; rep(j, 26) memo[j] = 0; rep(j, N / m) ++memo[cur[i + m * j] - 'a']; // 最大値. int mCur = 0; rep(j, 26) mCur = max(mCur, memo[j]); // 差分. // -> 最大値の内, いずれか, 一つに合わせるため, 最大値 1個分 を 減算. rep(j, 26) d += memo[j]; d -= mCur; } // 最小値更新. // -> 最小 m が, 見つかったことになるため, 終了. if(d <= K){ ans = min(ans, m); break; } } // 5. 出力. 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 |
[入力例] 4 1 abac [出力例] 2 ※AtCoderのテストケースより [入力例] 10 0 aaaaaaaaaa [出力例] 1 ※AtCoderのテストケースより [入力例] 50 39 fmrbfvlfopklpalqehekzachoddrdcxqqfcgressccewbagfnp [出力例] 10 [入力例] 100 77 rawmtbwjsjgnjmpafljmtayqtmjomnknutldmhzjsyouegubketezielclyuusdbaxlogxrlevlnpmxtfpkxqkyichgwujcwmkho [出力例] 20 [入力例] 250 185 zvsgwjqbirpmmrnvnuosfxdzucqeolhbijogtrigujfnuxlvmiugpyljrjdxvkxifdrjcsdwftnnsjxguixecbwganebcpnoaatienaohbwwudjsxesvxvgsykuwpelbnmgirrxokffrmcoutgjuogzfvfwtikxbvftsylturlbifsydhimgfoimcwsvthsdnjytbpywnmjaoghgdtmsdwhevvzpfymlziunrjcakpkfljdifkxjudcffj [出力例] 50 [入力例] 5000 2022 mofhhafhcfakhcimiemggefmkmmchfikfgfehagiocmctascpeqtlversbzhotisfqiifovrrpbpwctjjuujptwaxppjgkllomeadkfcbbfchefcedeakjodlbhmbagceglcacjeibobmmmnnchdamlkjlfddilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibcgkgbdcffkejddgblicnbdnhhohbcmmofhhafhcfakhcimiemggefmkmmchfikfgfehagiocmcamjheliblgnlnlingljfkkogelgnagfjckfnmbddeeielncegkllomeadkfcbbfchefcedeakjodlbhmbagceglcacjeibobmmmnnchdamlkjlfddilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibcgkgbdcffkejddgblicnbdnhhohbcmmofhhafhcfakhcimiemggefmkmmchfikfgfehagiocmcamjheliblgnlnlingljfkkogelgnagfjckfnmbddeeielncegkllomeadkfcbbfchefcedeakjodlbhmbagceglcacjeibobmmmnnchdamlkjlfddilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibcgkgbdcffkejddgblicnbdnhhohbcmmofhhafhcfakhcimiemggefmkmmchfikfgfehagiocmcamjheliblgnlnlingljfkkogelgnagfjckfnmbddeeielncegkllomeadkfcbbfchefcedeakjodlbhmbagceglcacjeibobmmmnnchdamlkjlfddilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibcgkgbdcffkejddgblicnbdnhhohbcmdxdxqlkqrberieoznulkbxdvrlzxfpxgghshodiajmrwovnqmvgmuwdacyxtjgjdypdmsmceizqyfqdqjzvahmutgtacenldomphsmixwdcmwllxooqcimphxrhtmxobupgneeyrxmlfwgtbecjyzwdqdfkxsilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibcqylajdbwfpsxebfmtwelmrkqgypaaxurrvihhxjpqjrvxvnlcgefmkmmchfikfgfehagiocmcamjheliblgnlnlingljfkkogelgnagfjckfnmbddeeielncegkllomeadkfcbbfchefcedeakjodlbhmbagceglcacjeibobmmmnnchdamlkjlfddilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibcgkgbdcffkejddgblicnbdnhhohbcmmofhhafhcfakhcimiemggefmkmmchfikfgfehagiocmcamjheliblgnlnlingljfkkogelgnagfjckfnmbddeeielncegkllomeadkfcbbfchefcedeakjodlbhmbagceglcacjeibobmmmnnchdamlkjlfddilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibcgkgbdcffkejddgblicnbdnhhohbcmmofhhafhcfakhcimiemggefmkmmchfikfgfehagiocmcamjheliblgnlnlingljfkkogelgnagfjckfnmbddeeielncegkllomeadkfcbbfchefcedeakjodlbhmbagceglcacjeibobmmmnnchdamlkjlfddilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibcgkgbdcffkejddgblicnbdnhhohbcmsbdnlwfikfsocklkiirbvmeibiqelisfftgllchnqwzxfpimhbmcyzgwpkyfrafnggpdlmvniptuqfwrrvhobsswrwalnaaacsgyshwszlrsgjanykvtjqeurruxioqrjwdwefqwtxfelessqpbqcgguxqftoilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibccgkpfnzrwgplrvsyxvfepmscvlcidyzweirtegdsgifuqifrjgefmkmmchfikfgfehagiocmcamjheliblgnlnlingljfkkogelgnagfjckfnmbddeeielncegkllomeadkfcbbfchefcedeakjodlbhmbagceglcacjeibobmmmnnchdamlkjlfddilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibcgkgbdcffkejddgblicnbdnhhohbcmmofhhafhcfakhcimiemggefmkmmchfikfgfehagiocmcamjheliblgnlnlingljfkkogelgnagfjckfnmbddeeielncegkllomeadkfcbbfchefcedeakjodlbhmbagceglcacjeibobmmmnnchdamlkjlfddilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibcgkgbdcffkejddgblicnbdnhhohbcmmofhhafhcfakhcimiemggefmkmmchfikfgfehagiocmcamjheliblgnlnlingljfkkogelgnagfjckfnmbddeeielncegkllomeadkfcbbfchefcedeakjodlbhmbagceglcacjeibobmmmnnchdamlkjlfddilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibcgkgbdcffkejddgblicnbdnhhohbcmkulealhqpcazqvehepghlztgazeyutiinlamabupafvwpjnhlccvalhcfqxlptwqxznsbakneccjwwyjwlgwcnrlvuystrwfturqlbdgmuodiuezxxaszzabaoteczkqgxtndduqpuysmuoxybthpgripoxvbilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibcdqobykmiafzjmxrlrkpmgufusepxymprjbifhpisukettxypdgefmkmmchfikfgfehagiocmcamjheliblgnlnlingljfkkogelgnagfjckfnmbddeeielncegkllomeadkfcbbfchefcedeakjodlbhmbagceglcacjeibobmmmnnchdamlkjlfddilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibcgkgbdcffkejddgblicnbdnhhohbcmmofhhafhcfakhcimiemggefmkmmchfikfgfehagiocmcamjheliblgnlnlingljfkkogelgnagfjckfnmbddeeielncegkllomeadkfcbbfchefcedeakjodlbhmbagceglcacjeibobmmmnnchdamlkjlfddilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibcgkgbdcffkejddgblicnbdnhhohbcmmofhhafhcfakhcimiemggefmkmmchfikfgfehagiocmcamjheliblgnlnlingljfkkogelgnagfjckfnmbddeeielncegkllomeadkfcbbfchefcedeakjodlbhmbagceglcacjeibobmmmnnchdamlkjlfddilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibcgkgbdcffkejddgblicnbdnhhohbcmqgeveqgzhxduxmxqtgpwykehjglxkholhsepilctuaxvqyqnrchbbqxaqowljlplastpcxugsiesansceenvtmlciewuvqqriamhebpirmwrzbvibtmxtvnfukmipwtgmquzfyrhzhtrukohvpemiwjbskvfcilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibcpxwnqiwyismhssaulvygsccgjzneoyeblbnhpfqnyglgezgcegefmkmmchfikfgfehagiocmcamjheliblgnlnlingljfkkogelgnagfjckfnmbddeeielncegkllomeadkfcbbfchefcedeakjodlbhmbagceglcacjeibobmmmnnchdamlkjlfddilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibcgkgbdcffkejddgblicnbdnhhohbcmmofhhafhcfakhcimiemggefmkmmchfikfgfehagiocmcamjheliblgnlnlingljfkkogelgnagfjckfnmbddeeielncegkllomeadkfcbbfchefcedeakjodlbhmbagceglcacjeibobmmmnnchdamlkjlfddilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibcgkgbdcffkejddgblicnbdnhhohbcmmofhhafhcfakhcimiemggefmkmmchfikfgfehagiocmcamjheliblgnlnlingljfkkogelgnagfjckfnmbddeeielncegkllomeadkfcbbfchefcedeakjodlbhmbagceglcacjeibobmmmnnchdamlkjlfddilbgdfgkckcfhcbebgdeigdbeffgelcjahjjjgalenddcjngiblnlkkmjmjloibcgkgbdcffkejddgblicnbdnhhohbcm [出力例] 250 ※ 整数 N が, 問題文の制約条件から範囲外であるケース. |
■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 53 |
// 解き直し. // https://atcoder.jp/contests/arc140/editorial/3894 // C++(GCC 9.2.1) #include <bits/stdc++.h> using namespace std; using vi = vector<int>; #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 pb push_back int main(){ // 1. 入力情報. int N; char x[202020]; scanf("%d %s", &N, x); string s(x); // 2. ブロックに分割. vi v; repx(i, 1, N - 1){ int a = 0, c = 0; // ..XRX.. となる 位置. if(s[i - 1] != 'R' && s[i] == 'R' && s[i + 1] != 'R'){ // 左側 の 'A'. repr(j, i - 1, 0){ if(s[j] == 'A') ++a; else break; } // 右側 の 'C'. repx(j, i + 1, N){ if(s[j] == 'C') ++c; else break; } } // ブロックを保存. if(a && c) v.pb(min(a, c)); } // 3. 集計. int ans = 0; for(auto &p : v) ans += p; ans = min(ans, (int)v.size() * 2); // 4. 出力. 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 |
[入力例] 6 AARCCC [出力例] 2 ※AtCoderのテストケースより [入力例] 5 AAAAA [出力例] 0 ※AtCoderのテストケースより [入力例] 9 ARCARCARC [出力例] 3 ※AtCoderのテストケースより [入力例] 100 AARRARCRRCACRCACCCACCCCACCRCAARCCARARRCACCACAAARCAARCCAACCRRCCCARRACCCRAARARARCCRRCRAACRRCARAARCCRRR [出力例] 9 [入力例] 500 ACRRCRRRRCCAARCAACAAACRCCCAAARCACAAAARRAAARCAACCACACRCACCRCACCCAACARRRAARAARARARARRAARCCCRCCRCARARAACACCARAACCRACRCRCCRCRCCCRCRACACCCARRAAAACARCACRCACRCRCAARCRCACACACCRCCACCAACARAARCRARCACRCCRRACAACCARRRAACRRCCRRCRRRRARCRRAAARCRCRCACAACAARCAAARRRCCRCCRCCARARACARCACACAAACCRRCRCCCACARRCRARCCRRAARACRCAACRRCARAAARACRARARCCRCARRCARARACCRACRCRACCACAAACRRRCARCACAAAARACARRARCCRAARRACRRCRACRACACCCACCRRRRCAARAAARRAARRRRARRAAARARCRCRRCCAACACRAACARAACRCCARARCAARCCACACCACCARRCRCARCRAACRCCRRRCCACRARRAACRRRACR [出力例] 22 [入力例] 3000 ARCACRCAACCRRAACCCRCCCCARACAARRACRRACAACACRACRCCRACCRCRARRCARCCCCRRRRRCACCACCCRRARRRARCRACAARCCCAARRCARACARRRRRRCAARRCARRRCAACCCRARCCCRCCRRACRRCRARRARRCAAACAACRRAAAARARRCCCRRARCARACARACRRACRCRCRRCAACACACCCACRRCRCAAARCRARCRRRACRCRRAACCCRRARACARRARRAARCAACCARCACRACRCRCARACAACAARRACRCCRRCACACCACRACARACARAACAACAARAARACAARACARCCACCCCAARRRAACRAAACRAARCCCRCCRCCACAACRRARCAACAARCARCRCACACCRRCCCCRCRAAARCCACCARCCRCARAARARAAAARRACRARCRCRCRCCRRACCRCRACCRCCCACCRRRRRCARAAARCRCAAARARACRCACAARCCACCRARCRCRACAARAAAAAARCRRACCRCAARRACCCRRCCAARRCRRCCRCCCCRACCRARACCCRRACRRARARAACCARARAAAARCRCCCCRRRACRCRAARCCRAAARRAARCAARAACCARRRCCARRCRACRAARACCCRRACRRRRRARCRRRRRACCARCCRARARRRRACCRAAACAACCRCRARACCCAARCARARCRCRRCRCACRCCCRAAARRRRCRCRCAARCACARAACRACRAARCARRCCARRACRCRAAARRCAACCCAAACAACACCRCRCRACAACCARCRARRACRRCCACRARCACCARARACARCCAACRCCCCCAAAACCRCRRARAAACCCARRARRARCRRRRRARRRCRRARRRCRAACAARRAAACACCRCCACCRACCCRARACCCRCARCAACCRCCARCCCRCACAAAAARCCRARCACCACCCCRCRCARRRAARRRRRRCRAAACARAAACRRCRCCRCCRACARACAACAARRRRCRCCACCRCARARRCRRARRCCAACCCRACCCACAARRRRCCCAARRRRAARAARACCCCCARCRRRCARACCRCRCCRRRARARRCARRARCARRCCRCACCCAARCARCRCARCACCRAACCARCRCRRARRRCCCAACRARARCCCCRRARRRAARARRRRCRCCARRARRCRCRACCCCCCRCCRCCARARRRAAARCRARCRCRRRCCACCCRRARRCCACRRARACRAACRRARRCARCRRCACRCCACCCRRAARACRRAAAACAACAAARRAACRRCCRACACAAAARCRCCARRCACRRARAACRRCRACAAACRAACRRCRCCCCCAAARAACAACARACCACCRCRACCCARRCACAACRCAAARCARRRRRARRACRCCRCACARARCACCAACAAARCRAARRAARACARCCACRCCARRRRCRCCRAAARCCAACRRCCRRCRARCCRCARCAACRRRAARCRARRARRCACARAAAAACARACCCACRAARAACCCAARRCRAAARCARCAAAAACRRCAAARRCACARCRRCCRARACCCAACACCCCRCAACCCCRCRARRAARARRACCCCRAARRRCACARACRAARRCRRRCCACRACRACRCCACACRARRCCARRACARCRRACCRCAARRCRCRRCRAARRRCCCRACCRRAACRRRRACACAAAAARCCARACACACCRARRCRARCACCCCCRCCARRRRRRRAAARCCCRRCARCAACAAAARCARAACARAACACCRCCCCRCARRCRCARRRRCAACCCCRACCRCCRCRCRCARRCCACRCCARCRAAACAACACCRCAAAACRARAARRRAAARCCCARCCARRRRRCRRCACRRCRACRCCARCARCACARRCARRCRRACRCRARAARCARAARCARCCARACRACACRARARRCRACARCRARCAACCCRCACRRCCARACCCAAAAARARACAAARAAAACRARCRACRRACCCRRCARRCARCRRACACCARCRACCRRCCRARAARAAAACCRCCCRRRCRRCCCCRRRCARRRRRCARAARRCCCRRCARRRCARRRCCCARCCRAACCACACAARCCACRCRCCRCCCCRCAAACRCCRACAAAAACACARAARRCCCACAAARCRRACRCCARCACAACAAAAAACAARAACRAAACCAARAACRAARCRRCCACAACRACRRRCARRCCAACRRCCCRAAAARCCCRARRRCRRAACCACRAACRARARAARRAACRCRCARARACCRRAACRRARCRCCRACARRRCAARAAACCRCRRRCRRRRCACARAAACCRACCCCCRRAARACRACRRACCACARACRRRRCARCCAARACRCAACCCAAARRACRAAACRRAARCRCCRAACRAACAARCCAACACCRRCRCCCRCAARRRRCACCACACCRRACCRARAAACRARRRRARCRACRARRACRCACCRRRARCRAAARCACACARCACRCCARACACARCRCRRARRCACARCARRAARRAAARRCARACAACRCACARRCRACCRCRACACACCCAARCAACACRCACRRRARARRARRCRRCRCRACARARCCCAACRRACACRCACRCRCAACCRARRRRRARCCCRRARCRCCCRRCACRRRRRRARRAARACARCARRCRACRAACRARRRAARARAAARAACCCCACRAAAACCAARRRCCCACCRCCRCCCRACAAARRRCRAAACCRAAACRCARCCCAACCRCCAARRACCRARACARCACARARRAARCCACCCAAAACRCRRCACRACCCCRCCRRCRCRRACRRARRCCARCCARRACAARACRACCCAACACCRRCRCARACRCRCRARCRCCRAACAARCCRARRRCACAAAARCRARARRRRRACCCCCACACRARCCACRRRCARCARRACACRRRRAAAACRRCCRRCCACCRARCACCRCCCAAARCCRCCRRCCCARRCAACCCRACAAAAAARACCCACCAARCARCCA [出力例] 133 |
■参照サイト
AtCoder Regular Contest 140