[关闭]
@Dmaxiya 2018-08-17T10:18:52.000000Z 字数 5331 阅读 1060

Educational Codeforces Round 37

Codeforces


Contests 链接:Educational Codeforces Round 37
过题数:3
排名:763/9623

A. Water The Garden

题意

个花圃排成一排,从 编号,有 个喷头放在这 个花圃中的 处,这个喷头开启后第 秒会将 处的花圃淋湿,第 秒会将区间 内的所有花圃淋湿,且只有整数秒时才会淋湿一个花圃,问所有喷头同时开启,最少多少秒可以将所有花圃淋湿。

输入

第一行为一个整数 ,接下来有 组数据,每组数据的第一行为两个整数 ,第二行有 个整数 ,表示第 个喷头的位置为

输出

输出最少让所有花圃淋湿的时间。

样例

输入 输出 提示
3
5 1
3
3 3
1 2 3
4 1
1
3
1
4
1.在第一组数据中,第 秒的时候第 个花圃会被淋湿,第 秒的时候区间 内的花圃会
被淋湿,第 秒的时候所有的花圃都会被淋湿;
2.在第二组数据中,每一个花圃上都有一个喷头,因此在第 秒的时候所有的花圃就都会被淋湿;
3.在第三组数据中,只有第 个花圃有喷头,所以需要 秒时间将所有花圃淋湿。

题解

计算所有相邻喷头中间位置的花圃被淋到的时间,以及边缘的花圃会被淋到的时间,取最大值。

过题代码

  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 = 200 + 100;
  19. int T, n, k, ans;
  20. int num[maxn];
  21. int main() {
  22. #ifdef Dmaxiya
  23. freopen("test.txt", "r", stdin);
  24. #endif // Dmaxiya
  25. ios::sync_with_stdio(false);
  26. scanf("%d", &T);
  27. while(T--) {
  28. scanf("%d%d", &n, &k);
  29. ans = 0;
  30. for(int i = 1; i <= k; ++i) {
  31. scanf("%d", &num[i]);
  32. }
  33. for(int i = 2; i <= k; ++i) {
  34. ans = max(ans, (num[i] - num[i - 1]) / 2 + 1);
  35. }
  36. ans = max(ans, num[1]);
  37. ans = max(ans, n - num[k] + 1);
  38. printf("%d\n", ans);
  39. }
  40. return 0;
  41. }

B. Tea Queue

题意

个学生,第 个学生将会在第 秒来等待接茶水,每个人接茶水消耗的时间为 ,如果两个学生同时到达,则编号小的同学排在队伍的前面,如果某个同学到达时前面有同学在等待,他也会排队等待,如果超过 秒仍然没有轮到他接茶水,他就会马上离开,求每个同学接到茶水的时间。

输入

第一行为一个整数 ,接下去有 组数据,每组数据第一行为一个整数 ,接下去有 行,每一行有两个整数 ,对于所有的 ,都有 。且所有数据的 的和不超过

输出

每一组数据输出一行 个整数,第 个整数代表第 个学生接到茶水的时间,如果某个学生没有接到茶水直接离开,则输出

样例

输入 输出 提示
2
2
1 3
1 4
3
1 5
1 1
2 3
1 2
1 0 2
1.对于第 组数据, 个学生同时到达,第 个学生在第 秒接到茶水,
个学生在第 秒接到茶水。
2.对于第 组数据,第 个和第 个学生同时到达,第 个学生在第
秒接到茶水,第 秒结束第二个学生没有接到茶水就离开了,第 秒时
个学生到达,并接到茶水。

题解

按照题意模拟。

过题代码

  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 = 1000 + 100;
  19. int T, n, l, r;
  20. int main() {
  21. #ifdef Dmaxiya
  22. freopen("test.txt", "r", stdin);
  23. #endif // Dmaxiya
  24. ios::sync_with_stdio(false);
  25. scanf("%d", &T);
  26. while(T--) {
  27. int now = 0;
  28. scanf("%d", &n);
  29. for(int i = 0; i < n; ++i) {
  30. scanf("%d%d", &l, &r);
  31. now = max(now, l);
  32. if(i != 0) {
  33. printf(" ");
  34. }
  35. if(now > r) {
  36. printf("0");
  37. } else {
  38. printf("%d", now);
  39. ++now;
  40. }
  41. }
  42. printf("\n");
  43. }
  44. return 0;
  45. }

C. Swap Adjacent Elements

题意

给定一个长度为 的序列,在这个序列中的某些位置 可以和第 个位置的数字进行交换,问能否通过任意次合法的交换,使得这个数列成为一个递增数列。

输入

第一行为一个整数 ,第二行为 个整数 ,第三行为一个字符串 ,字符串的第 位为 表示序列中的第 个数字可以和第 个数字交换位置。

