@Dmaxiya
2018-08-17T02:20:08.000000Z
字数 3217
阅读 1321
Codeforces
Contests 链接:Codeforces Round #452 (Div. 2)
过题数:3
排名:802/8883
有 组人要组队,每队 个人,如果有一组人的人数为 ,那么这个人可以和任何人组队,如果一组人的人数为 ,那么这两个人要么在一组和别人组队,要么都不组队,问最多能组成多少个队。
第一行为一个整数 ,第二行为 个整数 ,表示每组的人数。
输出最多能组多少个三人队。
| 输入 | 输出 | 提示 |
|---|---|---|
| 4 1 1 2 1 |
1 | 可以让第 组的人组成一队。 |
| 2 2 2 |
0 | 这两组人无法组队。 |
| 7 2 2 2 1 1 1 1 |
3 | 可以将第 组和第 组人组队,第 组和第 组人组队, 第 组和第 组组队。 |
| 3 1 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 n, num;int cnt[3];int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);while(scanf("%d", &n) != EOF) {memset(cnt, 0, sizeof(cnt));for(int i = 0; i < n; ++i) {scanf("%d", &num);++cnt[num];}int ans = min(cnt[2], cnt[1]);ans += (cnt[1] - ans) / 3;printf("%d\n", ans);}return 0;}
给出 个数字,判这 个数字是否等于某几个连续的月份的天数,按公历计算。
第一行为一个整数 ,第二行为 个整数 。
如果是,则输出 ,否则输出 。
| 输入 | 输出 | 提示 |
|---|---|---|
| 4 31 31 30 31 |
Yes | 第 月份的天数就是这样的。 |
| 2 30 30 |
No | 由于没有任何两个连续的月份的天数都为 ,所以应该输出 。 |
| 5 29 31 30 31 30 |
Yes | 闰年的 月份的天数就是这样的。 |
| 3 31 28 30 |
No | 是平年的 月份,所以接下去应该是 月份的 天,而数据为 ,所以不合法。 |
| 3 31 31 28 |
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 = 200;int n;int num[maxn];int month[maxn] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};int Month[maxn];void Copy(int Index) {for(int i = 0; i < 12; ++i) {Month[Index + i] = month[i];}}int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);for(int i = 0; i < 8; ++i) {Copy(i * 12);if(i % 4 == 3) {Month[i * 12 + 1] = 29;}}while(scanf("%d", &n) != EOF) {bool flag = false;for(int i = 0; i < n; ++i) {scanf("%d", &num[i]);}for(int i = 0; i + n - 1 < 96; ++i) {bool f = true;for(int j = 0; i + j < 96 && j < n; ++j) {if(Month[i + j] != num[j]) {f = false;break;}}if(f) {flag = true;break;}}printf("%s\n", flag? "Yes": "No");}return 0;}
将 到 总共 个自然数分成非空的 部分,要使得两部分各自数字的和相差最小,输出分配方案。
输入只包含一个整数 。
第一行输出最小的差值,第二行第一个数字表示其中一部分的数字个数 ,接下去 个数字,表示被分到同一组的数字,可以按任意顺序输出,如果有多解输出任意一组。
| 输入 | 输出 | 提示 |
|---|---|---|
| 4 | 0 2 1 4 |
将 和 放在同一组,另一组为 和 ,这样两组的和都是 ,它们的差值为 。 |
| 2 | 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 longconst int maxn = 60000 + 100;int n;void Print(int l, int r) {while(r > l) {printf(" %d %d", l, r);l += 2;r -= 2;}printf("\n");}int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);while(scanf("%d", &n) != EOF) {if(n % 4 == 0 || n % 4 == 3) {printf("0\n");if(n % 4 == 0) {printf("%d", n / 4 * 2);} else {printf("%d 3", n / 4 * 2 + 1);}} else {printf("1\n%d 1", n / 4 * 2 + 1);}Print(n % 4 + 1, n);}return 0;}