[关闭]
@Dmaxiya 2020-12-15T15:26:39.000000Z 字数 9251 阅读 2407

2018 Multi-University Training Contest 5

暑期集训


链接:2018 Multi-University Training Contest 5
过题数:1
排名:470/704
成员:官展鹏,冯彦博,孙昊哲

B. Beautiful Now

题意

给定一个没有前导零的整数 ,其十进制表示为 ,即 ,可以对这个整数进行 次操作,每次操作选择两个整数 并将这两个位置上的数字交换,问最终能够交换得到的最小的数字和最大的数字分别是多少。

输入

第一行为一个整数 ,接下去 行每行两个整数 。

输出

每组数据输出经过 次合法交换后能够得到的最大值和最小值。

样例

输入
5
12 1
213 2
998244353 1
998244353 2
998244353 3
输出
12 21
123 321
298944353 998544323
238944359 998544332
233944859 998544332

题解

由于每次交换可以选择 ,其中 可以等于 ,因此答案可以从任意小于等于 次的合法交换中取最小值得到,题目要求在交换过程中不允许出现前导零,可以证明,如果交换的结果不含有前导零,就一定存在一种方式在不出现前导零的情况下从原数字得到结果数字。最后就是暴力枚举所有交换次数小于等于 的全排列,每次 生成全排列会超时,可以预处理 以内的全排列(如果长度达到 就只有一种答案),全排列的交换次数可以用并查集找环计算。如果 大于等于 直接生成不含前导零的最小字典序和最大字典序作为答案。

