[关闭]
@Dmaxiya 2018-08-17T10:25:50.000000Z 字数 6881 阅读 910

Codeforces Round #416 (Div. 2)

Codeforces


Contests 链接:Codeforces Round #416 (Div. 2)
过题数:2
排名:1199/10439

A. Vladik and Courtesy

题意

分别有 个糖果, 先给 个糖果, 再给 个糖果 ,以此类推,直到 某一个人无法给出正确数量的糖果为止,他们都不会给出自己所收到的糖果。问谁会是第一个无法给出正确数量的糖果。

输入

输入包含两个整数

输出

输出第一个无法给出正确数量糖果的人。

样例

输入 输出 提示
1 1 Valera 每次糖果数量变化如图:
7 6 Vladik 每次糖果数量变化如图:

题解

由于 ,所以给出的糖果数量的和是平方增长的,所以可以直接按题意模拟,最多只要模拟到 次左右就可以得到结果。

过题代码

  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 a[10];
  21. int main() {
  22. #ifdef LOCAL
  23. freopen("test.txt", "r", stdin);
  24. // freopen("out.txt", "w", stdout);
  25. #endif // LOCAL
  26. ios::sync_with_stdio(false);
  27. while(scanf("%d%d", &a[0], &a[1]) != EOF) {
  28. int who = 0;
  29. for(int i = 1; i < 100000; ++i) {
  30. if(a[who] < i) {
  31. break;
  32. } else {
  33. a[who] -= i;
  34. who = 1 - who;
  35. }
  36. }
  37. if(who == 0) {
  38. printf("Vladik\n");
  39. } else {
  40. printf("Valera\n");
  41. }
  42. }
  43. return 0;
  44. }

B. Vladik and Complicated Book

题意

给定一个 的排列,有 次操作,每次操作将区间 上的数字进行从小到大排序,问第 个数字排序前后是否在原位,每次操作结束后都将序列还原成初始状态。

输入

第一行有两个整数 ,第二行有 个整数,为 的排列,接下去 行,每行三个整数 ,表示对区间 上的数字进行排序后,询问第 个数字的位置是否在原位。

输出

对于每次询问,如果第 个数字的位置在原位,则输出 ,否则输出

样例

输入 输出 提示
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
Yes
No
Yes
Yes
No
对于每次排序的结果:
1 2 3 4 5(第 个数字在原位)
3 4 5 2 1(第 个数字不在原位)
5 2 3 4 1(第 个数字在原位)
5 4 3 2 1(第 个数字在原位)
5 1 2 3 4(第 个数字不在原位)
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
Yes
No
Yes
No
Yes

题解

对于每次询问,判断第 个数字是否恰好比区间 内的 个数字大,如果是,说明排序前后 的位置不变,否则输出

过题代码

  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. int n, m, l, r, x;
  22. int num[maxn];
  23. int main() {
  24. #ifdef LOCAL
  25. freopen("test.txt", "r", stdin);
  26. // freopen("out.txt", "w", stdout);
  27. #endif // LOCAL
  28. ios::sync_with_stdio(false);
  29. while(scanf("%d%d", &n, &m) != EOF) {
  30. for(int i = 1; i <= n; ++i) {
  31. scanf("%d", &num[i]);
  32. }
  33. for(int i = 0; i < m; ++i) {
  34. scanf("%d%d%d", &l, &r, &x);
  35. int cnt = 0;
  36. for(int j = l; j <= r; ++j) {
  37. if(num[j] < num[x]) {
  38. ++cnt;
  39. }
  40. }
  41. if(l + cnt == x) {
  42. printf("Yes\n");
  43. } else {
  44. printf("No\n");
  45. }
  46. }
  47. }
  48. return 0;
  49. }

C. Vladik and Memorable Trip

题意

个人在火车站排队,每个人都有自己的目的地 ,列车长想要将这些乘客分到不同的车厢,他必须选择队列中连续的一段,让这些人进入到同一个车厢,如果想去城市 的人进入了一个车厢,那么队列中所有想去城市 的人都必须和那个人在同一个车厢内。同一个车厢内乘客的舒适度等于车厢内所有不同目的地的异或值,整列火车的舒适度等于每一节车厢的舒适度的和。
现在列车长想要找到一种能使整列火车舒适度最高的分配车厢的方案,注意他不必让所有人都上车。

