[关闭]
@Dmaxiya 2018-08-17T10:15:53.000000Z 字数 10607 阅读 965

Codeforces Round #502 (Div. 1 + Div. 2)

Codeforces


Contests 链接:Codeforces Round #502 (Div. 1 + Div. 2)
过题数:4
排名:765/7399

A. The Rank

题意

一个班上有 个学生,每个学生都有一个 和四门课的成绩,每个学生的 之间的整数,每个学生的 都不相同,将所有学生按总分从高到低排序,若总分相同则按 从小到大排序,问 的学生最终排在第几名。

输入

第一行为一个整数 ,接下去 行每行 个整数 ,第 行的 个整数表示 号为 的学生四门课的成绩。

输出

输出最终 的学生的排名。

样例

输入
5
100 98 100 100
100 100 100 100
100 100 99 99
90 99 90 100
100 98 60 99
输出
2
提示
个学生的总分分别为 的学生的分数和 的分数相等,但是他的 值更小,所以他排在第 名。
输入
6
100 80 90 99
60 60 60 60
90 60 100 60
60 100 60 80
100 100 0 100
0 0 0 0
输出
1
提示
个学生的总分分别为 的学生分数最高,所以他是第 名。

题解

按照总分为主关键字, 为次关键字排序,然后 扫一遍找到 的学生的排名。

过题代码

  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 sum, id;
  23. };
  24. bool operator<(const Node &a, const Node &b) {
  25. return a.sum == b.sum? a.id < b.id: a.sum > b.sum;
  26. }
  27. int n, num;
  28. Node node[maxn];
  29. int main() {
  30. #ifdef LOCAL
  31. freopen("test.txt", "r", stdin);
  32. // freopen("test1.out", "w", stdout);
  33. #endif // LOCAL
  34. ios::sync_with_stdio(false);
  35. while(scanf("%d", &n) != EOF) {
  36. for(int i = 1; i <= n; ++i) {
  37. node[i].sum = 0;
  38. node[i].id = i;
  39. for(int j = 0; j < 4; ++j) {
  40. scanf("%d", &num);
  41. node[i].sum += num;
  42. }
  43. }
  44. sort(node + 1, node + 1 + n);
  45. int ans = 0;
  46. for(int i = 1; i <= n; ++i) {
  47. if(node[i].id == 1) {
  48. ans = i;
  49. break;
  50. }
  51. }
  52. printf("%d\n", ans);
  53. }
  54. return 0;
  55. }

B. The Bits

题意

给定由 位组成的二进制数 ,问对 的所有 位有多少种交换不同位的方式,使得 (按位或)的值发生改变。

输入

第一行为一个整数 ,第 行为 字符,表示 的所有二进制位,第 行为 字符,表示 的二进制位。

输出

输出方案数。

样例

输入
5
01011
11001
输出
4
提示
中可以交换的 位为:
输入
6
011000
010011
输出
6
提示
中可以交换的 位为:

题解

枚举所有 位的对应情况:

  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 = 100000 + 100;
  21. int n;
  22. int sum[maxn][4];
  23. char a[maxn], b[maxn];
  24. int id(int i) {
  25. return (a[i] - '0') * 2 + b[i] - '0';
  26. }
  27. int main() {
  28. #ifdef LOCAL
  29. freopen("test.txt", "r", stdin);
  30. // freopen("test1.out", "w", stdout);
  31. #endif // LOCAL
  32. ios::sync_with_stdio(false);
  33. while(scanf("%d", &n) != EOF) {
  34. memset(sum, 0, sizeof(sum));
  35. scanf("%s%s", a + 1, b + 1);
  36. for(int i = n; i > 0; --i) {
  37. for(int j = 0; j < 4; ++j) {
  38. sum[i][j] = sum[i + 1][j];
  39. }
  40. ++sum[i][id(i)];
  41. }
  42. LL ans = 0;
  43. for(int i = 1; i < n; ++i) {
  44. switch(id(i)) {
  45. case 0: ans += sum[i + 1][2] + sum[i + 1][3]; break;
  46. case 1: ans += sum[i + 1][2]; break;
  47. case 2: ans += sum[i + 1][0] + sum[i + 1][1]; break;
  48. case 3: ans += sum[i + 1][0]; break;
  49. default: break;
  50. }
  51. }
  52. printf("%I64d\n", ans);
  53. }
  54. return 0;
  55. }

