[关闭]
@Dmaxiya 2018-08-17T02:27:35.000000Z 字数 7584 阅读 920

Codeforces Round #346 (Div. 2)

Codeforces


Contests 链接:Codeforces Round #346 (Div. 2)
过题数:5
排名:614/15230

A. Round House

题意

个围成圈的入口,入口 和入口 相邻, 最开始在入口 ,他将移动 步,如果 ,则 从数字小到大的方向移动,若 ,则他从数字从大到小的方向移动,问最终 的位置。

数据范围

题解

直接求 对应在 个入口的位置就是答案。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. int n, a, b;
  21. int main() {
  22. #ifdef LOCAL
  23. freopen("test.txt", "r", stdin);
  24. // freopen("test1.out", "w", stdout);
  25. #endif // LOCAL
  26. ios::sync_with_stdio(false);
  27. while(scanf("%d%d%d", &n, &a, &b) != EOF) {
  28. int ans = ((a + b) % n + n) % n;
  29. if(ans == 0) {
  30. printf("%d\n", n);
  31. } else {
  32. printf("%d\n", ans);
  33. }
  34. }
  35. return 0;
  36. }

B. Qualifying Contest

题意

个选手参加 个地区的比赛,每个地区将会对每个选手的分数进行排名,并选出每个地区的第一名和第二名,如果由于分数相同而无法确定某个地区的第 名,则输出问号,否则输出前两名选手的名字。

数据范围

题解

对每个地区开一个 按题意模拟排序,若比赛人数只有 人,则直接输出两人的名字,否则判断第二名和第三名的成绩是否相等,来判断能否决定出前两名。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. const int maxn = 10000 + 100;
  21. struct Node {
  22. string name;
  23. int score;
  24. };
  25. bool operator<(const Node &a, const Node &b) {
  26. return a.score > b.score;
  27. }
  28. int n, m, t;
  29. Node node;
  30. vector<Node> G[maxn];
  31. int main() {
  32. #ifdef LOCAL
  33. freopen("test.txt", "r", stdin);
  34. // freopen("test1.out", "w", stdout);
  35. #endif // LOCAL
  36. ios::sync_with_stdio(false);
  37. while(cin >> n >> m) {
  38. for(int i = 1; i <= m; ++i) {
  39. G[i].clear();
  40. }
  41. for(int i = 0; i < n; ++i) {
  42. cin >> node.name >> t >> node.score;
  43. G[t].push_back(node);
  44. }
  45. for(int i = 1; i <= m; ++i) {
  46. sort(G[i].begin(), G[i].end());
  47. }
  48. for(int i = 1; i <= m; ++i) {
  49. if(G[i].size() <= 2) {
  50. cout << G[i][0].name << " " << G[i][1].name << endl;
  51. continue;
  52. }
  53. if(G[i][1].score == G[i][2].score) {
  54. cout << "?" << endl;
  55. } else {
  56. cout << G[i][0].name << " " << G[i][1].name << endl;
  57. }
  58. }
  59. }
  60. return 0;
  61. }

C. Tanya and Toys

题意

最近 掀起了一股新的玩具收集热,总共有 种玩具,编号为 ,编号为 的玩具价值为 元, 已经有 种不同的玩具,玩具种类分别为 ,她还想要更多种类的玩具,她妈妈给了她 元,问她要收集到最多的不同的玩具种类,应该买哪些玩具,输出买玩具的方案。

数据范围

题解

