[关闭]
@Dmaxiya 2018-08-20T09:27:30.000000Z 字数 3916 阅读 908

Educational Codeforces Round 44

Codeforces


Contests 链接:Educational Codeforces Round 44
过题数:3
排名:833/10094

A. Chess Placing

题意

有一个 的棋盘, 为偶数,棋盘上的颜色是黑白相间的,在棋盘上放 个棋子,要求用最少的步数,使得所有棋子在的格子的颜色是一样的,每一步可以将任意一个棋子往左或者往右移动一位,棋子移动不能越过其他棋子。

输入

第一行为一个整数 保证为偶数,第二行为 个整数 ,表示每个棋子放的位置。

输出

输出最小的步数。

样例

输入
6
1 2 6
输出
2
提示
最优的移动方式是将 移动到 ,将 移动到
输入
10
1 2 3 4 5
输出
10
提示
最优的移动方式是 ,总共需要 步。

题解

枚举所有点都到偶数位上需要的最少步数和所有点都到奇数位上需要的最少步数,取最小值。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. const int maxn = 100 + 100;
  21. int n;
  22. int num[maxn];
  23. int get(int x) {
  24. int Index = 0;
  25. int ret = 0;
  26. for(int i = x; i <= n; i+= 2) {
  27. ret += abs(num[Index] - i);
  28. ++Index;
  29. }
  30. return ret;
  31. }
  32. int main() {
  33. #ifdef LOCAL
  34. freopen("test.txt", "r", stdin);
  35. // freopen("test1.out", "w", stdout);
  36. #endif // LOCAL
  37. ios::sync_with_stdio(false);
  38. while(scanf("%d", &n) != EOF) {
  39. for(int i = 0; i < n / 2; ++i) {
  40. scanf("%d", &num[i]);
  41. }
  42. sort(num, num + n / 2);
  43. printf("%d\n", min(get(1), get(2)));
  44. }
  45. return 0;
  46. }

B. Switches and Lamps

题意

个开关和 盏灯,每个开关可以控制几个灯,这些开关只能将所控制的灯变亮,问能否从中去掉一个开关,仍能使得所有灯都亮起来。

输入

第一行为两个整数 ,接下去为 行长度为 串,第 行第 列为 表示第 个开关能够让第 盏灯亮起来,为 表示该开关无法控制第 盏灯,数据保证如果打开 个开关,则 盏等一定会全都亮起来。

输出

如果可以达到要求就输出 ,否则输出

样例

输入
4 5
10101
01000
00111
10000
输出
YES
输入
4 5
10100
01000
00110
00101
输出
NO

题解

先统计每盏灯被控制的开关数,暴力枚举每一个开关,如果某个开关控制的灯对应的开关数量小于等于 ,说明这盏灯被这个开关唯一控制,这个开关就不符合条件。只要存在一个开关满足条件,则输出 ,否则输出

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. const int maxn = 2000 + 100;
  21. int n, m;
  22. bool flag;
  23. char str[maxn][maxn];
  24. int num[maxn];
  25. int main() {
  26. #ifdef LOCAL
  27. freopen("test.txt", "r", stdin);
  28. // freopen("test1.out", "w", stdout);
  29. #endif // LOCAL
  30. ios::sync_with_stdio(false);
  31. while(scanf("%d%d", &n, &m) != EOF) {
  32. flag = false;
  33. memset(num, 0, sizeof(num));
  34. for(int i = 1; i <= n; ++i) {
  35. scanf("%s", str[i] + 1);
  36. for(int j = 1; j <= m; ++j) {
  37. if(str[i][j] == '1') {
  38. ++num[j];
  39. }
  40. }
  41. }
  42. for(int i = 1; i <= n; ++i) {
  43. bool f = true;
  44. for(int j = 1; j <= m; ++j) {
  45. if(str[i][j] == '1') {
  46. if(num[j] <= 1) {
  47. f = false;
  48. break;
  49. }
  50. }
  51. }
  52. if(f) {
  53. flag = true;
  54. break;
  55. }
  56. }
  57. if(flag) {
  58. printf("YES\n");
  59. } else {
  60. printf("NO\n");
  61. }
  62. }
  63. return 0;
  64. }

C. Liebig's Barrels

题意

总共有 个木板,第 个木板的长度为 ,要用这 个木板做成 个桶,每个桶由 个木板组成,每个桶的容积由构成这个桶的最短木板决定,要求所有的桶的容积差距不能超过 ,问所有桶的最大容积和。

输入

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

输出

输出所有木桶的最大容积和,如果无法达到要求,则输出

样例

输入
4 2 1
2 2 1 2 3 2 2 3
输出
7
提示
可以按照这样的方式来造木桶:
输入
2 1 0
10 10
输出
20
提示
可以按照这样的方式来造木桶:
输入
1 2 1
5 2
输出
2
提示
可以按照这样的方式来造木桶:
输入
3 2 1
1 2 3 4 5 6
输出
0
提示
无论如何造木桶,任意两个木桶之间最大差距的最小值为 ,无法达到要求。

题解

由于最短的木板一定是其中某个木桶的容积,所以先将与最短的板长度差小于等于 的放到一起从小到大排序,每 个板构成一个桶,且保证剩下的板子足够构成 个桶,这样贪心下去,就能得到答案。

过题代码

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <climits>
  6. #include <cstring>
  7. #include <string>
  8. #include <vector>
  9. #include <list>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <bitset>
  15. #include <algorithm>
  16. #include <functional>
  17. #include <iomanip>
  18. using namespace std;
  19. #define LL long long
  20. const int maxn = 100000 + 100;
  21. int n, k, l;
  22. LL ans;
  23. int *it;
  24. int num[maxn];
  25. priority_queue<int, vector<int>, greater<int> > que;
  26. int main() {
  27. #ifdef LOCAL
  28. freopen("test.txt", "r", stdin);
  29. // freopen("test1.out", "w", stdout);
  30. #endif // LOCAL
  31. ios::sync_with_stdio(false);
  32. while(scanf("%d%d%d", &n, &k, &l) != EOF) {
  33. ans = 0;
  34. for(int i = 0; i < n * k; ++i) {
  35. scanf("%d", &num[i]);
  36. }
  37. sort(num, num + n * k);
  38. it = upper_bound(num, num + n * k, num[0] + l);
  39. int cnt = it - num;
  40. if(cnt < n) {
  41. printf("0\n");
  42. continue;
  43. }
  44. int Index = 0;
  45. int Chose = 0;
  46. int Left = cnt;
  47. while(Index < cnt) {
  48. ans += num[Index];
  49. ++Chose;
  50. for(int i = 0; i < k; ++i) {
  51. ++Index;
  52. --Left;
  53. if(n - Chose >= Left) {
  54. break;
  55. }
  56. }
  57. }
  58. printf("%I64d\n", ans);
  59. }
  60. return 0;
  61. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注