Codeforces Round #435 (Div. 2)【A、B、C、D】

//在我对着D题发呆的时候,柴神秒掉了D题并说:这个D感觉比C题简单呀!,,我:[哭.jpg](逃

Codeforces Round #435 (Div. 2)

codeforces 862 A. Mahmoud and Ehab and the MEX【水】

题意:定义一个集合的MEX为其中的最小的不在集合里的非负整数,现在给一个大小为N的集合和X,每次操作可以向其中加入一个非负整数或删除一个数,问最小的操作次数,使得这个集合的MEX为X。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int a[105];
 6 int main() {
 7     int n, x, i, j, ans = 0, cnt = 0;
 8     scanf("%d %d", &n, &x);
 9     for(i = 1; i <= n; ++i) {
10         scanf("%d", &a[i]);
11         if(a[i] < x) cnt++;
12         else if(a[i] == x) ans++;
13     }
14     ans += x - cnt;
15     printf("%d
", ans);
16     return 0;
17 }
15ms

codeforces 862 B. Mahmoud and Ehab and the bipartiteness【dfs】

题意:给一棵n个结点的树,问最多能加多少边使得其是二分图并且不能有重边和自环。

题解:dfs染色算出可匹配最多数,将其减去n-1即为答案。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<vector>
 5 using namespace std;
 6 vector<int>g[100005];
 7 int a[2];
 8 void dfs(int x, int fa, int t) {
 9     a[t]++;
10     int len = g[x].size();
11     for(int i = 0; i < len; ++i)
12         if(g[x][i] != fa) dfs(g[x][i], x, t^1);
13 }
14 int main() {
15     int n, u, v, i;
16     scanf("%d", &n);
17     for(i = 0; i <= n; ++i) g[i].clear();
18     a[0] = a[1] = 0;
19     for(i = 0; i<n-1; ++i) {
20         scanf("%d%d", &u, &v);
21         g[u].push_back(v); g[v].push_back(u);
22     }
23     dfs(1, 0, 0);
24     long long ans = 1ll*a[0]*a[1] - (n-1);
25     printf("%lld
", ans);
26     return 0;
27 }
78ms

codeforces 862 C. Mahmoud and Ehab and the xor【构造】

题意:要构造n(≤10^5)个不同的非负整数(每个数≤10^6),使得它们异或和为x(≤10^5)。

题解:考察异或的自反性。注意观察数据范围,用特殊数据完成构造。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<vector>
 5 using namespace std;
 6 
 7 int main() {
 8     int n, x, i;
 9     scanf("%d%d", &n, &x);
10     if(n==1) printf("YES
%d
", x);
11     else if(n == 2) {if(!x) puts("NO");else printf("YES
0 %d
", x);}
12     else {
13         printf("YES
");
14         for(i = 0; i < n-3; ++i) {printf("%d ", i); x ^= i;}
15         printf("%d %d %d
", (1<<18), (1<<19), (1<<18)^(1<<19)^x);
16     }
17     return 0;
18 }
31ms

待补。。= _ =

codeforces 862 D. Mahmoud and Ehab and the binary string【二分】

题意:已知一个01字符串的长度n (2 ≤ n ≤ 1000)(保证字符串至少包含一个0和一个1),现在你可以进行若干次询问(不超过15次),每次询问输出一个字符串,对应的输入给出了原字符串和你询问字符串的汉明距离(汉明距离:两个字符串对应位置的不同字符的个数),最后再输出原字符串任意一个0和一个1的位置。

题解:二分区间判断。利用两组询问的差值,看一个区间里有没有0或1。

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <iostream>
 5 using namespace std;
 6 const int N = 1005;
 7 char s[N], t[N];
 8 int main() {
 9     int n, i, d1, d2, l, r;
10     scanf("%d", &n);
11     for (i = 0; i < n; i++) s[i] = '0';
12     printf("? %s
", s); fflush(stdout);
13     int p0 = 0, p1 = 0;
14     l = 0, r = n - 1;
15     scanf("%d", &d1);
16     while (l <= r) {
17         int m = (l + r) >> 1;
18         strcpy(t, s);
19         for(i = l; i <= m; i++) t[i] = '1';
20         printf("? %s
", t); fflush(stdout);
21         scanf("%d", &d2);
22         if ((d2-d1) == (m-l+1)) {
23             p0 = m; l = m + 1; //printf("p0=%d
",p0);
24             continue;
25         }
26         if ((d1-d2) == (m-l+1)) {
27             p1 = m; l = m + 1; //printf("p1=%d
",p1);
28             continue;
29         }
30         r = m;
31     }
32     printf("! %d %d
", p0+1, p1+1); fflush(stdout);
33     return 0;
34 }
15ms
原文地址:https://www.cnblogs.com/GraceSkyer/p/7565570.html