Codeforces Round #527 (Div. 3) 总结 A B C D1 D2 F
Posted fo0o0ol
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #527 (Div. 3) 总结 A B C D1 D2 F相关的知识,希望对你有一定的参考价值。
A
贪心的取
每个字母n/k次
令r=n%k
让前r个字母各取一次
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 #define rep(i, a, b) for (int i = a; i <= b; ++i) 5 6 int t, n, k; 7 8 int main() { 9 cin >> t; 10 11 while (t--) { 12 cin >> n >> k; 13 int x = n / k, r = n - x * k; 14 rep(i, 1, k) rep(j, 1, x) { 15 cout << (char)(‘a‘ + i - 1); 16 } 17 rep(i, 1, r) { 18 cout << (char)(‘a‘ + i - 1); 19 } 20 cout << ‘ ‘; 21 } 22 return 0; 23 }
B
排序完连续两个比较
证明一下吧:max(a[2] - a[1]), a[4] - a[3]) <= max(a[3] - a[1]), a[4] - a[2]) (后者的间隙大)
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 #define rep(i, a, b) for (int i = a; i <= b; ++i) 5 6 const int N = 1e5 + 7; 7 8 int n, a[N]; 9 10 int main() { 11 cin >> n; 12 13 rep(i, 1, n) { 14 cin >> a[i]; 15 } 16 17 sort(a + 1, a + n + 1); 18 19 int ans = 0; 20 rep(i, 1, n / 2) { 21 ans += a[i * 2] - a[i * 2 - 1]; 22 } 23 24 cout << ans << ‘ ‘; 25 26 return 0; 27 }
C
题意难理解
找出最长的两个串 判断哪个是最长前缀
f[len]代表着长度为len的前后缀是否被选 用来避免长度为len的两个子串都是前缀或都是后缀的情况
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 #define rep(i, a, b) for (ll i = a; i <= b; ++i) 5 6 const int N = 507; 7 8 ll n; 9 bool f[N]; 10 string s[N]; 11 12 int main() { 13 cin >> n; ll m = 2 * n - 2; 14 15 string s1 = "", s2 = ""; 16 rep(i, 1, m) { 17 cin >> s[i]; 18 if (s[i].size() > s1.size()) s1 = s[i]; 19 else if (s[i].size() == s1.size()) s2 = s[i]; 20 } 21 22 ll cnt = 0; 23 rep(i, 1, m) if (s1.substr(0, s[i].size()) == s[i]) { 24 if (s[i] != s2) cnt++; 25 } 26 27 string pre; 28 if (cnt >= n - 1 && s1.substr(1, s1.size() - 1) == s2.substr(0, s1.size() - 1)) pre = s1; else pre = s2; 29 30 rep(i, 1, m) { 31 if (pre.substr(0, s[i].size()) == s[i] && !f[s[i].size()]) { 32 cout << ‘P‘; 33 f[s[i].size()] = 1; 34 } 35 else cout << ‘S‘; 36 } 37 38 return 0; 39 }
D1 D2
两题类似
用一个栈 若当前高度和栈顶一致就弹出 具体判断见代码
1 //D1 2 #include <bits/stdc++.h> 3 using namespace std; 4 typedef long long ll; 5 #define rep(i, a, b) for (int i = a; i <= b; ++i) 6 7 const int N = 2e5 + 7; 8 9 int n; 10 bool a[N]; 11 stack <bool> st; 12 13 int main() { 14 scanf("%d", &n); 15 16 int x; 17 rep(i, 1, n) { 18 scanf("%d", &x); 19 a[i] = x & 1; 20 } 21 22 rep(i, 1, n) { 23 if(st.empty()) 24 st.push(a[i]); 25 else if(a[i] == st.top()) 26 st.pop(); 27 else 28 st.push(a[i]); 29 } 30 31 st.size() > 1 ? puts("NO") : puts("YES"); 32 33 return 0; 34 }
1 //D2 2 #include <bits/stdc++.h> 3 using namespace std; 4 typedef long long ll; 5 #define rep(i, a, b) for (int i = a; i <= b; ++i) 6 7 int n, mx; 8 stack <int> s; 9 10 int main() { 11 scanf("%d", &n); 12 13 int x; 14 rep(i, 1, n) { 15 scanf("%d",&x); 16 mx=max(mx,x); 17 if (s.empty()) 18 s.push(x); 19 else if (s.top()==x) 20 s.pop(); 21 else if (s.top()>=x) 22 s.push(x); 23 else { 24 puts("NO"); 25 return 0; 26 } 27 } 28 29 if (!s.size()) 30 puts("YES"); 31 else if (s.size() == 1 && s.top() == mx) 32 puts("YES"); 33 else 34 puts("NO"); 35 36 return 0; 37 }
E
blank
F
初始思路是:维护树上的一堆和乱搞
其实dfs2的操作十分巧妙
1.res -= sum[v] 代表 v子树下的每个点的距离都少了1
2.sum[u] -= sum[v]; 为下一步做准备
3.res += sum[u]; 代表 u子树下(除)的每个点的距离都多了1
4.sum[v]+=sum[u]; dfs2到下一层的时候 (假如边为(v,w)) res+= sum[v](即步骤3)的时候v点外u点子树下的一些点到w的距离也多了1
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 5 #define rep(i, a, b) for (int i = a; i <= b; ++i) 6 7 const int N = 2e5 + 7; 8 9 int n; 10 11 ll a[N], sum[N], res, ans; 12 13 vector <int> e[N]; 14 15 void dfs1(int u, int p = 0, int d = 0) { 16 res += d * a[u]; 17 sum[u] = a[u]; 18 for (auto v : e[u]) if (v != p) { 19 dfs1(v, u, d + 1); 20 sum[u] += sum[v]; 21 } 22 } 23 24 void dfs2(int u, int p = 0) { 25 ans = max(ans, res); 26 for (auto v : e[u]) if (v != p) { 27 res -= sum[v]; 28 sum[u] -= sum[v]; 29 res += sum[u]; 30 sum[v] += sum[u]; 31 32 dfs2(v, u); 33 34 sum[v] -= sum[u]; 35 res -= sum[u]; 36 sum[u] += sum[v]; 37 res += sum[v]; 38 } 39 } 40 41 int main() { 42 scanf("%d", &n); 43 44 rep(i, 1, n) { 45 scanf("%lld", &a[i]); 46 } 47 48 int u, v; 49 rep(i, 1, n - 1) { 50 scanf("%d%d", &u, &v); 51 e[u].push_back(v); 52 e[v].push_back(u); 53 } 54 55 dfs1(1); 56 dfs2(1); 57 58 printf("%lld ", ans); 59 60 return 0; 61 }
以上是关于Codeforces Round #527 (Div. 3) 总结 A B C D1 D2 F的主要内容,如果未能解决你的问题,请参考以下文章
CodeForces Round #527 (Div3) C. Prefixes and Suffixes
cf codeforces round#527F. Tree with Maximum Cost树形dp
Codeforces Round #527 (Div. 3) 总结 A B C D1 D2 F
Codeforces Round #436 E. Fire(背包dp+输出路径)