贪心按照从价格最小的开始买。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. const int maxn = 100000 + 100;
  21. int n, m;
  22. int num;
  23. set<int> st;
  24. int ans[maxn];
  25. int main() {
  26. #ifdef LOCAL
  27. freopen("test.txt", "r", stdin);
  28. // freopen("test1.out", "w", stdout);
  29. #endif // LOCAL
  30. ios::sync_with_stdio(false);
  31. while(scanf("%d%d", &n, &m) != EOF) {
  32. st.clear();
  33. for(int i = 0; i < n; ++i) {
  34. scanf("%d", &num);
  35. st.insert(num);
  36. }
  37. int Index = 0;
  38. int cnt = 1;
  39. while(m >= cnt) {
  40. if(st.find(cnt) == st.end()) {
  41. m -= cnt;
  42. ans[Index++] = cnt;
  43. }
  44. ++cnt;
  45. }
  46. printf("%d\n", Index);
  47. for(int i = 0; i < Index; ++i) {
  48. if(i != 0) {
  49. printf(" ");
  50. }
  51. printf("%d", ans[i]);
  52. }
  53. printf("\n");
  54. }
  55. return 0;
  56. }

D. Bicycle Race

题意

参加了一个环湖自行车比赛,比赛路线上有 个转折点,每个转折点都用一个平面直角坐标系上的点 表示,这些点将从起点按照顺时针的顺序给出,两个转折点之间的路线都是直线, 是个新手,她每骑到一个转折点,如果在这个转折点直行会掉到湖里,她就有可能存在危险,问这个赛道上有多少个转折点对她来说可能是危险的。数据保证是一条合法的赛道。

数据范围

题解

用向量叉积判断下一条路线是是否是往左转的,如果是,则答案

过题代码

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

E. New Reform

题意

给一个 个节点 条边的无向图,无向图不保证连通,现在要将所有边都变成有向边,要使入度为 的点个数最少,输出最少的入度为 的点的个数。

数据范围

题解

画几组数据就可以发现,如果一个连通块内存在环,就可以将这个环的所有边变成顺时针旋转的有向图,将与这个环相连的所有边,都转化为从这个环上的点出发的边,这样这个连通块上的所有的点入度就都不为 了,而对于一条链的情况,有向边可以从链的一头指向另一头,这样这条链上入度为 的点就只有一个。所以这题要做的就是用并查集判断图上链的数量。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. const int maxn = 100000 + 100;
  21. int n, m, u, v, ans;
  22. int num[maxn], fa[maxn], sum[maxn];
  23. bool vis[maxn];
  24. void Init() {
  25. for(int i = 1; i <= n; ++i) {
  26. fa[i] = i;
  27. num[i] = sum[i] = 1;
  28. vis[i] = false;
  29. }
  30. }
  31. int Find(int x) {
  32. return (x == fa[x]? x: fa[x] = Find(fa[x]));
  33. }
  34. void unit(int x, int y) {
  35. int xx = Find(x);
  36. int yy = Find(y);
  37. if(xx != yy) {
  38. fa[xx] = yy;
  39. sum[yy] += sum[xx];
  40. }
  41. num[yy] += num[xx];
  42. }
  43. int main() {
  44. #ifdef LOCAL
  45. freopen("test.txt", "r", stdin);
  46. // freopen("test1.out", "w", stdout);
  47. #endif // LOCAL
  48. ios::sync_with_stdio(false);
  49. while(scanf("%d%d", &n, &m) != EOF) {
  50. ans = 0;
  51. Init();
  52. for(int i = 0; i < m; ++i) {
  53. scanf("%d%d", &u, &v);
  54. unit(u, v);
  55. }
  56. for(int i = 1; i <= n; ++i) {
  57. int f = Find(i);
  58. if(!vis[f]) {
  59. vis[f] = true;
  60. if(num[f] == sum[f]) {
  61. ++ans;
  62. }
  63. }
  64. }
  65. printf("%d\n", ans);
  66. }
  67. return 0;
  68. }

F. Polycarp and Hay

题意

在一个 的仓库里,每个位置上都有一个高度为 的草垛,如果 ,则说明那个位置没有草垛,现在 想要搬走一些草垛,使得所有的草垛满足以下条件:

  1. 所有草垛的高度和为
  2. 除了高度为 的草垛,其他草垛的高度都应该相等;
  3. 至少要有一个高度非零的草垛,保留原来的高度;
  4. 所有剩下的草垛都应该相互连通。

数据范围

题解

