Codeforces Round #604 (Div. 2)
题目链接:https://codeforces.com/contest/1265
A:Beautiful String
题意:给出一个字符串,字符串只包含a,b,c,? 四种字符,要求把其中的 ?替换成 a,b,c 问能不能构造成完美的字符串。输出替换成功的字符串,不能就输出-1。完美:任意两个相邻的字符都不相等。
idea:就几种情况,暴力签到
时间复杂度:O(n)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 using namespace std; 6 int t; 7 string s; 8 9 int main() 10 { 11 scanf("%d",&t); 12 while (t -- ) 13 { 14 cin >> s; 15 int flag = 0, len = s.size(); 16 for (int i = 0; i < len; i ++ ) 17 if (s[i] == s[i + 1] && s[i] != '?' && s[i + 1] != '?') flag = 1; 18 if (flag) puts("-1"); 19 else 20 { 21 for (int i = 0; i < len; i ++ ) 22 { 23 if (s[i] != '?') cout << s[i]; 24 else 25 { 26 if (s[i + 1] == '?') 27 { 28 if (s[i - 1] == 'a') s[i] = 'b'; 29 else if (s[i - 1] == 'b') s[i] = 'c'; 30 else s[i] = 'a'; 31 cout << s[i]; 32 } 33 else 34 { 35 if (s[i - 1] != 'a' && s[i + 1] != 'a') s[i] = 'a'; 36 if (s[i - 1] != 'b' && s[i + 1] != 'b') s[i] = 'b'; 37 if (s[i - 1] != 'c' && s[i + 1] != 'c') s[i] = 'c'; 38 cout << s[i]; 39 } 40 } 41 } 42 puts(""); 43 } 44 } 45 return 0;
B:Beautiful Numbers
题意:给出一个无序数组,从1~n询问每个数m,能否有包含1~m且长度是m的连续集合。若能输出1,否输出0
idea:预处理出来每个数距离 1 的距离,若当前查询长度为m,因为序列要包括 1 ~ m ,所以每次从1 ~ m 距离1的距离中更新边界即可。
时间复杂度:O(n)
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N = 2e5 + 10; 4 int t, n, a[N], cnt[N]; 5 6 int main() 7 { 8 scanf("%d",&t); 9 while (t -- ) 10 { 11 scanf("%d",&n); 12 int s; 13 for (int i = 1; i <= n; i ++ ) 14 { 15 scanf("%d",&a[i]); 16 if (a[i] == 1) s = i; 17 } 18 for (int i = 1; i <= n; i ++ ) cnt[a[i]] = i - s; 19 int l, r; 20 l = r = 0; 21 for (int i = 1; i <= n; i ++ ) 22 { 23 l = min(l, cnt[i]); 24 r = max(r, cnt[i]); 25 if ((abs(l) + r + 1) == i) cout << "1"; 26 else cout << "0"; 27 } 28 puts(""); 29 } 30 return 0; 31 }
C:Beautiful Regional Contest
题意:给出每个人的过题数,最多能分别发多少奖牌,且 金 < 银 && 金 < 铜,金 + 银 + 铜 <= n / 2
idea:找间断点即可
1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 const int N = 4e5 + 10; 6 int t, n, a[N]; 7 8 int main() 9 { 10 scanf("%d",&t); 11 while (t -- ) 12 { 13 scanf("%d",&n); 14 for (int i = 1; i <= n; i ++ ) 15 scanf("%d",&a[i]); 16 int x = 1, y, z; 17 while (x < n && a[x] == a[x + 1]) x ++ ; 18 y = 2 * x + 1; 19 while (y < n && a[y] == a[y + 1]) y ++ ; 20 z = n / 2; 21 while (z > y && a[z] == a[z + 1]) z -- ; 22 if (z - y > x) printf("%d %d %d\n",x,y - x,z - y); 23 else puts("0 0 0"); 24 } 25 return 0; 26 }