Codeforces Round #435 (Div. 2)

来源:互联网 发布:穿越火线哪里优化 编辑:程序博客网 时间:2024/06/05 17:08

A

#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <ctime>#include <iostream>#include <algorithm>#include <sstream>#include <string>#include <vector>#include <queue>#include <stack>#include <map>#include <set>#include <utility>#include <bitset>using namespace std;#define LL long long#define pb push_back#define mk make_pair#define pill pair<int, int>#define mst(a, b)memset(a, b, sizeof a)#define REP(i, x, n)for(int i = x; i <= n; ++i)const int MOD = 1e9 + 7;const int qq = 1e6 + 10;const int INF = 1e9 + 10;bool vis[105];int main(){int n, x;scanf("%d%d", &n, &x);for(int i = 1; i <= n; ++i) {int a;scanf("%d", &a);vis[a] = true;}int cnt = 0;if(vis[x])cnt++;for(int i = 0; i < x; ++i) {if(!vis[i])cnt++;}printf("%d\n", cnt);return 0;}

B

题意:n个结点n-1条边组成一棵树,现在要把结点分成一个二分图,问最多能加多少条边

思路:直接统计两部分的结点数,求出两部分结点的乘积减去n - 1条边即可

#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <ctime>#include <iostream>#include <algorithm>#include <sstream>#include <string>#include <vector>#include <queue>#include <stack>#include <map>#include <set>#include <utility>#include <bitset>using namespace std;#define LL long long#define pb push_back#define mk make_pair#define pill pair<int, int>#define mst(a, b)memset(a, b, sizeof a)#define REP(i, x, n)for(int i = x; i <= n; ++i)const int MOD = 1e9 + 7;const int qq = 1e6 + 10;const int INF = 1e9 + 10;vector<int> G[qq];LL num[2];bool vis[qq];void Dfs(int u, int idx) {num[idx]++;vis[u] = true;for(int i = 0; i < G[u].size(); ++i) {int v = G[u][i];if(vis[v])continue;Dfs(v, idx ^ 1);}}int main(){int n;scanf("%d", &n);for(int i = 1; i < n; ++i) {int u, v;scanf("%d%d", &u, &v);G[u].pb(v), G[v].pb(u);}Dfs(1, 0);printf("%lld\n", num[0] * num[1] - (n - 1));return 0;}


C

题意:给出n,x, 要求输出n个不同的非负数xor的结果是x,如果不存在输出NO,最大数不超过1e6

思路:参考了题解的思路

#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <ctime>#include <iostream>#include <algorithm>#include <sstream>#include <string>#include <vector>#include <queue>#include <stack>#include <map>#include <set>#include <utility>#include <bitset>using namespace std;#define LL long long#define pb push_back#define mk make_pair#define pill pair<int, int>#define mst(a, b)memset(a, b, sizeof a)#define REP(i, x, n)for(int i = x; i <= n; ++i)const int MOD = 1e9 + 7;const int qq = 2e5 + 10;const int INF = 1e9 + 10;int main(){int n = 100000;int maxn = 0;int cnt = 0;while(maxn > 0) {if(n & 1) maxn = max(maxn, cnt);n >>= 1;cnt++;}int a = (1 << maxn);int b = (1 << (maxn + 1));int x;scanf("%d%d", &n, &x);if(n == 1) {printf("YES\n%d\n", x);} else if(n == 2) {if(x == 0)puts("NO");elseprintf("YES\n0 %d\n", x);} else {puts("YES");int ans = 0;for(int i = 1; i <= n - 3; ++i) {ans ^= i;printf("%d ", i);}if(ans == x) {printf("%d %d %d\n", a, b, a + b);} else {printf("%d %d %d\n", a, a ^ ans ^ x, 0);}}return 0;}

D

题意:交互题,答案是一个长度为n的字符串,你可以输出一个字符串,机器会返回给你和答案串的Hamming distance,让你再最多询问15次的前提下求出某个1和0的位置,保证答案串中至少一个1和一个0

思路:首先通过两次询问可以确定答案串的第一个字符是1还是0,假设答案串的前m个字符都是0或者1,如果都是那么我们要找的答案大于m,如果不是,那么可以确定m以内有我们要找的答案

#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <ctime>#include <iostream>#include <algorithm>#include <sstream>#include <string>#include <vector>#include <queue>#include <stack>#include <map>#include <set>#include <utility>#include <bitset>using namespace std;#define LL long long#define pb push_back#define mk make_pair#define pill pair<int, int>#define mst(a, b)memset(a, b, sizeof a)#define REP(i, x, n)for(int i = x; i <= n; ++i)const int MOD = 1e9 + 7;const int qq = 1e6 + 10;const int INF = 1e9 + 10;int Query(int n, int t) {string str(n, '0');for(int i = 0; i < t; ++i) {str[i] = '1';}printf("? %s\n", str.c_str());fflush(stdout);int ans;scanf("%d", &ans);return ans;}int res[2];int main(){int n;scanf("%d", &n);int a = Query(n, 0), b = Query(n, 1);int tmp;if(a > b) {// find 0res[1] = 1;int l = 2, r = n;while(l <= r) {int m = (l + r) >> 1;if(-Query(n, m) + a == m) {l = m + 1;} else {tmp = m;r = m - 1;}}res[0] = tmp;} else {//find 1res[0] = 1;int l = 2, r = n;while(l <= r) {int m = (l + r) >> 1;if(-a + Query(n, m) == m) {l = m + 1;} else {tmp = m;r = m - 1;}//printf("%d %d %d\n", l, r, tmp);}res[1] = tmp;}printf("! %d %d\n", res[0], res[1]);return 0;}


原创粉丝点击