枚举所有非零的草垛高度 ,在满足 的条件下, 判所有高度大于等于 的草垛是否相互连通。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. const int maxn = 1000 + 100;
  21. struct Node {
  22. int x, y;
  23. LL num;
  24. Node() {}
  25. Node(int xx, int yy, LL n) {
  26. x = xx;
  27. y = yy;
  28. num = n;
  29. }
  30. };
  31. bool operator<(const Node &a, const Node &b) {
  32. return a.num < b.num;
  33. }
  34. int n, m, cnt, ansx, ansy;
  35. LL k;
  36. LL num[maxn][maxn];
  37. Node node[maxn * maxn];
  38. Node *it;
  39. bool vis[maxn][maxn];
  40. queue<Node> que;
  41. const int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
  42. bool in(int x, int y) {
  43. return x >= 0 && x < n && y >= 0 && y < m;
  44. }
  45. bool bfs(int x, int y, int cnt) {
  46. vis[x][y] = true;
  47. --cnt;
  48. if(cnt == 0) {
  49. return true;
  50. }
  51. while(!que.empty()) {
  52. que.pop();
  53. }
  54. que.push(Node(x, y, 0));
  55. while(!que.empty()) {
  56. Node tmp = que.front();
  57. que.pop();
  58. for(int i = 0; i < 4; ++i) {
  59. int xx = tmp.x + dir[i][0];
  60. int yy = tmp.y + dir[i][1];
  61. if(in(xx, yy) && !vis[xx][yy] && num[xx][yy] >= num[x][y]) {
  62. --cnt;
  63. vis[xx][yy] = true;
  64. que.push(Node(xx, yy, 0));
  65. }
  66. if(cnt == 0) {
  67. return true;
  68. }
  69. }
  70. }
  71. return false;
  72. }
  73. bool Check(int x, int y) {
  74. memset(vis, 0, sizeof(vis));
  75. int cnt = k / num[x][y];
  76. for(int i = 0; i < n; ++i) {
  77. for(int j = 0; j < m; ++j) {
  78. if(num[i][j] == num[x][y] && !vis[i][j]) {
  79. if(bfs(i, j, cnt)) {
  80. ansx = i;
  81. ansy = j;
  82. return true;
  83. }
  84. }
  85. }
  86. }
  87. return false;
  88. }
  89. int main() {
  90. #ifdef LOCAL
  91. freopen("test.txt", "r", stdin);
  92. // freopen("test1.out", "w", stdout);
  93. #endif // LOCAL
  94. ios::sync_with_stdio(false);
  95. while(scanf("%d%d%I64d", &n, &m, &k) != EOF) {
  96. cnt = 0;
  97. for(int i = 0; i < n; ++i) {
  98. for(int j = 0; j < m; ++j) {
  99. scanf("%I64d", &num[i][j]);
  100. node[cnt++] = Node(i, j, num[i][j]);
  101. }
  102. }
  103. bool flag = false;
  104. sort(node, node + cnt);
  105. it = node;
  106. while(it != node + cnt) {
  107. if(k % it->num == 0 && it->num * (cnt - (it - node)) >= k) {
  108. if(Check(it->x, it->y)) {
  109. flag = true;
  110. break;
  111. }
  112. }
  113. it = upper_bound(node, node + cnt, *it);
  114. }
  115. if(flag) {
  116. printf("YES\n");
  117. memset(vis, 0, sizeof(vis));
  118. bfs(ansx, ansy, k / num[ansx][ansy]);
  119. for(int i = 0; i < n; ++i) {
  120. for(int j = 0; j < m; ++j) {
  121. if(j != 0) {
  122. printf(" ");
  123. }
  124. if(vis[i][j]) {
  125. printf("%I64d", num[ansx][ansy]);
  126. } else {
  127. printf("0");
  128. }
  129. }
  130. printf("\n");
  131. }
  132. } else {
  133. printf("NO\n");
  134. }
  135. }
  136. return 0;
  137. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注