C. The Phone Number

题意

构造一个 的排列,使得这个排列中最长上升子序列的长度加上最长下降子序列的长度最小。

输入

输入为一个整数

输出

输出 个整数,为 的排列,如果有多解输出任意一组。

样例

输入
4
输出
3 4 1 2
提示
最长上升子序列的长度为 ,最长下降子序列的长度为 ,总和为 ,是所有排列中最小的,但不是唯一的答案。
输入
2
输出
2 1
提示
最长上升子序列的长度为 ,最长下降子序列的长度为 ,总和为 ,是所有排列中最小的,但不是唯一的答案,序列 也是合法的答案。

题解

分成 块3,其中每一块中的数字是递增的,各个块之间是递减的,则最长上升子序列的长度和最长下降子序列的长度和最小,为 ,由基本不等式可知,当 的值最小。

过题代码

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

D. The Wu

题意

有一个元素个数为 串集合,集合内的 串可以重复出现,且每一个串的长度都为 ,对于两个 ,它们产生的价值为 ,现在有 次询问,每次询问由一个长度为 和一个数 组成,询问集合中满足条件 的字符串个数。

输入

第一行为 个整数 。第二行为 个整数 ,接下去 行每行一个长度为 字符串,接下去 行每行由一个长度为 字符串和 组成。

输出

对于每次询问,输出答案。

样例

输入
2 4 5
40 20
01
01
10
11
00 20
00 40
11 20
11 40
11 60
输出
2
4
2
3
4
输入
1 2 4
100
0
1
0 0
0 100
1 0
1 100
输出
1
2
1
2

题解

先将字符串转化为对应数字(),对于集合中的所有数字 ,统计每个数字出现的次数 ,接着对所有可能的询问 )都预处理出对应的价值 ,将价值为 的集合中的数字 的个数记录在 中,即 ,由于询问中的 最大值为 ,所以 数组的第 维只需要开到 ,大于 的可以不用处理,最后对 的每一次询问 都做一遍前缀和,就能得到询问为 时,价值小于等于 的所有满足条件的 的个数。预处理和查询的时间复杂度为

过题代码

  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, m, k, q;
  21. char str[20];
  22. int num[20], cnt[4096];
  23. int sum[4096 + 100][100 + 20];
  24. int main() {
  25. #ifdef LOCAL
  26. freopen("test.txt", "r", stdin);
  27. // freopen("test1.out", "w", stdout);
  28. #endif // LOCAL
  29. ios::sync_with_stdio(false);
  30. while(scanf("%d%d%d", &n, &m, &q) != EOF) {
  31. memset(cnt, 0, sizeof(cnt));
  32. memset(sum, 0, sizeof(sum));
  33. for(int i = 0; i < n; ++i) {
  34. scanf("%d", &num[i]);
  35. }
  36. for(int i = 0; i < m; ++i) {
  37. scanf("%s", str);
  38. int tmp = 0;
  39. for(int j = 0; j < n; ++j) {
  40. tmp |= (str[j] - '0') << j;
  41. }
  42. ++cnt[tmp];
  43. }
  44. for(int i = 0; i < (1 << n); ++i) {
  45. for(int j = 0; j < (1 << n); ++j) {
  46. int tmp = 0;
  47. for(int k = 0; k < n; ++k) {
  48. if(((i >> k) & 1) == ((j >> k) & 1)) {
  49. tmp += num[k];
  50. }
  51. }
  52. if(tmp > 100) {
  53. continue;
  54. }
  55. sum[i][tmp] += cnt[j];
  56. }
  57. }
  58. for(int i = 0; i < (1 << n); ++i) {
  59. for(int j = 1; j <= 100; ++j) {
  60. sum[i][j] += sum[i][j - 1];
  61. }
  62. }
  63. for(int i = 0; i < q; ++i) {
  64. scanf("%s%d", str, &k);
  65. int tmp = 0;
  66. for(int j = 0; j < n; ++j) {
  67. tmp |= (str[j] - '0') << j;
  68. }
  69. printf("%d\n", sum[tmp][k]);
  70. }
  71. }
  72. return 0;
  73. }

