@Dmaxiya
2018-08-17T02:21:09.000000Z
字数 4766
阅读 1241
Codeforces
Contests 链接:Codeforces Round #446 (Div. 2)
过题数:3
排名:798/10599
有 个可乐罐,第 个可乐罐的容量为 ,其中剩余的可乐体积为 ,问能否将所有可乐罐中剩余的可乐都倒到两个可乐罐中。
第一行为一个整数 ,第二行为 个整数 ,第三行为 个整数 。
如果可以则输出 ,否则输出 。
| 输入 | 输出 | 提示 |
|---|---|---|
| 2 3 5 3 6 |
YES | 由于本来就已经有两个罐子,所以一定可以将所有可乐都倒到两个罐子中。 |
| 3 6 8 9 6 10 12 |
NO | |
| 5 0 0 5 0 0 1 1 8 10 5 |
YES | |
| 4 4 1 0 3 5 2 2 3 |
YES |
判断所有剩余可乐的体积 能否放入体积最大的两个罐子中即可。
#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;int n;LL a, sum;LL num[maxn];int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);while(scanf("%d", &n) != EOF) {sum = 0;for(int i = 0; i < n; ++i) {scanf("%I64d", &a);sum += a;}for(int i = 0; i < n; ++i) {scanf("%I64d", &num[i]);}sort(num, num + n);if(sum > num[n - 1] + num[n - 2]) {printf("NO\n");} else {printf("YES\n");}}return 0;}
有 个人站成一排,第 个人手里有一个长度为 的钩子,如果第 个人满足条件 且 ,那么这个人就会被杀死,铃声一响,所有人同时抛出钩子杀死前面的人,问最终有多少人存活。
第一行为一个整数 ,第二行为 个整数 。
在铃声响后,最终有多少人存活。
| 输入 | 输出 | 提示 |
|---|---|---|
| 4 0 1 0 10 |
1 | 最后一个人杀死了在他前面的所有人。 |
| 2 0 0 |
2 | |
| 10 1 1 3 0 0 0 2 1 0 3 |
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 = 1000000 + 100;int n, Min, ans;int num[maxn];int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);while(scanf("%d", &n) != EOF) {ans = 0;for(int i = 1; i <= n; ++i) {scanf("%d", &num[i]);}Min = n + 1;for(int i = n; i > 0; --i) {if(i < Min) {++ans;}Min = min(Min, i - num[i]);}printf("%d\n", ans);}return 0;}
对于一个长度为 的序列,可以选择其中两个相邻的数字 ,并将它们中的其中一个用 进行替换,问至少要经过多少次操作,才能使得序列中所有数字都为 。
第一行为一个整数 ,第二行为 个整数 。
输出最少的操作次数,如果永远都无法达到目标,则输出 。
| 输入 | 输出 | 提示 |
|---|---|---|
| 5 2 2 3 4 6 |
5 | 可以将序列经过以下变换达到目标: |
| 4 2 4 6 8 |
-1 | |
| 3 2 6 9 |
4 |
首先取一个最短的区间,这个区间的最大公约数为 ,通过这个长度为 的区间得到一个 需要的操作次数为 ,然后再利用这个 与其他数字进行最大公约数的替换操作,操作次数等于除这个数字外,其他所有不等于 的数字的个数,可以由此得到答案。如果整个序列的最大公约数不为 ,则输出 。
#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 = 2000 + 100;int n, Index, ans;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", &n) != EOF) {ans = n + 1;Index = n + 1;for(int i = 1; i <= n; ++i) {scanf("%d", &num[i]);}for(int i = 1; i <= n; ++i) {int Gcd = num[i];if(Gcd == 1) {Index = i;ans = 0;break;}for(int j = i + 1; j <= n; ++j) {Gcd = gcd(Gcd, num[j]);if(Gcd == 1) {if(ans > j - i) {ans = j - i;Index = j;}break;}}}if(ans == n + 1) {printf("-1\n");continue;}for(int i = 1; i <= n; ++i) {if(num[i] != 1 && i != Index) {++ans;}}printf("%d\n", ans);}return 0;}
给定 个互不相等的数字 ,将 个数字重新生成一个排列 后,要求对于下标的任意一个非空真子集 ,都满足条件 。
第一行为一个正整数 ,第二行为 个整数 。
如果可以构造出满足条件的序列 ,则按顺序输出 序列中的每一个元素,否则输出 ,有多解则输出任意一个。
| 输入 | 输出 |
|---|---|
| 2 1 2 |
2 1 |
| 4 1000 100 10 1 |
100 1 1000 10 |
首先考虑一个更简单的问题,对于一个有序的序列 ,如果我们将序列中的每个位置上的数字都往右移动一位,第 个数字移动到第 位,生成的新的序列为 ,首先考虑真子集不包含 的情况,则可以发现对于任意一个集合,原序列中的数字都大于新序列中的数字,而如果考虑 的情况,由于 对两个序列都是相等的,所以包含下标为 的情况的大小关系与不包含 的情况正好相反,原序列中的任意一个真子集数字的和都小于新序列中的数字和,因此不论如何总是可以满足题给条件。
对于乱序的序列 ,只要用一个结构体标记一下下标即可。
#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 = 50;struct Node {int num, Index;};bool operator<(const Node &a, const Node &b) {return a.num < b.num;}int n;int num[maxn];Node node[maxn];int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);while(scanf("%d", &n) != EOF) {for(int i = 0; i < n; ++i) {scanf("%d", &num[i]);node[i].Index = i;node[i].num = num[i];}sort(node, node + n);for(int i = 1; i < n; ++i) {swap(node[i].Index, node[0].Index);}for(int i = 0; i < n; ++i) {num[node[i].Index] = node[i].num;}for(int i = 0; i < n; ++i) {if(i != 0) {printf(" ");}printf("%d", num[i]);}printf("\n");}return 0;}