Educational Codeforces Round 44

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

A. Chess Placing

题意

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

输入

第一行为一个整数 n (2n100) n   ( 2 ≤ n ≤ 100 ) n n 保证为偶数,第二行为 n2 个整数 p1,p2,,pn2 (1pin) p 1 , p 2 , ⋯ , p n 2   ( 1 ≤ p i ≤ n ) ,表示每个棋子放的位置。

输出

输出最小的步数。

样例

输入
6
1 2 6
输出
2
提示
最优的移动方式是将 6 6 移动到 5,将 2 2 移动到 3
输入
10
1 2 3 4 5
输出
10
提示
最优的移动方式是 59,47,35,23 5 → 9 , 4 → 7 , 3 → 5 , 2 → 3 ,总共需要 10 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 long
const 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 LOCAL
    freopen("test.txt", "r", stdin);
//    freopen("test1.out", "w", stdout);
    #endif // LOCAL
    ios::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;
}

B. Switches and Lamps

题意

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

输入

第一行为两个整数 n,m (1n,m2000) n , m   ( 1 ≤ n , m ≤ 2000 ) ,接下去为 n n 行长度为 m 01 01 串,第 i i 行第 j 列为 1 1 表示第 i 个开关能够让第 j j 盏灯亮起来,为 0 表示该开关无法控制第 j j 盏灯,数据保证如果打开 n 个开关,则 m m 盏等一定会全都亮起来。

输出

如果可以达到要求就输出 YES,否则输出 NO N O

样例

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

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

过题代码

#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 long
const int maxn = 2000 + 100;
int n, m;
bool flag;
char str[maxn][maxn];
int num[maxn];

int main() {
    #ifdef LOCAL
    freopen("test.txt", "r", stdin);
//    freopen("test1.out", "w", stdout);
    #endif // LOCAL
    ios::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;
}

C. Liebig’s Barrels

题意

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

输入

第一行为 3 3 个整数 n,k,l (1n,k105,1n×k105,0l109),第二行为 n×k n × k 个整数 a1,a2,,an×k (1ai109) a 1 , a 2 , ⋯ , a n × k   ( 1 ≤ a i ≤ 10 9 )

输出

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

样例

输入
4 2 1
2 2 1 2 3 2 2 3
输出
7
提示
可以按照这样的方式来造木桶:[1,2],[2,2],[2,3],[2,3]

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

题解

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

过题代码

#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 long
const 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 LOCAL
    freopen("test.txt", "r", stdin);
//    freopen("test1.out", "w", stdout);
    #endif // LOCAL
    ios::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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值