E. The Supersonic Rocket

题意

有两个点集,点的个数分别为 ,问两个点集分别构成的凸包是否可以通过旋转、平移使得两个凸包完全重合。

输入

第一行包含两个整数 ,接下去 行每行两个整数 ,表示第一个点集中每个点的坐标,最后 行每行两个整数 ,表示第二个点集中每个点的坐标,其中

输出

如果可以通过旋转与平移使两个凸包完全重合,输出 ,否则输出 ,大小写任意。

样例

输入
3 4
0 0
0 2
2 0
0 2
2 2
2 0
1 1
输出
YES
提示
点集在平面直角坐标系上的位置为:



将第一个点集旋转 后:



再将第二个点集平移 后:

输入
3 4
0 0
0 2
2 0
0 2
2 2
2 0
0 0
输出
NO
提示
不论如何平移旋转,两个凸包都不会重合。

题解

先分别跑出两个点集的凸包,判定两个凸包可以旋转平移重合的条件是:每条对应边的长度相等且对应角的大小相等,对应边直接求边长平方,对应角可以用叉积。将其中一个点集在凸包上所有点都复制到最后,让另一个凸包的所有点在这上面跑一遍 能够往后跑的条件就是对应边与对应角都相等。

过题代码

  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. struct Point {
  22. LL x, y;
  23. Point() {}
  24. Point(LL xx, LL yy) {
  25. x = xx;
  26. y = yy;
  27. }
  28. };
  29. LL operator*(const Point &a, const Point &b) {
  30. return a.x * b.x + a.y * b.y;
  31. }
  32. bool operator<(const Point &a, const Point &b) {
  33. return a.x == b.x? a.y < b.y: a.x < b.x;
  34. }
  35. LL operator^(const Point &a, const Point &b) {
  36. return a.x * b.y - a.y * b.x;
  37. }
  38. Point operator-(const Point &a, const Point &b) {
  39. return Point(a.x - b.x, a.y - b.y);
  40. }
  41. struct Cross_len {
  42. LL cross, len;
  43. };
  44. bool operator==(const Cross_len &a, const Cross_len &b) {
  45. return a.cross == b.cross && a.len == b.len;
  46. }
  47. bool operator!=(const Cross_len &a, const Cross_len &b) {
  48. return !(a == b);
  49. }
  50. int n[2], top[2];
  51. Point point[2][maxn], sta[2][maxn << 1];
  52. int Next[maxn];
  53. Cross_len cross_len[2][maxn << 1];
  54. void ConvexHull(Point *p, int n, int &top, Point *sta) {
  55. top = 0;
  56. sort(p + 1, p + 1 + n);
  57. for(int i = 1; i <= n; ++i) {
  58. while(top > 1 && ((sta[top - 1] - sta[top - 2]) ^ (p[i] - sta[top - 2])) <= 0) {
  59. --top;
  60. }
  61. sta[top++] = p[i];
  62. }
  63. int ttop = top;
  64. for(int i = n; i > 0; --i) {
  65. while(top > ttop && ((sta[top - 1] - sta[top - 2]) ^ (p[i] - sta[top - 2])) <= 0) {
  66. --top;
  67. }
  68. sta[top++] = p[i];
  69. }
  70. --top;
  71. }
  72. void Change_to_Cross_len(Point *p, int n, Cross_len *c) {
  73. Point p0 = p[0];
  74. for(int i = 0; i < n - 1; ++i) {
  75. p[i] = p[i + 1] - p[i];
  76. c[i].len = p[i] * p[i];
  77. }
  78. p[n - 1] = p0 - p[n - 1];
  79. c[n - 1].len = p[n - 1] * p[n - 1];
  80. for(int i = 0; i < n; ++i) {
  81. c[i].cross = p[i] ^ p[(i + 1) % n];
  82. }
  83. }
  84. void get_next(Cross_len *str, int n) {
  85. int j = -1;
  86. Next[0] = -1;
  87. for(int i = 1; i < n; ++i) {
  88. while(j != -1 && str[j + 1] != str[i]) {
  89. j = Next[j];
  90. }
  91. if(str[i] == str[j + 1]) {
  92. ++j;
  93. }
  94. Next[i] = j;
  95. }
  96. }
  97. bool kmp(Cross_len *str1, int n, Cross_len *str2, int m) {
  98. int j = -1;
  99. for(int i = 0; i < n; ++i) {
  100. while(j != -1 && str1[i] != str2[j + 1]) {
  101. j = Next[j];
  102. }
  103. if(str1[i] == str2[j + 1]) {
  104. ++j;
  105. }
  106. if(j == m - 1) {
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. int main() {
  113. #ifdef LOCAL
  114. freopen("test.txt", "r", stdin);
  115. // freopen("test1.out", "w", stdout);
  116. #endif // LOCAL
  117. ios::sync_with_stdio(false);
  118. while(scanf("%d%d", &n[0], &n[1]) != EOF) {
  119. memset(point, 0, sizeof(point));
  120. for(int i = 0; i < 2; ++i) {
  121. for(int j = 1; j <= n[i]; ++j) {
  122. scanf("%I64d%I64d", &point[i][j].x, &point[i][j].y);
  123. }
  124. ConvexHull(point[i], n[i], top[i], sta[i]);
  125. if(i == 0) {
  126. for(int j = 0; j < top[i]; ++j) {
  127. sta[i][j + top[i]] = sta[i][j];
  128. }
  129. top[i] *= 2;
  130. }
  131. Change_to_Cross_len(sta[i], top[i], cross_len[i]);
  132. }
  133. get_next(cross_len[1], top[1]);
  134. if(kmp(cross_len[0], top[0], cross_len[1], top[1])) {
  135. printf("Yes\n");
  136. } else {
  137. printf("No\n");
  138. }
  139. }
  140. return 0;
  141. }

F. The Neutral Zone

题意

注意本题内存限制为
定义函数:

,其中的每个 的质因数, 值是质因数对应的指数。
给定整数 和参数 的值,计算以下公式:

输入

输入包含 个整数

输出

将计算结果对 取模后输出。

样例

输入
12 0 0 1 0
输出
63
输入
4 1 2 3 4
输出
136

题解

内每个质数 对答案的贡献为 ,其中 为满足 的最大值,因此最后的答案就是:

取模,就是用 unsigned int 进行计算并让它们自然溢出。
最后是内存限制,如果我们用一个线性筛,就需要存每个数字是否是质数,如果用 bool 数组,bool 数组需要 ,用 bitset 压位需要 ,如果用埃氏筛,就可以在筛的过程中跳过 的倍数,我们将删去 的倍数后连续的数字用对应的 表示:

可以发现数字与下标之间是 的关系,因此如果跳过 的倍数,可以减少 的空间,最后只需要

过题代码

  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. unsigned n, a, b, c, d;
  21. unsigned ans;
  22. bitset<100000001> bit;
  23. unsigned f(unsigned x) {
  24. return a * x * x * x + b * x * x + c * x + d;
  25. }
  26. unsigned Get(unsigned x) {
  27. unsigned ret = f(x);
  28. unsigned tmp = 0;
  29. for(unsigned i = 1; i <= n / x; i *= x) {
  30. tmp += n / (i * x);
  31. }
  32. return ret * tmp;
  33. }
  34. int main() {
  35. #ifdef LOCAL
  36. freopen("test.txt", "r", stdin);
  37. // freopen("test1.out", "w", stdout);
  38. #endif // LOCAL
  39. ios::sync_with_stdio(false);
  40. while(cin >> n >> a >> b >> c >> d) {
  41. ans = Get(2) + Get(3);
  42. bit.reset();
  43. for(unsigned i = 5; i <= n; ++i) {
  44. if(i % 2 == 0 || i % 3 == 0) {
  45. continue;
  46. }
  47. if(bit[i / 3] == 0) {
  48. ans += Get(i);
  49. for(unsigned j = i; j <= n / i; ++j) {
  50. if(i * j % 2 == 0 || i * j % 3 == 0) {
  51. continue;
  52. }
  53. bit[i * j / 3] = 1;
  54. }
  55. }
  56. }
  57. cout << ans << endl;
  58. }
  59. return 0;
  60. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注