[关闭]
@Dmaxiya 2018-08-09T21:10:53.000000Z 字数 10419 阅读 1007

2018 Multi-University Training Contest 1

暑期集训


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

A. Maximum Multiple

题意

给定一个整数 ,找到三个整数 ,要求满足 都能整除 ,要使得 的值最大,求 的最大值。

输入

第一行为一个整数 ,表示数据组数,接下去 行每行一个整数

输出

如果存在满足条件的 的值,输出其中 的最大值,如果不存在满足条件的值,则输出

样例

输入
3
1
2
3
输出
-1
-1
1

题解

暴力打表找规律,可以发现如果 的倍数,那么 的值一定都是 ,如果 的倍数,那么 其中一个数是 ,另外两个数是 ,其他情况都输出

过题代码

  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. int T;
  19. LL n, a, b, c, ans;
  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. scanf("%I64d", &n);
  28. a = b = c = -1;
  29. if(n % 3 == 0) {
  30. a = b = c = n / 3;
  31. } else if(n % 4 == 0) {
  32. a = b = n / 4;
  33. c = a * 2;
  34. }
  35. if(a == -1) {
  36. printf("-1\n");
  37. } else {
  38. printf("%I64d\n", a * b * c);
  39. }
  40. }
  41. return 0;
  42. }

B. Balanced Sequence

题意

给出 个只包含左右括号的字符串,将这些字符串拼起来,要求最终拼出来的括号序列中,最长的完全匹配括号子序列的长度最长,问最长子序列长度。

输入

第一行为一个整数 ,接下去有 组数据,每组数据第一行为一个整数 ,后面 行每行为一个只包含左右括号的字符串 ,数据保证 的和不超过

输出

输出最长完全匹配子序列长度。

样例