输入

第一行为一个整数 ,第二行有 个整数 ,表示 位乘客想去的目的地编号,以及排队的顺序(从 )。

输出

输出最大的舒适度。

样例

输入 输出 提示
6
4 4 2 5 2 3
14 最优的选择为 ,舒适度为
9
5 1 3 1 5 2 4 2 5
9 最优的选择为 ,舒适度为

题解

首先 处理出所有合法的区间,对于每个终点 ,找到对应合法区间的起点 以及区间异或值 ,如果终点 不是一个合法区间的终点,则将 设为 ,接着从前往后 ,设 表示以 为结尾的序列的最大的舒适度,于是有下面的递推式:

过题代码

  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. typedef long long LL;
  20. const int maxn = 5000 + 100;
  21. int n;
  22. int num[maxn];
  23. int Begin[maxn], cnt[maxn], cnttmp[maxn], Xor[maxn], dp[maxn];
  24. bool vis[maxn];
  25. void Init(int Index) {
  26. int ccnt = 0;
  27. Xor[Index] = 0;
  28. memset(vis, 0, sizeof(vis));
  29. memcpy(cnttmp, cnt, sizeof(cnt));
  30. for(int i = Index; i > 0; --i) {
  31. if(!vis[num[i]]) {
  32. Xor[Index] ^= num[i];
  33. vis[num[i]] = true;
  34. ++ccnt;
  35. }
  36. --cnttmp[num[i]];
  37. if(cnttmp[num[i]] == 0) {
  38. --ccnt;
  39. }
  40. if(ccnt == 0) {
  41. Begin[Index] = i;
  42. return ;
  43. }
  44. }
  45. }
  46. int main() {
  47. #ifdef Dmaxiya
  48. freopen("test.txt", "r", stdin);
  49. // freopen("test1.out", "w", stdout);
  50. #endif // Dmaxiya
  51. ios::sync_with_stdio(false);
  52. while(scanf("%d", &n) != EOF) {
  53. memset(cnt, 0, sizeof(cnt));
  54. memset(Begin, 0, sizeof(Begin));
  55. for(int i = 1; i <= n; ++i) {
  56. scanf("%d", &num[i]);
  57. ++cnt[num[i]];
  58. }
  59. for(int i = 1; i <= n; ++i) {
  60. Init(i);
  61. }
  62. for(int i = 1; i <= n; ++i) {
  63. if(Begin[i] != 0) {
  64. dp[i] = max(dp[i - 1], dp[Begin[i] - 1] + Xor[i]);
  65. } else {
  66. dp[i] = dp[i - 1];
  67. }
  68. }
  69. printf("%d\n", dp[n]);
  70. }
  71. return 0;
  72. }

D. Vladik and Favorite Game

题意

很喜欢玩一个游戏,这个游戏是在一个 的迷宫里从初始位置 走到终点,迷宫用一字符表示,所有字符只包含:

  1. '.':一般的位置,玩家可以站在这里,数据保证 '.'
  2. 'F':终点,数据保证只有一个终点,且一定存在一条从 'F' 的路线;
  3. '*':危险的位置,如果玩家走到这里,则游戏失败。

现在玩家可以按下 'U','D','L','R' 四个按钮,每按下一个按钮,都将会返回一个坐标,如果走向的方向是边界,则玩家的坐标不会改变,如果走向的方向是安全的,则会返回这个坐标,如果走向的方向是一个 '*',则会返回 ,游戏立即结束。
有趣的是,在这个游戏中,上下和左右两个相对的方向可能会被颠倒,如果玩家输入 'U' 可能会返回一个往下走的坐标,现在玩家要在 步内到达终点,请输出玩家的移动步骤。

输入

初始输入第一行包含两个整数 ,接下去 行每行 个字符,字符只包含 '.''*''F'。后续输出将根据玩家的输入决定。

输出

每次输出 'U','D','L','R' 四个字符中的一个占一行,表示要按下的按钮,每输出一行应及时清空输出缓冲区。

样例

输入 输出
4 3
...
**.
F*.
...

1 1

1 2

1 3

1 3

2 3

3 3

4 3

4 2

4 1

3 1





R

L

L

D

U

U

U

R

R

D

题解

