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 << ‘\n‘;
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 << ‘\n‘;
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\n", ans);
59
60     return 0;
61 }

原文地址:https://www.cnblogs.com/Fo0o0ol/p/10164365.html

时间: 2024-08-30 14:25:39

Codeforces Round #527 (Div. 3) 总结 A B C D1 D2 F的相关文章

Codeforces Round #527 (Div. 3)D2(栈,思维)

#include<bits/stdc++.h>using namespace std;int a[200007];stack<int>s;int main(){    int n;    int mn=0;    scanf("%d",&n);    for(int i=1;i<=n;i++){        scanf("%d",&a[i]);        if(a[i]>mn)            mn=a

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd

Codeforces Round #273 (Div. 2)

Codeforces Round #273 (Div. 2) 题目链接 A:签到,仅仅要推断总和是不是5的倍数就可以,注意推断0的情况 B:最大值的情况是每一个集合先放1个,剩下都丢到一个集合去,最小值是尽量平均去分 C:假如3种球从小到大是a, b, c,那么假设(a + b) 2 <= c这个比較明显答案就是a + b了.由于c肯定要剩余了,假设(a + b)2 > c的话,就肯定能构造出最优的(a + b + c) / 3,由于肯定能够先拿a和b去消除c,而且控制a和b成2倍关系或者消除

Codeforces Round #339 (Div. 2) B. Gena&#39;s Code

B. Gena's Code It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, f

Codeforces Round #315 (Div. 1)

A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and un

DP Codeforces Round #303 (Div. 2) C. Woodcutters

题目传送门 1 /* 2 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 3 问最多能砍到多少棵树 4 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 5 分情况讨论,若符合就取最大值更新,线性dp,自己做出来了:) 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath> 11 #include &