[关闭]
@Dmaxiya 2018-08-17T10:20:08.000000Z 字数 3217 阅读 1048

Codeforces Round #452 (Div. 2)

Codeforces


Contests 链接:Codeforces Round #452 (Div. 2)
过题数:3
排名:802/8883

A. Splitting in Teams

题意

组人要组队,每队 个人,如果有一组人的人数为 ,那么这个人可以和任何人组队,如果一组人的人数为 ,那么这两个人要么在一组和别人组队,要么都不组队,问最多能组成多少个队。

输入

第一行为一个整数 ,第二行为 个整数 ,表示每组的人数。

输出

输出最多能组多少个三人队。

样例

输入 输出 提示
4
1 1 2 1
1 可以让第 组的人组成一队。
2
2 2
0 这两组人无法组队。
7
2 2 2 1 1 1 1
3 可以将第 组和第 组人组队,第 组和第 组人组队,
组和第 组组队。
3
1 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 n, num;
  19. int cnt[3];
  20. int main() {
  21. #ifdef Dmaxiya
  22. freopen("test.txt", "r", stdin);
  23. #endif // Dmaxiya
  24. ios::sync_with_stdio(false);
  25. while(scanf("%d", &n) != EOF) {
  26. memset(cnt, 0, sizeof(cnt));
  27. for(int i = 0; i < n; ++i) {
  28. scanf("%d", &num);
  29. ++cnt[num];
  30. }
  31. int ans = min(cnt[2], cnt[1]);
  32. ans += (cnt[1] - ans) / 3;
  33. printf("%d\n", ans);
  34. }
  35. return 0;
  36. }

B. Months and Years

题意

给出 个数字,判这 个数字是否等于某几个连续的月份的天数,按公历计算。

输入

第一行为一个整数 ,第二行为 个整数

输出

如果是,则输出 ,否则输出

样例

输入 输出 提示
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 月、 月和平年 月就是这样的。

题解

先预处理出八年连续的月份天数,然后暴力地去匹配天数。

过题代码

  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 = 200;
  19. int n;
  20. int num[maxn];
  21. int month[maxn] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  22. int Month[maxn];
  23. void Copy(int Index) {
  24. for(int i = 0; i < 12; ++i) {
  25. Month[Index + i] = month[i];
  26. }
  27. }
  28. int main() {
  29. #ifdef Dmaxiya
  30. freopen("test.txt", "r", stdin);
  31. #endif // Dmaxiya
  32. ios::sync_with_stdio(false);
  33. for(int i = 0; i < 8; ++i) {
  34. Copy(i * 12);
  35. if(i % 4 == 3) {
  36. Month[i * 12 + 1] = 29;
  37. }
  38. }
  39. while(scanf("%d", &n) != EOF) {
  40. bool flag = false;
  41. for(int i = 0; i < n; ++i) {
  42. scanf("%d", &num[i]);
  43. }
  44. for(int i = 0; i + n - 1 < 96; ++i) {
  45. bool f = true;
  46. for(int j = 0; i + j < 96 && j < n; ++j) {
  47. if(Month[i + j] != num[j]) {
  48. f = false;
  49. break;
  50. }
  51. }
  52. if(f) {
  53. flag = true;
  54. break;
  55. }
  56. }
  57. printf("%s\n", flag? "Yes": "No");
  58. }
  59. return 0;
  60. }

C. Dividing the numbers

题意

总共 个自然数分成非空的 部分,要使得两部分各自数字的和相差最小,输出分配方案。

输入

输入只包含一个整数

输出

第一行输出最小的差值,第二行第一个数字表示其中一部分的数字个数 ,接下去 个数字,表示被分到同一组的数字,可以按任意顺序输出,如果有多解输出任意一组。

样例

输入 输出 提示
4 0
2 1 4
放在同一组,另一组为 ,这样两组的和都是 ,它们的差值为
2 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. const int maxn = 60000 + 100;
  19. int n;
  20. void Print(int l, int r) {
  21. while(r > l) {
  22. printf(" %d %d", l, r);
  23. l += 2;
  24. r -= 2;
  25. }
  26. printf("\n");
  27. }
  28. int main() {
  29. #ifdef Dmaxiya
  30. freopen("test.txt", "r", stdin);
  31. #endif // Dmaxiya
  32. ios::sync_with_stdio(false);
  33. while(scanf("%d", &n) != EOF) {
  34. if(n % 4 == 0 || n % 4 == 3) {
  35. printf("0\n");
  36. if(n % 4 == 0) {
  37. printf("%d", n / 4 * 2);
  38. } else {
  39. printf("%d 3", n / 4 * 2 + 1);
  40. }
  41. } else {
  42. printf("1\n%d 1", n / 4 * 2 + 1);
  43. }
  44. Print(n % 4 + 1, n);
  45. }
  46. return 0;
  47. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注