@Dmaxiya
2018-08-20T01:27:30.000000Z
字数 3916
阅读 1167
Codeforces
Contests 链接:Educational Codeforces Round 44
过题数:3
排名:833/10094
有一个 的棋盘, 为偶数,棋盘上的颜色是黑白相间的,在棋盘上放 个棋子,要求用最少的步数,使得所有棋子在的格子的颜色是一样的,每一步可以将任意一个棋子往左或者往右移动一位,棋子移动不能越过其他棋子。
第一行为一个整数 , 保证为偶数,第二行为 个整数 ,表示每个棋子放的位置。
输出最小的步数。
| 输入 |
|---|
| 6 1 2 6 |
| 输出 |
| 2 |
| 提示 |
| 最优的移动方式是将 移动到 ,将 移动到 。 |
| 输入 |
|---|
| 10 1 2 3 4 5 |
| 输出 |
| 10 |
| 提示 |
| 最优的移动方式是 ,总共需要 步。 |
枚举所有点都到偶数位上需要的最少步数和所有点都到奇数位上需要的最少步数,取最小值。
#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 <functional>#include <iomanip>using namespace std;#define LL long longconst int maxn = 100 + 100;int n;int num[maxn];int get(int x) {int Index = 0;int ret = 0;for(int i = x; i <= n; i+= 2) {ret += abs(num[Index] - i);++Index;}return ret;}int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);// freopen("test1.out", "w", stdout);#endif // LOCALios::sync_with_stdio(false);while(scanf("%d", &n) != EOF) {for(int i = 0; i < n / 2; ++i) {scanf("%d", &num[i]);}sort(num, num + n / 2);printf("%d\n", min(get(1), get(2)));}return 0;}
有 个开关和 盏灯,每个开关可以控制几个灯,这些开关只能将所控制的灯变亮,问能否从中去掉一个开关,仍能使得所有灯都亮起来。
第一行为两个整数 ,接下去为 行长度为 的 串,第 行第 列为 表示第 个开关能够让第 盏灯亮起来,为 表示该开关无法控制第 盏灯,数据保证如果打开 个开关,则 盏等一定会全都亮起来。
如果可以达到要求就输出 ,否则输出 。
| 输入 |
|---|
4 510101010000011110000 |
| 输出 |
| YES |
| 输入 |
|---|
4 510100010000011000101 |
| 输出 |
| NO |
先统计每盏灯被控制的开关数,暴力枚举每一个开关,如果某个开关控制的灯对应的开关数量小于等于 ,说明这盏灯被这个开关唯一控制,这个开关就不符合条件。只要存在一个开关满足条件,则输出 ,否则输出 。
#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 <functional>#include <iomanip>using namespace std;#define LL long longconst int maxn = 2000 + 100;int n, m;bool flag;char str[maxn][maxn];int num[maxn];int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);// freopen("test1.out", "w", stdout);#endif // LOCALios::sync_with_stdio(false);while(scanf("%d%d", &n, &m) != EOF) {flag = false;memset(num, 0, sizeof(num));for(int i = 1; i <= n; ++i) {scanf("%s", str[i] + 1);for(int j = 1; j <= m; ++j) {if(str[i][j] == '1') {++num[j];}}}for(int i = 1; i <= n; ++i) {bool f = true;for(int j = 1; j <= m; ++j) {if(str[i][j] == '1') {if(num[j] <= 1) {f = false;break;}}}if(f) {flag = true;break;}}if(flag) {printf("YES\n");} else {printf("NO\n");}}return 0;}
总共有 个木板,第 个木板的长度为 ,要用这 个木板做成 个桶,每个桶由 个木板组成,每个桶的容积由构成这个桶的最短木板决定,要求所有的桶的容积差距不能超过 ,问所有桶的最大容积和。
第一行为 个整数 ,第二行为 个整数 。
输出所有木桶的最大容积和,如果无法达到要求,则输出 。
| 输入 |
|---|
| 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 |
| 提示 |
| 无论如何造木桶,任意两个木桶之间最大差距的最小值为 ,无法达到要求。 |
由于最短的木板一定是其中某个木桶的容积,所以先将与最短的板长度差小于等于 的放到一起从小到大排序,每 个板构成一个桶,且保证剩下的板子足够构成 个桶,这样贪心下去,就能得到答案。
#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 <functional>#include <iomanip>using namespace std;#define LL long longconst int maxn = 100000 + 100;int n, k, l;LL ans;int *it;int num[maxn];priority_queue<int, vector<int>, greater<int> > que;int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);// freopen("test1.out", "w", stdout);#endif // LOCALios::sync_with_stdio(false);while(scanf("%d%d%d", &n, &k, &l) != EOF) {ans = 0;for(int i = 0; i < n * k; ++i) {scanf("%d", &num[i]);}sort(num, num + n * k);it = upper_bound(num, num + n * k, num[0] + l);int cnt = it - num;if(cnt < n) {printf("0\n");continue;}int Index = 0;int Chose = 0;int Left = cnt;while(Index < cnt) {ans += num[Index];++Chose;for(int i = 0; i < k; ++i) {++Index;--Left;if(n - Chose >= Left) {break;}}}printf("%I64d\n", ans);}return 0;}