输入
2
1
)()(()(
2
)
)(
输出
4
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 = 100000 + 100;
  19. struct Node {
  20. int l, m, r;
  21. Node() {
  22. l = m = r = 0;
  23. }
  24. Node(int L, int M, int R) {
  25. l = L;
  26. m = M;
  27. r = R;
  28. }
  29. };
  30. bool operator<(const Node &a, const Node &b) {
  31. int aa = min(a.r, b.l);
  32. int bb = min(b.r, a.l);
  33. if(aa == bb) {
  34. return a.r > b.r;
  35. }
  36. return aa > bb;
  37. }
  38. int T, n;
  39. char str[maxn];
  40. Node node[maxn];
  41. Node Get() {
  42. int len = strlen(str + 1);
  43. Node ret;
  44. ret.l = ret.r = 0;
  45. for(int i = 1; i <= len; ++i) {
  46. if(str[i] == ')') {
  47. if(ret.r != 0) {
  48. --ret.r;
  49. } else {
  50. ++ret.l;
  51. }
  52. } else {
  53. ++ret.r;
  54. }
  55. }
  56. ret.m = len - ret.l - ret.r;
  57. return ret;
  58. }
  59. int main() {
  60. #ifdef Dmaxiya
  61. freopen("test.txt", "r", stdin);
  62. #endif // Dmaxiya
  63. ios::sync_with_stdio(false);
  64. scanf("%d", &T);
  65. while(T--) {
  66. scanf("%d", &n);
  67. for(int i = 0; i < n; ++i) {
  68. scanf("%s", str + 1);
  69. node[i] = Get();
  70. }
  71. sort(node, node + n);
  72. Node tmp = node[0];
  73. Node ll = node[0];
  74. Node rr;
  75. for(int i = 1; i < n; ++i) {
  76. rr = node[i];
  77. tmp.l = rr.l + ll.l - min(ll.r, rr.l);
  78. tmp.r = ll.r + rr.r - min(ll.r, rr.l);
  79. tmp.m = ll.m + rr.m + 2 * min(ll.r, rr.l);
  80. ll = tmp;
  81. }
  82. printf("%d\n", tmp.m);
  83. }
  84. return 0;
  85. }

C. Triangle Partition

题意

在平面直角坐标系上给出 个点,每 个点连成一个三角形,共连成 个三角形,要求任意两个三角形都没有公共点,输出连接三角形的方案。

输入

第一行为一个整数 ,接下去有 组数据,每组数据第一行为一个整数 ,接下去的 行每行两个整数 ,数据保证所有 的和不超过 ,且不存在三点共线。

输出

输出 行,每行 个整数 ,表示选择第 个点构成一个三角形。

样例

输入
1
-1
1 2
2 3
3 5
输出
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 = 3000 + 100;
  19. struct Node {
  20. int x, y;
  21. int Index;
  22. };
  23. bool operator<(const Node &a, const Node &b) {
  24. return (a.x == b.x? a.y < b.y: a.x < b.x);
  25. }
  26. int T, n;
  27. Node node[maxn];
  28. int main() {
  29. #ifdef Dmaxiya
  30. freopen("test.txt", "r", stdin);
  31. #endif // Dmaxiya
  32. ios::sync_with_stdio(false);
  33. scanf("%d", &T);
  34. while(T--) {
  35. scanf("%d", &n);
  36. n *= 3;
  37. for(int i = 1; i <= n; ++i) {
  38. scanf("%d%d", &node[i].x, &node[i].y);
  39. node[i].Index = i;
  40. }
  41. sort(node + 1, node + 1 + n);
  42. for(int i = 1; i <= n; ++i) {
  43. if(i != 1) {
  44. printf(" ");
  45. }
  46. printf("%d", node[i].Index);
  47. }
  48. printf("\n");
  49. }
  50. return 0;
  51. }

D. Distinct Values

题意

构造一个长度为 的正整数序列,要求这个序列内对于所有给定的 个区间 ,都满足区间内所有数字两两互不相等,求字典序最小的合法序列。

输入

第一行为一个整数 ,接下去 组数据,每组数据第一行为两个整数 ,接下去 行每行两个整数 ,数据保证所有 至少一个的和不超过

输出

输出一个长度为 的合法序列。

样例

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

题解

将所有区间按 为主关键字, 为次关键字排序,然后 预处理出每个数字对应的最靠前的左区间下标 ,如果某个点不在任何一个区间内,则 ,然后先把 所有数字放到小顶堆内,从前往后扫,每次取出堆顶的值放入当前位置,如果出现 ,就先将区间 内的所有放下去的数字收回到小顶堆,就可以得到答案。

过题代码

  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 = 100000 + 100;
  19. struct Node {
  20. int l, r;
  21. };
  22. bool operator<(const Node &a, const Node &b) {
  23. return (a.l == b.l? a.r < a.r: a.l < b.l);
  24. }
  25. int T, n, m;
  26. Node node[maxn];
  27. int ans[maxn], head[maxn];
  28. priority_queue<int, vector<int>, greater<int> > que;
  29. int main() {
  30. #ifdef Dmaxiya
  31. freopen("test.txt", "r", stdin);
  32. #endif // Dmaxiya
  33. ios::sync_with_stdio(false);
  34. scanf("%d", &T);
  35. while(T--) {
  36. while(!que.empty()) {
  37. que.pop();
  38. }
  39. scanf("%d%d", &n, &m);
  40. for(int i = 1; i <= n; ++i) {
  41. head[i] = i;
  42. que.push(i);
  43. }
  44. for(int i = 0; i < m; ++i) {
  45. scanf("%d%d", &node[i].l, &node[i].r);
  46. }
  47. sort(node, node + m);
  48. int End = 1;
  49. for(int i = 0; i < m; ++i) {
  50. End = max(End, node[i].l);
  51. while(End <= node[i].r) {
  52. head[End] = node[i].l;
  53. ++End;
  54. }
  55. }
  56. ans[1] = 1;
  57. que.pop();
  58. for(int i = 2; i <= n; ++i) {
  59. if(head[i] != head[i - 1]) {
  60. for(int j = head[i - 1]; j < head[i]; ++j) {
  61. que.push(ans[j]);
  62. }
  63. }
  64. ans[i] = que.top();
  65. que.pop();
  66. }
  67. for(int i = 1; i <= n; ++i) {
  68. if(i != 1) {
  69. printf(" ");
  70. }
  71. printf("%d", ans[i]);
  72. }
  73. printf("\n");
  74. }
  75. return 0;
  76. }

G. Chiaki Sequence Revisited

题意

定义一个序列

的值。

输入

第一行为一个整数 ,表示有 组数据,每组数据包含一个整数

输出

每组数据输出

样例

输入
10
1
2
3
4
5
6
7
8
9
10
输出
1
2
4
6
9
13
17
21
26
32

题解

先打出前 的表:

首先对于任意两个相邻且不相等的数字,它们的差都为 。我将除了第 个数字和第 个数字的连续相同的数字统计个数以及个数的前缀和打表:

中每个数字第一次出现的位置打上标记,可以发现这是一个 的循环,对于每一个数字 ,我们首先可以找到小于等于它的最大的 值, 个数字其对应的 的值只增加了 个数字,将 加上 减去 ,不断查找下去,就可以在 次内确定 的值。而从 的所有数字的前缀和,通过观察可以发现,如果 是某段连续相同数字的最后一项,可以先计算 的等差数列求和,再计算 等差数列求和……每次都计算一个首项为 ,末项为 ,项数为 ,的一个等差数列,直到 ,停止计算,就能得到 的所有数字的前缀和;而如果 不是某段连续相同数字的最后一项,就要先计算出 的前一个连续的相同数字的最后一项,按照上述方法计算后,加上 这段连续的数字中, 前面的数字个数。由于前面的规律是在去掉第 个数字找到的,所以计算最后答案的时候要
最后注意答案会乘爆 long long,所以在乘法之前应该先取模一次。

过题代码

  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 LL MOD = 1000000007;
  19. const LL inv = 500000004;
  20. int T;
  21. LL n;
  22. LL two[64], two_1[64];
  23. LL *it;
  24. LL get_a(LL n) {
  25. if(n == 1) {
  26. return 0;
  27. }
  28. --n;
  29. LL ret = 0;
  30. while(n != 0) {
  31. it = upper_bound(two_1, two_1 + 62, n);
  32. --it;
  33. ret += two[it - two_1 - 1];
  34. n -= *it;
  35. }
  36. return ret;
  37. }
  38. LL solve(LL d, LL n) {
  39. return (((d + (n / d * d % MOD)) % MOD) * ((n / d) % MOD) % MOD) * inv % MOD;
  40. }
  41. int main() {
  42. #ifdef Dmaxiya
  43. freopen("test.txt", "r", stdin);
  44. #endif // Dmaxiya
  45. ios::sync_with_stdio(false);
  46. two[0] = 1;
  47. for(int i = 1; i < 62; ++i) {
  48. two[i] = two[i - 1] * 2;
  49. }
  50. for(int i = 0; i < 62; ++i) {
  51. two_1[i] = two[i] - 1;
  52. }
  53. cin >> T;
  54. while(T--) {
  55. cin >> n;
  56. if(n == 1) {
  57. cout << 1 << endl;
  58. continue;
  59. }
  60. LL a_n = get_a(n);
  61. LL n_1 = n;
  62. LL a_n_1;
  63. do {
  64. --n_1;
  65. a_n_1 = get_a(n_1);
  66. } while(a_n_1 == a_n);
  67. LL ans = 0;
  68. for(int i = 0; two[i] <= a_n_1; ++i) {
  69. ans += solve(two[i], a_n_1);
  70. ans %= MOD;
  71. }
  72. ans = (ans + (a_n % MOD) * (n - n_1) % MOD) % MOD;
  73. ans = (ans + 1) % MOD;
  74. cout << ans << endl;
  75. }
  76. return 0;
  77. }

H. RMQ Similar Sequence

题意

首先给定一个长度为 的整数序列 ,定义 表示查找区间 内的最大值的下标,如果存在多个最大值,则取最左边的数字的下标。现在要构造一个长度为 的序列 ,要求每个数字都是属于区间 内的实数,且对于每一个 ,其询问结果都和 相同,问所有满足条件的序列 的期望值。

输入

第一行为一个整数 ,接下去有 组数据,每组数据第一行为一个整数 ,第二行为 个整数 ,数据保证所有 的和不超过

输出

输出期望对 取模的结果,如果期望无法被 整除,先化简期望为 ,保证 互质且都为正整数的情况下,输出 的值,要求 能够被 整除。

样例

输入
3
3
1 2 3
3
1 2 1
5
1 2 3 2 1
输出
250000002
500000004
125000001

题解

根据题意建立一棵序列 的笛卡尔树,题目即求笛卡尔树与数列 同构的序列 的序列和的期望,由于 内每个数字都是实数,所以任意两个数字相等的概率趋近于 ,对于每一个节点对应的区间,节点的数字大于该区间内所有数字的概率为 ,其中 为节点 对应子树的节点数,将所有节点的满足条件的概率相乘,再乘上序列求和的期望值 ,就是答案。最后要求期望对 的取模的结果,就是将结果 转化成

过题代码

  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 = 1000000 + 100;
  19. const LL MOD = 1000000007;
  20. LL ans;
  21. LL inv[maxn];
  22. struct Tree {
  23. int root, top, n;
  24. int sta[maxn], l[maxn], r[maxn];
  25. bool vis[maxn];
  26. void build(int *num, int nn) {
  27. n = nn;
  28. top = 0;
  29. memset(l, 0, sizeof(int) * (n + 1));
  30. memset(r, 0, sizeof(int) * (n + 1));
  31. memset(vis, 0, sizeof(bool) * (n + 1));
  32. for(int i = 1; i <= n; ++i) {
  33. int tmp = top;
  34. while(top > 0 && num[sta[top - 1]] < num[i]) {
  35. --top;
  36. }
  37. if(top != 0) {
  38. r[sta[top - 1]] = i;
  39. }
  40. if(top < tmp) {
  41. l[i] = sta[top];
  42. }
  43. sta[top++] = i;
  44. }
  45. for(int i = 1; i <= n; ++i) {
  46. vis[l[i]] = vis[r[i]] = true;
  47. }
  48. for(int i = 1; i <= n; ++i) {
  49. if(!vis[i]) {
  50. root = i;
  51. }
  52. }
  53. }
  54. int dfs(int x) {
  55. int cnt = 1;
  56. if(l[x] != 0) {
  57. cnt += dfs(l[x]);
  58. }
  59. if(r[x] != 0) {
  60. cnt += dfs(r[x]);
  61. }
  62. ans = (ans * inv[cnt]) % MOD;
  63. return cnt;
  64. }
  65. };
  66. int T, n;
  67. int num[maxn];
  68. Tree t;
  69. void Init() {
  70. inv[1] = 1;
  71. for(int i = 2; i < maxn; ++i) {
  72. inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;
  73. }
  74. }
  75. int main() {
  76. #ifdef Dmaxiya
  77. freopen("test.txt", "r", stdin);
  78. #endif // Dmaxiya
  79. ios::sync_with_stdio(false);
  80. Init();
  81. scanf("%d", &T);
  82. while(T--) {
  83. scanf("%d", &n);
  84. ans = (n * inv[2]) % MOD;
  85. for(int i = 1; i <= n; ++i) {
  86. scanf("%d", &num[i]);
  87. }
  88. t.build(num, n);
  89. t.dfs(t.root);
  90. printf("%I64d\n", ans);
  91. }
  92. return 0;
  93. }

K. Time Zone

题意

给出北京时间,要求计算其他时间。

输入

第一行为一个整数 ,接下去 行每行前两个数字为 ,表示北京时间,接着是一个格式为 UTC+X / UTC-X / UTC+X.Y / UTC-X.Y 的字符串,其中

输出

输出给定 UTC-X[.Y] 对应的时间。

样例

输入
3
11 11 UTC+8
11 12 UTC+9
11 23 UTC+0
输出
11:11
12:12
03:23

题解

将所有数字都转化为分钟后按照题意模拟,注意精度误差,不要读入小数。

过题代码

  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 <sstream>
  17. using namespace std;
  18. #define LL long long
  19. int T;
  20. int h, m, a, b;
  21. char ch;
  22. int main() {
  23. #ifdef Dmaxiya
  24. freopen("test.txt", "r", stdin);
  25. #endif // Dmaxiya
  26. ios::sync_with_stdio(false);
  27. scanf("%d", &T);
  28. while(T--) {
  29. b = 0;
  30. scanf("%d %d UTC%c%d.%d", &h, &m, &ch, &a, &b);
  31. m += h * 60;
  32. b = b * 6 + a * 60;
  33. if(ch == '-') {
  34. b *= -1;
  35. }
  36. m += b - 480;
  37. m = (m % (24 * 60) + 24 * 60) % (24 * 60);
  38. h = m / 60;
  39. m = m % 60;
  40. printf("%02d:%02d\n", h, m);
  41. }
  42. return 0;
  43. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注