[关闭]
@Dmaxiya 2018-08-17T02:20:46.000000Z 字数 3485 阅读 920

Educational Codeforces Round 33

Codeforces


Contests 链接:Educational Codeforces Round 33
过题数:3
排名:813/9628

A. Chess For Three

题意

三个人轮流玩一个游戏,这个游戏没有平局,三个人玩游戏的规则如下:

  1. 先进行游戏, 在旁边旁观;
  2. 上一局游戏失败的人退出游戏,旁观下一局的比赛,上一局旁观的人继续与获胜的人进行游戏。
    现在他们进行了 轮游戏,并记录下了每一局获胜的人,现在要求检查这个记录是否是可能出现的情况(例如上一局失败的人不可能在下一局继续进行游戏)。

输入

第一行为一个正整数 ,接下来 行每行为一个整数 ,分别对应 ,表示第 局获胜的人。

输出

如果这种结果是可能出现的,输出 ,否则输出

样例

输入 输出 提示
3
1
1
2
YES 可能的情况为:
1. 获胜, 替换 进行游戏;
2. 获胜, 继续进行游戏;
3. 获胜。
2
1
2
NO 由于 在第一局游戏中失败了,所以他不可能是第二局游戏的获胜者。

题解

按照题意模拟,如果出现旁观者获胜的情形,就是非法的情况,否则总是合法的。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cfloat>
  7. #include <cstring>
  8. #include <string>
  9. #include <vector>
  10. #include <list>
  11. #include <queue>
  12. #include <stack>
  13. #include <map>
  14. #include <set>
  15. #include <algorithm>
  16. using namespace std;
  17. #define LL long long
  18. int n, x;
  19. int num[3];
  20. int main() {
  21. #ifdef Dmaxiya
  22. freopen("test.txt", "r", stdin);
  23. #endif // Dmaxiya
  24. ios::sync_with_stdio(false);
  25. while(scanf("%d", &n) != EOF) {
  26. bool flag = true;
  27. for(int i = 0; i < 3; ++i) {
  28. num[i] = i + 1;
  29. }
  30. for(int i = 0; i < n; ++i) {
  31. scanf("%d", &x);
  32. if(x == num[0]) {
  33. swap(num[1], num[2]);
  34. } else if(x == num[1]) {
  35. swap(num[0], num[2]);
  36. } else {
  37. flag = false;
  38. }
  39. }
  40. if(flag) {
  41. printf("YES\n");
  42. } else {
  43. printf("NO\n");
  44. }
  45. }
  46. return 0;
  47. }

B. Beautiful Divisors

题意

如果一个数字的二进制表示是由 位连续的 位连续的 组成,那么这个数字就是一个漂亮的数,换一句话说,如果一个数字能够表示成某个整数 ,那么它就是漂亮的数。现在给定一个整数 ,询问最大的能整除 的漂亮的数。

输入

输入只包含一个整数

输出

输出最大的能整数 的漂亮的数。

样例

输入 输出
3 1
992 496

题解

枚举每一个数字暴力判断找到第一个满足条件的数字即可。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cfloat>
  7. #include <cstring>
  8. #include <string>
  9. #include <vector>
  10. #include <list>
  11. #include <queue>
  12. #include <stack>
  13. #include <map>
  14. #include <set>
  15. #include <algorithm>
  16. using namespace std;
  17. #define LL long long
  18. int n;
  19. int lowbit(int x) {
  20. return x & (-x);
  21. }
  22. bool judge(LL x) {
  23. for(int i = 1; i < 30; ++i) {
  24. if(x == ((1LL << i) - 1) * (1LL << (i - 1))) {
  25. return true;
  26. }
  27. }
  28. return false;
  29. }
  30. int main() {
  31. #ifdef Dmaxiya
  32. freopen("test.txt", "r", stdin);
  33. #endif // Dmaxiya
  34. ios::sync_with_stdio(false);
  35. while(scanf("%d", &n) != EOF) {
  36. int ans;
  37. for(int i = n; i > 0; --i) {
  38. if(judge(i) && n % i == 0) {
  39. ans = i;
  40. break;
  41. }
  42. }
  43. printf("%d\n", ans);
  44. }
  45. return 0;
  46. }

C. Rumor

题意

在玩一款游戏,这款游戏的目标是将谣言散播到所有的游戏人物,总共有 个游戏人物, 每将谣言告诉给一个人物并要他把谣言告诉他的朋友,就需要 的代价, 个人物中有 对朋友,如果 是朋友,那么 就会把谣言告诉 并且在告诉的过程中是免费的。问 要完成游戏任务所需要的最小的代价。

输入

第一行为两个整数 ,第二行为 个整数 ,接下去 行每行两个整数 ,表示第 个人和第 个人是朋友。

输出

输出最小代价。

样例

输入 输出 提示
5 2
2 5 3 4 8
1 4
4 5
10 最优的选择是将谣言传给第 个人,这样第 个人就会把谣言传给第 个人,第 个人会继续传给第 个人,当然他也需要贿赂第 和第 个人。
10 0
1 2 3 4 5 6 7 8 9 10
55 需要贿赂所有人。
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10
15 最优的选择是贿赂第 个人。

题解

能够互相散播谣言的组成了一个连通块,只要选择每个连通块中代价最小的一个,就可以以这个代价将谣言散播到这个连通块,连通块的最小值用并查集进行维护。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cfloat>
  7. #include <cstring>
  8. #include <string>
  9. #include <vector>
  10. #include <list>
  11. #include <queue>
  12. #include <stack>
  13. #include <map>
  14. #include <set>
  15. #include <algorithm>
  16. using namespace std;
  17. #define LL long long
  18. const int maxn = 100000 + 100;
  19. int n, m, u, v;
  20. LL ans;
  21. int fa[maxn];
  22. LL Min[maxn];
  23. bool vis[maxn];
  24. void Init() {
  25. for(int i = 1; i <= n; ++i) {
  26. fa[i] = i;
  27. vis[i] = false;
  28. }
  29. }
  30. int Find(int x) {
  31. return (x == fa[x]? x: fa[x] = Find(fa[x]));
  32. }
  33. void unit(int x, int y) {
  34. int xx = Find(x);
  35. int yy = Find(y);
  36. fa[xx] = yy;
  37. Min[yy] = min(Min[xx], Min[yy]);
  38. }
  39. int main() {
  40. #ifdef Dmaxiya
  41. freopen("test.txt", "r", stdin);
  42. #endif // Dmaxiya
  43. ios::sync_with_stdio(false);
  44. while(scanf("%d%d", &n, &m) != EOF) {
  45. ans = 0;
  46. Init();
  47. for(int i = 1; i <= n; ++i) {
  48. scanf("%I64d", &Min[i]);
  49. }
  50. for(int i = 0; i < m; ++i) {
  51. scanf("%d%d", &u, &v);
  52. unit(u, v);
  53. }
  54. for(int i = 1; i <= n; ++i) {
  55. int f = Find(i);
  56. if(!vis[f]) {
  57. vis[f] = true;
  58. ans += Min[f];
  59. }
  60. }
  61. printf("%I64d\n", ans);
  62. }
  63. return 0;
  64. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注