@Dmaxiya
2018-08-17T02:17:46.000000Z
字数 9164
阅读 1209
Codeforces
Contests 链接:Codeforces Round #499 (Div. 2)
过题数:5
排名:145/7830
给出 个小写字母,从中选出 个小写字母构成一个字符串,要求构成的字符串中 ,要使字符串所有字符的权值和最小,输出最小权值和,每个字符对应的权值大小为 。
第一行为两个整数 ,第二行为一个长度为 的字符串,字符串只包含小写字母。
输出构成的字符串的最小权值,如果无法构成满足条件的字符串,输出 。
| 输入 | 输出 | 提示 |
|---|---|---|
| 5 3 xyabd |
29 | 以下为所有合法的字符串以及对应的权值: "adx"(权值为 ); "ady"(权值为 ); "bdx"(权值为 ); "bdy"(权值为 )。 "adx" 是权值最小的字符串,权值为 。 |
| 7 4 problem |
34 | 最优的字符串 "belo" 的权值为 。 |
| 2 2 ab |
-1 | 和 的值都为 ,所以只能将 和 都选上,但是 "ab" 并不是满足条件的字符串,因此应该输出 。 |
| 12 1 abaabbaaabbb |
1 |
贪心按从小到大找出满足条件的字符串。
#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 longconst int maxn = 100;int n, k, cnt;char str[maxn], ans[maxn];int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);while(scanf("%d%d%s", &n, &k, str) != EOF) {cnt = 0;int len = strlen(str);sort(str, str + len);ans[++cnt] = str[0];for(int i = 1; str[i]; ++i) {if(str[i] - ans[cnt] > 1) {ans[++cnt] = str[i];}}if(cnt < k) {printf("-1\n");continue;}int sum = 0;for(int i = 1; i <= k; ++i) {sum += ans[i] - 'a' + 1;}printf("%d\n", sum);}return 0;}
有 个人和 袋食物,第 个袋子里装的食物种类是 ,每个人每天要吃一袋食物,且他们必须从头到尾都只吃同一种食物。问这些食物最多能够所有人吃多少天。
第一行包含两个整数 ,第二行包含 个整数 。
输出最多的天数。
| 输入 | 输出 | 提示 |
|---|---|---|
| 4 10 1 5 2 1 1 1 2 5 7 2 |
2 | 可以将第 种食物分配给第 个人,第 种食物分配给第 个人, 第 种食物分配给第 个人。 |
| 100 1 1 |
0 | 总共有 个人而只有 袋食物,他们连一天都撑不过。 |
| 2 5 5 4 3 2 1 |
1 | |
| 3 9 42 42 42 42 42 42 42 42 42 |
3 |
暴力从 到 枚举所有人能够撑过的天数,对于每一个 ,贪心检查他们是否能够撑过这些天,如果他们能够撑过 天,那么每个人都给他们分 袋一个种类的食物,看最后食物是否够分。
#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 longconst int maxn = 100 + 100;int n, m, ans, x;int num[maxn], tmp[maxn];bool judge(int x) {int Index = 1;memcpy(tmp, num, sizeof(num));for(int i = 1; i <= n; ++i) {while(Index <= 100 && tmp[Index] < x) {++Index;}if(Index > 100) {return false;}tmp[Index] -= x;}return true;}int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);while(scanf("%d%d", &n, &m) != EOF) {memset(num, 0, sizeof(num));for(int i = 1; i <= m; ++i) {scanf("%d", &x);++num[x];}for(int i = m; i >= 0; --i) {if(judge(i)) {ans = i;break;}}printf("%d\n", ans);}return 0;}
一个火箭要从第 个星球经过 个星球后回到第 个星球,每到达一个星球都要经历降落与升空的过程,由于每个星球的引力不同,在每个星球降落与升空的过程中需要消耗的动力也不同。
火箭自身有一个质量 ,如果到达第 个星球时的油的质量为 吨,且在降落/升空过程中每吨重量需要消耗的油量为 吨,则在这个过程中需要消耗的总油量为 。在第 个星球升空时每吨重量需要消耗的油量为 吨,降落时每吨重量消耗的油量为 吨,问要完成整个任务所需要的最少油量为多少。
前两行为两个整数 ,第三行为 个整数 ,第四行为 个整数 ,其中 。
输出完成任务需要的最少油量,如果无法完成任务,则输出 ,误差在 以内都认为答案正确。
| 输入 | 输出 | 提示 |
|---|---|---|
| 2 12 11 8 7 5 |
10.0000000000 | 火箭的初始质量为 : 离开第 个星球需要的油的质量为 吨,剩下 吨总质量; 在第 个星球降落时的总质量为 ,需要的油的质量为 吨,剩下 吨总质量; 离开第 个星球需要的油的质量为 吨,剩下 吨总质量; 在第 个星球降落需要的油的质量为 吨,剩下 吨总质量,油刚好用完。 |
| 3 1 1 4 1 2 5 3 |
-1 | 火箭永远都无法离开第 个星球。 |
| 6 2 4 6 3 3 5 6 2 6 3 6 5 3 |
85.4800000000 |
中只要存在 个 ,就永远无法完成任务,否则可以二分初始油量,每次 判断油量是否会在中途消耗完,为减少精度误差,二分 次。
#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 longconst int maxn = 1000 + 100;bool flag;int n, m;int a[maxn], b[maxn];bool judge(double mid) {for(int i = 1; i <= n; ++i) {int ii = (i + 1) % n;if(ii == 0) {ii = n;}mid -= (m + mid) / a[i];if(mid < 0) {return false;}mid -= (m + mid) / b[ii];if(mid < 0) {return false;}}return true;}int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);while(scanf("%d%d", &n, &m) != EOF) {flag = true;for(int i = 1; i <= n; ++i) {scanf("%d", &a[i]);if(a[i] == 1) {flag = false;}}for(int i = 1; i <= n; ++i) {scanf("%d", &b[i]);if(b[i] == 1) {flag = false;}}if(!flag) {printf("-1\n");continue;}double low = 0;double high = 1e9;double mid;for(int i = 0; i < 500; ++i) {mid = (low + high) / 2;if(judge(mid)) {high = mid;} else {low = mid;}}printf("%.10f\n", high);}return 0;}
和机器人玩一个猜数字的游戏,要被猜中的数字为 ,最开始机器人会告诉 两个整数 ,表示 , 每次询问一个整数 ,如果 ,正确的回复应该是 ,如果 ,正确的回复应该是 ,如果 ,正确的回复应该是 ,机器人自己有一个长度为 的 序列, 不知道这个序列是什么样的,机器人在 的第 次询问中( 从 开始计数),如果序列中的第 个数字为 ,则机器人会回复正确的值 ,否则会回复 。要求 在 次询问内猜中 。
第一次输入为两个整数 ,之后的输入根据题给规则给出。
每次输出一个整数 ,要求在 次输出内得到 的值,且程序必须在得到 的回复时立即退出。
| 输入 | 输出 | 提示 |
|---|---|---|
| 5 2 1 -1 -1 1 0 |
1 2 4 5 3 |
机器人自身的序列为 ,最终的答案为 在第 次询问中 ,此时 ,正确回复为 ,序列中的第 个数字为 ,因此机器人最终的回复为 ; 在第 次询问中 ,正确的回复为 ,序列中第 个数字为 ,因此机器人最终的回复为 ; 在第 次询问中 ,正确的回复为 ,序列中的第 个数字为 ,因此机器人最终的回复为 ; 在第 次询问中 ,正确的回复为 ,序列中第 个数字为 ,因此机器人最终的回复为 。 |
先输出 次 ,如果 则可以立即得到结果,否则由于所有可能的 一定都大于 ,就可以根据得到的 和 的序列得到机器人的 序列,再进行 次的二分询问,就可以得到答案。
#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 longconst int maxn = 100 + 100;int n, m, x;int num[maxn];int main() {#ifdef Dmaxiya// freopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);scanf("%d%d", &m, &n);printf("1\n");fflush(stdout);scanf("%d", &x);if(x == 0) {return 0;}num[0] = x;for(int i = 1; i < n; ++i) {printf("1\n");fflush(stdout);scanf("%d", &num[i]);}int low = 1;int high = m + 1;int mid;int Index = 0;while(high - low > 1) {mid = (low + high) / 2;printf("%d\n", mid);fflush(stdout);scanf("%d", &x);if(x == 0) {return 0;}if(num[Index] == -1) {x = -x;}if(x == 1) {low = mid;} else {high = mid;}Index = (Index + 1) % n;}return 0;}
给定 个整数 以及 的值,求 的所有取值,其中 为任意非负整数且 。
第一行为两个整数 ,第二行为 个整数 。
第一行输出一个整数 表示所有取值的数量,第二行按从小到大的顺序输出所有 的值。
| 输入 | 输出 | 提示 |
|---|---|---|
| 2 8 12 20 |
2 0 4 |
; ; ; 即使有其他的系数 使 得到其他的值,但是所有其他值对 取模后的值都为 或者 。 |
| 3 10 10 20 30 |
1 0 |
三个数字都是 的倍数,因此无论如何组合它们的和都是 的倍数。 |
对于任意一个数字 ,其 的值一定是 的倍数,两个数字 任意组合再对 取模的值一定是 ,以此类推,只要得到所有数字与 的最大公约数即可。
#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 longconst int maxn = 100000 + 100;int n, k;int num[maxn];int gcd(int a, int b) {return (b == 0? a: gcd(b, a % b));}int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);while(scanf("%d%d", &n, &k) != EOF) {int g = k;for(int i = 0; i < n; ++i) {scanf("%d", &num[i]);g = gcd(g, num[i]);}printf("%d\n", k / g);for(int i = 0; i < k; i += g) {if(i != 0) {printf(" ");}printf("%d", i);}printf("\n");}return 0;}
给出一个由 个元器件组成的电路,每个元器件的功能只可能是“输入”、“与运算”、“或运算”、“非运算”、“异或运算”中的一个,整个电路是一个树形结构,只有一个输出,如果对某个输入取反,则可能会改变最终的输出,问对于每一个输入,如果仅将这个输入取反,得到的最终输出的结果。
第一行包含一个整数 ,接下去 行,第 行的第一个字符串表示元器件类型:"IN"、"AND"、"OR"、"XOR"、"NOT",其中 "AND"、"OR"、"XOR" 后面跟两个数字,表示这个元器件的两个输入元器件编号,"NOT" 后面跟一个数字,表示这个元器件的一个输入元器件的编号,"IN" 后面跟着 或者 ,表示这个输入元器件的初始输入值。数据保证最终输出元器件编号为 。
按元器件编号从小到大的顺序输出每一个输入元器件取反后输出的值,每一个输入元器件取反是相互独立的,即每个输出都是在仅改变一个输入元器件的值的情况下的输出。
| 输入 | 输出 | 提示 |
|---|---|---|
| 10 AND 9 4 IN 1 IN 1 XOR 6 5 AND 3 7 IN 0 NOT 10 IN 1 IN 1 AND 2 8 |
10110 | 对应的元器件结构如下图:![]() 其中绿色元器件表示这个元器件的计算结果为 ,黄色表示值为 : 如果改变输入 的值为 ,则输出为 ; 如果改变输入 的值为 ,则输出的值为 ; 如果改变输入 的值为 ,则输出的值为 ; 如果改变输入 的值为 ,则输出的值为 ; 如果改变输入 的值为 ,则输出的值为 。 |
由于每修改一个输入元器件的值,都只会修改树上一条路径上的值,与其他路径无关,所以可以预处理第 个元器件的计算结果修改为 且其他元器件的计算结果不变时,最终输出的值,两次 就可以得到结果。
#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 unsigned long longconst int maxn = 1000000 + 100;int n, l, r;char str[maxn][10];int num[maxn], Y[maxn][2];vector<int> G[maxn];void dfs1(int x) {int len = G[x].size();for(int i = 0; i < len; ++i) {dfs1(G[x][i]);}switch(str[x][0]) {case 'A': num[x] = num[G[x][0]] & num[G[x][1]]; break;case 'O': num[x] = num[G[x][0]] | num[G[x][1]]; break;case 'X': num[x] = num[G[x][0]] ^ num[G[x][1]]; break;case 'N': num[x] = 1 - num[G[x][0]]; break;default: break;}}void dfs2(int x) {int len = G[x].size();for(int i = 0; i < len; ++i) {int xx = 1 - num[G[x][i]];switch(str[x][0]) {case 'A': Y[G[x][i]][xx] = Y[x][xx & num[G[x][1 - i]]]; break;case 'O': Y[G[x][i]][xx] = Y[x][xx | num[G[x][1 - i]]]; break;case 'X': Y[G[x][i]][xx] = Y[x][xx ^ num[G[x][1 - i]]]; break;case 'N': Y[G[x][i]][xx] = Y[x][1 - xx]; break;default: break;}dfs2(G[x][i]);}}int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);while(scanf("%d", &n) != EOF) {for(int i = 1; i <= n; ++i) {num[i] = -1;G[i].clear();scanf("%s", str[i]);if(str[i][0] == 'I') {scanf("%d", &num[i]);} else if(str[i][0] == 'A' || str[i][0] == 'O' || str[i][0] == 'X') {scanf("%d%d", &l, &r);G[i].push_back(l);G[i].push_back(r);} else if(str[i][0] == 'N') {scanf("%d", &l);G[i].push_back(l);}}dfs1(1);for(int i = 1; i <= n; ++i) {Y[i][num[i]] = num[1];}Y[1][1 - num[1]] = 1 - num[1];dfs2(1);for(int i = 1; i <= n; ++i) {if(str[i][0] == 'I') {printf("%d", Y[i][1 - num[i]]);}}printf("\n");}return 0;}