Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 – Final)题解

CodeForces题目通道

A. Kids Seating

这题是一题很简单的思维题。n~2n-1之间的数必然不会互相被整除,所有数字乘以2也就可以满足gcd不为1的条件,且范围在4n内。

#include "bits/stdc++.h"

using namespace std;

int main(void) {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        for (int i = 1; i <= n; ++i) {
            printf("%d ", 4 * n - 2 * i);
        }
        cout << endl;
    }

    return 0;
}

 

 

B. Saving the City

把并列的分块,再判断选择放炸弹还是分开炸。就是需要注意一下两个特判。

#include "bits/stdc++.h"

using namespace std;

const int MAXSIZE = 1e5 + 5;

typedef struct {
    int before = 0;
    int num = 0;
}mine;

mine mines[MAXSIZE];

int main(void) {
    int t;
    cin >> t;
    while (t--) {
        memset(mines, 0,sizeof(mines));
        int a, b;
        cin >> b >> a;
        string str;
        cin >> str;
        int i = 0, j = 0;
        while (i < str.length()) {
            while (i < str.length() && str[i] == '0') {
                ++i;
                mines[j].before++;
            }
            while (i < str.length() && str[i] == '1') {
                mines[j].num++;
                ++i;
            }
            ++j;
        }
        if (j == 1 && mines[0].num == 0) {
            cout << 0 << endl;
            continue;
        }
        int cnt = b;
        for (int k = 1; k < j - 1; ++k) {
            cnt += min(mines[k].before * a, b);
        }
        if (mines[j - 1].num > 0 && j > 1) {
            cnt += min(mines[j - 1].before * a, b);
        }
        cout << cnt << endl;
    }

    return 0;
}

 

 

C. The Delivery Dilemma

先按照外卖的所需时间排序,最大值即位所有外卖送达所需的时间,随着外卖时间的减少所需要跑腿的时间增多,使二者尽量接近对方,在比对其中的最小值即为答案。

#include "bits/stdc++.h"

using namespace std;

const int MAXSIZE = 2 * 1e5 + 5;

struct tempStruct {
    int a, b;
}places[MAXSIZE];

bool cmp(tempStruct a, tempStruct b) {
    return a.a > b.a;
}

int main(void) {
    int t;

    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        for (int i = 0; i < n; ++i) {
            scanf("%d", &places[i].a);
        }
        for (int i = 0; i < n; ++i) {
            scanf("%d", &places[i].b);
        }
        sort(places, places + n, cmp);
        int sum = 0, k = 0;
        while (places[k].a >= sum && k < n) {
            sum += places[k++].b;
        }
        int ans = min(sum, places[k-1].a);
        cout << ans << endl;
    }

    return 0;
}

 

 

D. Extreme Subtraction

这题是一题差分题,判断第一个数字是否大于后面所有负数差分和的绝对值即可。


#include "bits/stdc++.h"

using namespace std;

const int MAXSIZE = 30005;

int a[MAXSIZE], d[MAXSIZE];

int main(void) {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        for (int i = 0; i < n; ++i) {
            scanf("%d", &a[i]);
        }
        d[0] = a[0];
        int sum = 0;
        for (int i = 1; i < n; ++i) {
            d[i] = a[i] - a[i - 1];
            if (d[i] < 0) sum += d[i];
        }
        if (sum + d[0] >= 0) {
            cout << "YES" << endl;
        }else {
            cout << "NO" << endl;
        }

    }

    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值