过题代码

  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. #include <unordered_set>
  19. using namespace std;
  20. #define LL long long
  21. const int maxn = 20;
  22. int T, k, len;
  23. char n[maxn], Max[maxn], Min[maxn], tmp[maxn];
  24. int fa[maxn], per[maxn], ten[maxn];
  25. vector<int> G[maxn][maxn];
  26. void Init(int len) {
  27. for(int i = 0; i < len; ++i) {
  28. fa[i] = i;
  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. fa[xx] = yy;
  38. }
  39. int Count(int *per, int len) {
  40. int cnt = len;
  41. Init(len);
  42. for(int i = 0; i < len; ++i) {
  43. if(Find(per[i]) != Find(i)) {
  44. unit(per[i], i);
  45. --cnt;
  46. }
  47. }
  48. return len - cnt;
  49. }
  50. void Init() {
  51. ten[0] = 1;
  52. for(int i = 1; i <= 9; ++i) {
  53. ten[i] = ten[i - 1] * 10;
  54. }
  55. for(int len = 0; len <= 9; ++len) {
  56. for(int i = 0; i < len; ++i) {
  57. per[i] = i;
  58. }
  59. do {
  60. int cnt = Count(per, len);
  61. int num = 0;
  62. for(int i = 0; i < len; ++i) {
  63. num += per[i] * ten[i];
  64. }
  65. G[len][cnt].push_back(num);
  66. } while(next_permutation(per, per + len));
  67. }
  68. }
  69. int main() {
  70. #ifdef LOCAL
  71. freopen("test.txt", "r", stdin);
  72. // freopen("out.txt", "w", stdout);
  73. #endif // LOCAL
  74. ios::sync_with_stdio(false);
  75. Init();
  76. scanf("%d", &T);
  77. while(T--) {
  78. scanf("%s%d", n, &k);
  79. len = strlen(n);
  80. if(len == 10) {
  81. printf("%s %s\n", n, n);
  82. continue;
  83. }
  84. strcpy(Min, n);
  85. strcpy(Max, n);
  86. if(k >= len - 1) {
  87. sort(Min, Min + len);
  88. sort(Max, Max + len);
  89. int Index = 0;
  90. for(int i = 0; i < len; ++i) {
  91. if(Min[i] != '0') {
  92. Index = i;
  93. break;
  94. }
  95. }
  96. swap(Min[0], Min[Index]);
  97. for(int i = 0; i < len / 2; ++i) {
  98. swap(Max[i], Max[len - i - 1]);
  99. }
  100. printf("%s %s\n", Min, Max);
  101. continue;
  102. }
  103. for(int i = 1; i <= k; ++i) {
  104. int llen = G[len][i].size();
  105. for(int j = 0; j < llen; ++j) {
  106. int num = G[len][i][j];
  107. if(n[num % 10] == '0') {
  108. continue;
  109. }
  110. for(int k = 0; k < len; ++k) {
  111. tmp[k] = n[num / ten[k] % 10];
  112. }
  113. tmp[len] = '\0';
  114. if(strcmp(Min, tmp) > 0) {
  115. strcpy(Min, tmp);
  116. } else if(strcmp(Max, tmp) < 0) {
  117. strcpy(Max, tmp);
  118. }
  119. }
  120. }
  121. printf("%s %s\n", Min, Max);
  122. }
  123. return 0;
  124. }

E. Everything Has Changed

题意

在平面直角坐标系内有一个圆心在原点,半径为 的初始圆,以及 个圆心在 ,半径为 的圆,这 个圆两两互不相交且不会包含整个初始圆,这 个小圆将对初始圆进行切割,问最终初始圆的周长。

输入

第一行为一个整数 ,接下去有 组数据,每组数据第一行为两个整数 ,接下去 行每行三个整数 。

输出

输出最终圆的周长,误差在 以内都认为答案正确。

样例

输入
1
4 10
6 3 5
10 -4 3
-2 -4 4
0 9 1
输出
81.62198908430238475376
提示
切割圆的方式如下:



其中红线为圆的周长。

题解

对于两圆相离或者外切的情况,对周长的贡献都为 ,对于两圆内含的情况,贡献也为零,因此只要考虑两圆相交与内切的情况,先考虑第一种相交(两圆圆心在交点连线的两侧):



设 ,则可以联立方程:

解方程得到 ,而对于两圆圆心在交点连线同侧的情况:



只需要将上面方程组的第 个方程改为 即可,得到 ,发现和上式差一个负号,要求圆 的贡献,就是求圆 在圆 内部的圆弧长度,因此如果直接对 取 ,第二种情况的负号正好使得圆 的圆心角计算得到大于 的部分。
最后还需要减掉圆 在圆 内部的圆弧长度,计算方式与第一种情况相同。

过题代码

  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 double eps = 1e-8;
  21. const double PI = acos(-1.0);
  22. const int maxn = 100 + 100;
  23. struct Circle {
  24. double x, y;
  25. double r, val;
  26. };
  27. int T, n;
  28. double R;
  29. Circle c[maxn];
  30. int sign(const double &x) {
  31. if(fabs(x) < eps) {
  32. return 0;
  33. }
  34. if(x < 0) {
  35. return -1;
  36. }
  37. return 1;
  38. }
  39. void get_val(Circle &c, int Index) {
  40. double dis = c.x * c.x + c.y * c.y;
  41. int sgn = sign(dis - (R + c.r) * (R + c.r));
  42. if(sgn > 0) {
  43. c.val = 0;
  44. return ;
  45. }
  46. sgn = (R - c.r) * (R - c.r) - dis;
  47. if(sgn > 0) {
  48. c.val = 0;
  49. return ;
  50. }
  51. double a = (c.r * c.r - R * R + dis) / (2 * sqrt(dis));
  52. double theta = acos(a / c.r) * 2;
  53. c.val = c.r * theta;
  54. double b = (R * R - c.r * c.r + dis) / (2 * sqrt(dis));
  55. theta = acos(b / R) * 2;
  56. c.val -= R * theta;
  57. }
  58. bool Set(Circle &a, Circle &b) {
  59. double dis = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
  60. int sgn = sign(dis - (a.r + b.r) * (a.r + b.r));
  61. if(sgn > 0) {
  62. return false;
  63. }
  64. sgn = (a.r - b.r) * (a.r - b.r) - dis;
  65. if(sgn > 0) {
  66. return false;
  67. }
  68. return true;
  69. }
  70. int main() {
  71. #ifdef LOCAL
  72. freopen("test.txt", "r", stdin);
  73. // freopen("out.txt", "w", stdout);
  74. #endif // LOCAL
  75. ios::sync_with_stdio(false);
  76. scanf("%d", &T);
  77. while(T--) {
  78. scanf("%d%lf", &n, &R);
  79. for(int i = 1; i <= n; ++i) {
  80. scanf("%lf%lf%lf", &c[i].x, &c[i].y, &c[i].r);
  81. get_val(c[i], i);
  82. }
  83. double ans = 2 * PI * R;
  84. for(int i = 1; i <= n; ++i) {
  85. ans += c[i].val;
  86. }
  87. printf("%.10f\n", ans);
  88. }
  89. return 0;
  90. }

G. Glad You Came

题意

有一个长度为 的序列,序列中每个数字的初始值都为 ,接下来对这个序列进行 次操作,每次操作将区间 之间的所有数字 ,都更新为 ,输出 次操作后 的值。为了避免大数据输入,给出初始随机种子 ,每次随机数字由以下伪代码生成:



总共生成 个随机数 ,第 次操作的 由以下规则得到:

输入

第一行为一个整数 ,接下去 行每行 个整数 。

输出

对于每组数据,输出最终结果。

样例

输入
4
1 10 100 1000 10000
10 100 1000 10000 100000
100 1000 10000 100000 1000000
1000 10000 100000 1000000 10000000
输出
1031463378
1446334207
351511856
47320301347
提示
第一组数据经过 次操作序列元素为 ;
第二组数据经过 次操作后数组元素为 。

题解

反向用 表 地标记最大值更新,最后从大到小将最大值标记往下打,就可以得到答案,时间复杂度为 。

过题代码

  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. const int Log = 20;
  22. const unsigned int one = (unsigned int)1 << 30;
  23. int T, n, m;
  24. unsigned int x, y, z, l, r, v;
  25. unsigned int f1, f2, f3;
  26. LL stmax[maxn][Log], mn[maxn];
  27. void Init() {
  28. mn[0] = -1;
  29. for(int i = 1; i <= n; ++i) {
  30. mn[i] = ((i & (i - 1)) == 0)? mn[i - 1] + 1: mn[i - 1];
  31. memset(stmax[i], 0, sizeof(stmax[i]));
  32. }
  33. }
  34. unsigned int Rand(unsigned int &x, unsigned int &y, unsigned int &z) {
  35. x = x ^ (x << 11);
  36. x = x ^ (x >> 4);
  37. x = x ^ (x << 5);
  38. x = x ^ (x >> 14);
  39. unsigned int w = x ^ (y ^ z);
  40. x = y;
  41. y = z;
  42. z = w;
  43. return z;
  44. }
  45. int main() {
  46. #ifdef LOCAL
  47. freopen("test.txt", "r", stdin);
  48. // freopen("out.txt", "w", stdout);
  49. #endif // LOCAL
  50. ios::sync_with_stdio(false);
  51. cin >> T;
  52. while(T--) {
  53. cin >> n >> m >> x >> y >> z;
  54. Init();
  55. for(int i = 1; i <= m; ++i) {
  56. f1 = Rand(x, y, z);
  57. f2 = Rand(x, y, z);
  58. f3 = Rand(x, y, z);
  59. f1 = f1 % (unsigned int)n + 1;
  60. f2 = f2 % (unsigned int)n + 1;
  61. l = min(f1, f2);
  62. r = max(f1, f2);
  63. v = f3 % one;
  64. int k = mn[r - l + 1];
  65. stmax[l][k] = max(stmax[l][k], (LL)v);
  66. stmax[r - (1 << k) + 1][k] = max(stmax[r - (1 << k) + 1][k], (LL)v);
  67. }
  68. for(int j = Log - 1; j > 0; --j) {
  69. for(int i = 1; i + (1 << (j - 1)) <= n; ++i) {
  70. stmax[i][j - 1] = max(stmax[i][j - 1], stmax[i][j]);
  71. stmax[i + (1 << (j - 1))][j - 1] = max(stmax[i + (1 << (j - 1))][j - 1], stmax[i][j]);
  72. }
  73. }
  74. LL ans = 0;
  75. for(int i = 1; i <= n; ++i) {
  76. ans ^= (LL)i * stmax[i][0];
  77. }
  78. cout << ans << endl;
  79. }
  80. return 0;
  81. }

H. Hills And Valleys

题意

给定一个长度为 的序列,可以选择序列上的一个区间 ,将这个区间上的所有数字翻转,求进行一次操作后能够得到最长非递减子序列的长度。

输入

第一行为一个整数 ,接下去有 组数据,每组数据第一行为一个整数 ,第二行包含一个长度为 的数字字符串 。

输出

输出经过一次交换后能够得到的最长非递减子序列的长度,以及翻转区间的左右端点。

样例

输入
2
9
864852302
9
203258468
输出
5 1 8
6 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 <functional>
  15. #include <algorithm>
  16. using namespace std;
  17. #define LL long long
  18. const int maxn = 100000 + 100;
  19. int T, n, ans, ansl, ansr;
  20. char str[maxn], stmp[20];
  21. int dp[maxn][20], dpl[maxn][20], dpr[maxn][20];
  22. int solve(int l, int r, int len) {
  23. for(int i = 1; i <= n; ++i) {
  24. for(int j = 1; j <= len; ++j) {
  25. dp[i][j] = dp[i - 1][j];
  26. dpl[i][j] = dpl[i - 1][j];
  27. dpr[i][j] = dpr[i - 1][j];
  28. if(str[i] == stmp[j]) {
  29. ++dp[i][j];
  30. if(j == l && dpl[i][j] == 0) {
  31. dpl[i][j] = i;
  32. }
  33. if(j == r) {
  34. dpr[i][j] = i;
  35. }
  36. }
  37. if(dp[i][j] < dp[i][j - 1]) {
  38. dp[i][j] = dp[i][j - 1];
  39. dpl[i][j] = dpl[i][j - 1];
  40. dpr[i][j] = dpr[i][j - 1];
  41. }
  42. }
  43. }
  44. return dp[n][len];
  45. }
  46. void Create(int l, int r) {
  47. int Index = 1;
  48. for(int i = 0; i <= l; ++i) {
  49. stmp[Index++] = i + '0';
  50. }
  51. for(int i = r; i >= l; --i) {
  52. stmp[Index++] = i + '0';
  53. }
  54. for(int i = r; i < 10; ++i) {
  55. stmp[Index++] = i + '0';
  56. }
  57. }
  58. int main() {
  59. #ifdef LOCAL
  60. freopen("test.txt", "r", stdin);
  61. // freopen("testout.txt", "w", stdout);
  62. #endif // LOCAL
  63. ios::sync_with_stdio(false);
  64. scanf("%d", &T);
  65. while(T--) {
  66. scanf("%d%s", &n, str + 1);
  67. for(int i = 0; i < 10; ++i) {
  68. stmp[i + 1] = i + '0';
  69. }
  70. ans = solve(0, 0, 10);
  71. ansl = ansr = 1;
  72. for(int i = 0; i < 10; ++i) {
  73. for(int j = i + 1; j < 10; ++j) {
  74. Create(i, j);
  75. int tmp = solve(i + 2, j + 2, 12);
  76. if(tmp > ans && dpl[n][12] != 0 && dpr[n][12] != 0) {
  77. ans = tmp;
  78. ansl = dpl[n][12];
  79. ansr = dpr[n][12];
  80. }
  81. }
  82. }
  83. printf("%d %d %d\n", ans, ansl, ansr);
  84. }
  85. return 0;
  86. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注