@Dmaxiya
2018-08-09T13:10:53.000000Z
字数 10419
阅读 1236
暑期集训
链接:2018 Multi-University Training Contest 1
过题数:6
排名:78/818
成员:官展鹏,冯彦博,孙昊哲
给定一个整数 ,找到三个整数 ,要求满足 且 都能整除 ,要使得 的值最大,求 的最大值。
第一行为一个整数 ,表示数据组数,接下去 行每行一个整数 。
如果存在满足条件的 的值,输出其中 的最大值,如果不存在满足条件的值,则输出 。
| 输入 |
|---|
| 3 1 2 3 |
| 输出 |
| -1 -1 1 |
暴力打表找规律,可以发现如果 是 的倍数,那么 的值一定都是 ,如果 是 的倍数,那么 其中一个数是 ,另外两个数是 ,其他情况都输出 。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cfloat>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <algorithm>using namespace std;#define LL long longint T;LL n, a, b, c, ans;int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);scanf("%d", &T);while(T--) {scanf("%I64d", &n);a = b = c = -1;if(n % 3 == 0) {a = b = c = n / 3;} else if(n % 4 == 0) {a = b = n / 4;c = a * 2;}if(a == -1) {printf("-1\n");} else {printf("%I64d\n", a * b * c);}}return 0;}
给出 个只包含左右括号的字符串,将这些字符串拼起来,要求最终拼出来的括号序列中,最长的完全匹配括号子序列的长度最长,问最长子序列长度。
第一行为一个整数 ,接下去有 组数据,每组数据第一行为一个整数 ,后面 行每行为一个只包含左右括号的字符串 ,数据保证 的和不超过 。
输出最长完全匹配子序列长度。
| 输入 |
|---|
| 2 1 )()(()(2 ))( |
| 输出 |
| 4 2 |
可以发现,每个字符串中,已经匹配的左右括号,不论以任何顺序与其他字符串连接,这些已经匹配的左右括号对都不会改变,用栈模拟匹配后,最后只会剩下最前面的 个
)和最后面的 个(,类似)))((的形式,只有这些括号在和其他字符串匹配的时候才会产生新的括号匹配子序列。
如果字符串 有 个)和 个(,字符串 有 个)和 个(,将 放在 的前面(不必相邻),则会产生 对新的括号匹配子序列,将 放在 的前面,则会产生 对新的括号匹配子序列,现在贪心地对所有字符串进行排序,如果 ,则把 排在 的前面,如果 大,则把 排在 的前面,如果 ,则把含有(个数多的放在前面,最后扫一遍整个排序拼接后的字符串,就可以计得到答案。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cfloat>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <algorithm>using namespace std;#define LL long longconst int maxn = 100000 + 100;struct Node {int l, m, r;Node() {l = m = r = 0;}Node(int L, int M, int R) {l = L;m = M;r = R;}};bool operator<(const Node &a, const Node &b) {int aa = min(a.r, b.l);int bb = min(b.r, a.l);if(aa == bb) {return a.r > b.r;}return aa > bb;}int T, n;char str[maxn];Node node[maxn];Node Get() {int len = strlen(str + 1);Node ret;ret.l = ret.r = 0;for(int i = 1; i <= len; ++i) {if(str[i] == ')') {if(ret.r != 0) {--ret.r;} else {++ret.l;}} else {++ret.r;}}ret.m = len - ret.l - ret.r;return ret;}int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);scanf("%d", &T);while(T--) {scanf("%d", &n);for(int i = 0; i < n; ++i) {scanf("%s", str + 1);node[i] = Get();}sort(node, node + n);Node tmp = node[0];Node ll = node[0];Node rr;for(int i = 1; i < n; ++i) {rr = node[i];tmp.l = rr.l + ll.l - min(ll.r, rr.l);tmp.r = ll.r + rr.r - min(ll.r, rr.l);tmp.m = ll.m + rr.m + 2 * min(ll.r, rr.l);ll = tmp;}printf("%d\n", tmp.m);}return 0;}
在平面直角坐标系上给出 个点,每 个点连成一个三角形,共连成 个三角形,要求任意两个三角形都没有公共点,输出连接三角形的方案。
第一行为一个整数 ,接下去有 组数据,每组数据第一行为一个整数 ,接下去的 行每行两个整数 ,数据保证所有 的和不超过 ,且不存在三点共线。
输出 行,每行 个整数 ,表示选择第 个点构成一个三角形。
| 输入 |
|---|
| 1 -1 1 2 2 3 3 5 |
| 输出 |
| 1 2 3 |
将所有点按横坐标从小到大排序,选取连续的三个点,就能构成合法的三角形。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cfloat>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <algorithm>using namespace std;#define LL long longconst int maxn = 3000 + 100;struct Node {int x, y;int Index;};bool operator<(const Node &a, const Node &b) {return (a.x == b.x? a.y < b.y: a.x < b.x);}int T, n;Node node[maxn];int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);scanf("%d", &T);while(T--) {scanf("%d", &n);n *= 3;for(int i = 1; i <= n; ++i) {scanf("%d%d", &node[i].x, &node[i].y);node[i].Index = i;}sort(node + 1, node + 1 + n);for(int i = 1; i <= n; ++i) {if(i != 1) {printf(" ");}printf("%d", node[i].Index);}printf("\n");}return 0;}
构造一个长度为 的正整数序列,要求这个序列内对于所有给定的 个区间 ,都满足区间内所有数字两两互不相等,求字典序最小的合法序列。
第一行为一个整数 ,接下去 组数据,每组数据第一行为两个整数 ,接下去 行每行两个整数 ,数据保证所有 和 至少一个的和不超过 。
输出一个长度为 的合法序列。
| 输入 |
|---|
| 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 |
将所有区间按 为主关键字, 为次关键字排序,然后 预处理出每个数字对应的最靠前的左区间下标 ,如果某个点不在任何一个区间内,则 ,然后先把 到 所有数字放到小顶堆内,从前往后扫,每次取出堆顶的值放入当前位置,如果出现 ,就先将区间 内的所有放下去的数字收回到小顶堆,就可以得到答案。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cfloat>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <algorithm>using namespace std;#define LL long longconst int maxn = 100000 + 100;struct Node {int l, r;};bool operator<(const Node &a, const Node &b) {return (a.l == b.l? a.r < a.r: a.l < b.l);}int T, n, m;Node node[maxn];int ans[maxn], head[maxn];priority_queue<int, vector<int>, greater<int> > que;int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);scanf("%d", &T);while(T--) {while(!que.empty()) {que.pop();}scanf("%d%d", &n, &m);for(int i = 1; i <= n; ++i) {head[i] = i;que.push(i);}for(int i = 0; i < m; ++i) {scanf("%d%d", &node[i].l, &node[i].r);}sort(node, node + m);int End = 1;for(int i = 0; i < m; ++i) {End = max(End, node[i].l);while(End <= node[i].r) {head[End] = node[i].l;++End;}}ans[1] = 1;que.pop();for(int i = 2; i <= n; ++i) {if(head[i] != head[i - 1]) {for(int j = head[i - 1]; j < head[i]; ++j) {que.push(ans[j]);}}ans[i] = que.top();que.pop();}for(int i = 1; i <= n; ++i) {if(i != 1) {printf(" ");}printf("%d", ans[i]);}printf("\n");}return 0;}
定义一个序列
求 的值。
第一行为一个整数 ,表示有 组数据,每组数据包含一个整数 。
每组数据输出 。
| 输入 |
|---|
| 10 1 2 3 4 5 6 7 8 9 10 |
| 输出 |
| 1 2 4 6 9 13 17 21 26 32 |
先打出前 个 的表:
首先对于任意两个相邻且不相等的数字,它们的差都为 。我将除了第 个数字和第 个数字的连续相同的数字统计个数以及个数的前缀和打表:
把 中每个数字第一次出现的位置打上标记,可以发现这是一个 的循环,对于每一个数字 ,我们首先可以找到小于等于它的最大的 的 值, 个数字其对应的 的值只增加了 个数字,将 加上 , 减去 ,不断查找下去,就可以在 次内确定 的值。而从 到 的所有数字的前缀和,通过观察可以发现,如果 是某段连续相同数字的最后一项,可以先计算 的等差数列求和,再计算 等差数列求和……每次都计算一个首项为 ,末项为 ,项数为 ,的一个等差数列,直到 ,停止计算,就能得到 到 的所有数字的前缀和;而如果 不是某段连续相同数字的最后一项,就要先计算出 的前一个连续的相同数字的最后一项,按照上述方法计算后,加上 这段连续的数字中, 前面的数字个数。由于前面的规律是在去掉第 个数字找到的,所以计算最后答案的时候要 。
最后注意答案会乘爆long long,所以在乘法之前应该先取模一次。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cfloat>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <algorithm>using namespace std;#define LL long longconst LL MOD = 1000000007;const LL inv = 500000004;int T;LL n;LL two[64], two_1[64];LL *it;LL get_a(LL n) {if(n == 1) {return 0;}--n;LL ret = 0;while(n != 0) {it = upper_bound(two_1, two_1 + 62, n);--it;ret += two[it - two_1 - 1];n -= *it;}return ret;}LL solve(LL d, LL n) {return (((d + (n / d * d % MOD)) % MOD) * ((n / d) % MOD) % MOD) * inv % MOD;}int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);two[0] = 1;for(int i = 1; i < 62; ++i) {two[i] = two[i - 1] * 2;}for(int i = 0; i < 62; ++i) {two_1[i] = two[i] - 1;}cin >> T;while(T--) {cin >> n;if(n == 1) {cout << 1 << endl;continue;}LL a_n = get_a(n);LL n_1 = n;LL a_n_1;do {--n_1;a_n_1 = get_a(n_1);} while(a_n_1 == a_n);LL ans = 0;for(int i = 0; two[i] <= a_n_1; ++i) {ans += solve(two[i], a_n_1);ans %= MOD;}ans = (ans + (a_n % MOD) * (n - n_1) % MOD) % MOD;ans = (ans + 1) % MOD;cout << ans << endl;}return 0;}
首先给定一个长度为 的整数序列 ,定义 表示查找区间 内的最大值的下标,如果存在多个最大值,则取最左边的数字的下标。现在要构造一个长度为 的序列 ,要求每个数字都是属于区间 内的实数,且对于每一个 ,其询问结果都和 相同,问所有满足条件的序列 的 的期望值。
第一行为一个整数 ,接下去有 组数据,每组数据第一行为一个整数 ,第二行为 个整数 ,数据保证所有 的和不超过 。
输出期望对 取模的结果,如果期望无法被 整除,先化简期望为 ,保证 和 互质且都为正整数的情况下,输出 的值,要求 能够被 整除。
| 输入 |
|---|
| 3 3 1 2 3 3 1 2 1 5 1 2 3 2 1 |
| 输出 |
| 250000002 500000004 125000001 |
根据题意建立一棵序列 的笛卡尔树,题目即求笛卡尔树与数列 同构的序列 的序列和的期望,由于 内每个数字都是实数,所以任意两个数字相等的概率趋近于 ,对于每一个节点对应的区间,节点的数字大于该区间内所有数字的概率为 ,其中 为节点 对应子树的节点数,将所有节点的满足条件的概率相乘,再乘上序列求和的期望值 ,就是答案。最后要求期望对 的取模的结果,就是将结果 转化成 。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cfloat>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <algorithm>using namespace std;#define LL long longconst int maxn = 1000000 + 100;const LL MOD = 1000000007;LL ans;LL inv[maxn];struct Tree {int root, top, n;int sta[maxn], l[maxn], r[maxn];bool vis[maxn];void build(int *num, int nn) {n = nn;top = 0;memset(l, 0, sizeof(int) * (n + 1));memset(r, 0, sizeof(int) * (n + 1));memset(vis, 0, sizeof(bool) * (n + 1));for(int i = 1; i <= n; ++i) {int tmp = top;while(top > 0 && num[sta[top - 1]] < num[i]) {--top;}if(top != 0) {r[sta[top - 1]] = i;}if(top < tmp) {l[i] = sta[top];}sta[top++] = i;}for(int i = 1; i <= n; ++i) {vis[l[i]] = vis[r[i]] = true;}for(int i = 1; i <= n; ++i) {if(!vis[i]) {root = i;}}}int dfs(int x) {int cnt = 1;if(l[x] != 0) {cnt += dfs(l[x]);}if(r[x] != 0) {cnt += dfs(r[x]);}ans = (ans * inv[cnt]) % MOD;return cnt;}};int T, n;int num[maxn];Tree t;void Init() {inv[1] = 1;for(int i = 2; i < maxn; ++i) {inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;}}int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);Init();scanf("%d", &T);while(T--) {scanf("%d", &n);ans = (n * inv[2]) % MOD;for(int i = 1; i <= n; ++i) {scanf("%d", &num[i]);}t.build(num, n);t.dfs(t.root);printf("%I64d\n", ans);}return 0;}
给出北京时间,要求计算其他时间。
第一行为一个整数 ,接下去 行每行前两个数字为 ,表示北京时间,接着是一个格式为
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 |
将所有数字都转化为分钟后按照题意模拟,注意精度误差,不要读入小数。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>#include <algorithm>#include <sstream>using namespace std;#define LL long longint T;int h, m, a, b;char ch;int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);scanf("%d", &T);while(T--) {b = 0;scanf("%d %d UTC%c%d.%d", &h, &m, &ch, &a, &b);m += h * 60;b = b * 6 + a * 60;if(ch == '-') {b *= -1;}m += b - 480;m = (m % (24 * 60) + 24 * 60) % (24 * 60);h = m / 60;m = m % 60;printf("%02d:%02d\n", h, m);}return 0;}