输出

如果可以在给定的条件下,通过任意次交换得到一个递增的数列,则输出 ,否则输出

样例

输入 输出 提示
6
1 2 5 3 4 6
01110
YES 可以通过交换 ,使数列成为一个递增的数列。
6
1 2 5 3 4 6
01010
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. const int maxn = 200000 + 100;
  19. int n;
  20. int num[maxn], Index1[maxn], Index2[maxn];
  21. int fa[maxn];
  22. char str[maxn];
  23. int Find(int x) {
  24. return (x == fa[x]? x: fa[x] = Find(fa[x]));
  25. }
  26. void unit(int x, int y) {
  27. int xx = Find(x);
  28. int yy = Find(y);
  29. fa[xx] = yy;
  30. }
  31. int main() {
  32. #ifdef Dmaxiya
  33. freopen("test.txt", "r", stdin);
  34. #endif // Dmaxiya
  35. ios::sync_with_stdio(false);
  36. while(scanf("%d", &n) != EOF) {
  37. for(int i = 1; i <= n; ++i) {
  38. scanf("%d", &num[i]);
  39. fa[i] = i;
  40. Index1[num[i]] = i;
  41. }
  42. sort(num + 1, num + 1 + n);
  43. for(int i = 1; i <= n; ++i) {
  44. Index2[num[i]] = i;
  45. }
  46. scanf("%s", str + 1);
  47. for(int i = 1; i < n; ++i) {
  48. if(str[i] == '1') {
  49. unit(i, i + 1);
  50. }
  51. }
  52. bool flag = true;
  53. for(int i = 1; i <= n; ++i) {
  54. if(Find(Index1[num[i]]) != Find(Index2[num[i]])) {
  55. flag = false;
  56. break;
  57. }
  58. }
  59. if(flag) {
  60. printf("YES\n");
  61. } else {
  62. printf("NO\n");
  63. }
  64. }
  65. return 0;
  66. }

D. Connected Components?

题意

一个 个节点的无向图的补图上有 条边,输出原图上所有连通块中节点的数量。

输入

第一行为两个整数 ,接下去 行每行两个整数 ,表示原图上 之间没有连边。

输出

第一行输出一个整数 ,表示连通块的个数,第二行按照非递减的顺序输出每个连通块内的节点数量。

样例

输入 输出
5 5
1 2
3 4
3 2
4 2
2 5
2
1 4

题解

用一个 来存所有没有被访问过的节点,每访问一个节点就从 中删去,直到所有节点都被访问( 为空),就可以得到所有连通块的信息,用 判连通块。

过题代码

  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 = 200000 + 100;
  19. int n, m, u, v, cnt;
  20. set<int> st;
  21. set<int> G[maxn];
  22. int ans[maxn];
  23. queue<int> que;
  24. set<int>::iterator it;
  25. vector<set<int>::iterator> vct;
  26. int bfs(int x) {
  27. int ret = 1;
  28. while(!que.empty()) {
  29. que.pop();
  30. }
  31. que.push(x);
  32. st.erase(x);
  33. while(!que.empty()) {
  34. if(st.empty()) {
  35. return ret;
  36. }
  37. vct.clear();
  38. int tmp = que.front();
  39. que.pop();
  40. for(it = st.begin(); it != st.end(); ++it) {
  41. if(G[tmp].find(*it) == G[tmp].end()) {
  42. que.push(*it);
  43. vct.push_back(it);
  44. ++ret;
  45. }
  46. }
  47. int len = vct.size();
  48. for(int i = 0; i < len; ++i) {
  49. st.erase(vct[i]);
  50. }
  51. }
  52. return ret;
  53. }
  54. int main() {
  55. #ifdef Dmaxiya
  56. freopen("test.txt", "r", stdin);
  57. #endif // Dmaxiya
  58. ios::sync_with_stdio(false);
  59. while(scanf("%d%d", &n, &m) != EOF) {
  60. cnt = 0;
  61. st.clear();
  62. for(int i = 1; i <= n; ++i) {
  63. G[i].clear();
  64. st.insert(i);
  65. }
  66. for(int i = 0; i < m; ++i) {
  67. scanf("%d%d", &u, &v);
  68. G[u].insert(v);
  69. G[v].insert(u);
  70. }
  71. while(!st.empty()) {
  72. ans[cnt++] = bfs(*(st.begin()));
  73. }
  74. sort(ans, ans + cnt);
  75. printf("%d\n", cnt);
  76. for(int i = 0; i < cnt; ++i) {
  77. if(i != 0) {
  78. printf(" ");
  79. }
  80. printf("%d", ans[i]);
  81. }
  82. printf("\n");
  83. }
  84. return 0;
  85. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注