先对给出的图进行一遍 找到最短路径,然后在第一个安全的地方( 以及路径中的第一个转折点)确定上下、左右是否被颠倒,第一次确认之后,就不必再进行确认,直接交换上下、左右的两个字符即可,后面的直接按照最短路径输出。

过题代码

  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. typedef long long LL;
  20. const int maxn = 200;
  21. struct Node {
  22. int x, y;
  23. Node() {}
  24. Node(int xx, int yy) {
  25. x = xx;
  26. y = yy;
  27. }
  28. };
  29. bool operator<(const Node &a, const Node &b) {
  30. return (a.x == b.x? a.y < b.y: a.x < b.x);
  31. }
  32. bool operator==(const Node &a, const Node &b) {
  33. return a.x == b.x && a.y == b.y;
  34. }
  35. Node operator+(const Node &a, const Node &b) {
  36. return Node(a.x + b.x, a.y + b.y);
  37. }
  38. Node operator-(const Node &a, const Node &b) {
  39. return Node(a.x - b.x, a.y - b.y);
  40. }
  41. Node s, f;
  42. bool flag[2];
  43. int n, m, top;
  44. queue<Node> que;
  45. map<Node, int> mp;
  46. int ans[maxn * maxn];
  47. char str[maxn][maxn];
  48. bool vis[maxn][maxn];
  49. Node pos[maxn * maxn];
  50. char dstr[5] = "UDLR";
  51. Node pre[maxn][maxn], sta[maxn * maxn];
  52. Node dir[4] = {Node(-1, 0), Node(1, 0), Node(0, -1), Node(0, 1)};
  53. bool in(const Node &node) {
  54. return node.x > 0 && node.x <= n && node.y > 0 && node.y <= m;
  55. }
  56. void bfs() {
  57. memset(vis, 0, sizeof(vis));
  58. que.push(s);
  59. vis[1][1] = true;
  60. while(!que.empty()) {
  61. Node tmp = que.front();
  62. que.pop();
  63. if(tmp == f) {
  64. return ;
  65. }
  66. for(int i = 0; i < 4; ++i) {
  67. Node ntmp = tmp + dir[i];
  68. if(in(ntmp) && !vis[ntmp.x][ntmp.y] && str[ntmp.x][ntmp.y] != '*') {
  69. que.push(ntmp);
  70. vis[ntmp.x][ntmp.y] = true;
  71. pre[ntmp.x][ntmp.y] = tmp;
  72. }
  73. }
  74. }
  75. }
  76. int main() {
  77. #ifdef Dmaxiya
  78. // freopen("test.txt", "r", stdin);
  79. // freopen("test1.out", "w", stdout);
  80. #endif // Dmaxiya
  81. ios::sync_with_stdio(false);
  82. s = Node(1, 1);
  83. for(int i = 0; i < 4; ++i) {
  84. mp[dir[i]] = i;
  85. }
  86. scanf("%d%d", &n, &m);
  87. top = 0;
  88. memset(flag, 0, sizeof(flag));
  89. for(int i = 1; i <= n; ++i) {
  90. scanf("%s", str[i] + 1);
  91. for(int j = 1; j <= m; ++j) {
  92. if(str[i][j] == 'F') {
  93. f = Node(i, j);
  94. }
  95. }
  96. }
  97. bfs();
  98. while(!(f == s)) {
  99. Node ntmp = f;
  100. f = pre[ntmp.x][ntmp.y];
  101. sta[top++] = ntmp - f;
  102. }
  103. for(int i = top - 1; i >= 0; --i) {
  104. ans[i] = mp[sta[i]];
  105. }
  106. pos[top] = s;
  107. for(int i = top - 1; i >= 0; --i) {
  108. printf("%c\n", dstr[ans[i]]);
  109. fflush(stdout);
  110. scanf("%d %d", &pos[i].x, &pos[i].y);
  111. if(!flag[ans[i] >> 1]) {
  112. flag[ans[i] >> 1] = true;
  113. if(pos[i] == pos[i + 1]) {
  114. swap(dstr[ans[i]], dstr[ans[i] ^ 1]);
  115. printf("%c\n", dstr[ans[i]]);
  116. fflush(stdout);
  117. scanf("%d %d", &pos[i].x, &pos[i].y);
  118. }
  119. }
  120. }
  121. return 0;
  122. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注