CF1066 ABCD

A

题目链接:CF1066 A Vova and Train

思路: 类似前缀和暴力搞一下

code:

#include <bits/stdc++.h>
using namespace std;
int T, L, V, l, r;

template <typename T>
inline void read(T &t) { ; }

int main() {
    scanf("%d", &T);
    while(T--) {
        int ans = 0;
        scanf("%d %d %d %d", &L, &V, &l, &r);
        printf("%d\n", L / V - r / V + (l - 1) / V);
    }
    return 0;
}

B

题目链接:CF1066B Heaters

思路: 贪心的另一种思路,先假设所有的加热器全部开启,此时如果还有地方没有被加热到,输出-1,

从左到右扫描每一个加热器,如果它的范围内所有的加热叠加次数都大于1,这个加热器就可以被去掉,对答案没有影响

code:

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
int n, a[N], cnt[N], ans = 0, r;

int main() {
    scanf("%d %d", &n, &r);
    for(int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        if(a[i]) {
            ans++;
            for(int j = max(1, i - r + 1); j <= min(n, i + r - 1); j++) {
                cnt[j]++;
            }
        }
    }
    for(int i = 1; i <= n; i++) {
        if(!cnt[i]) return printf("-1\n"), 0; 
    }
    for(int i = 1; i <= n; i++) {
        bool fl = true;
        if(a[i]) {
            for(int j = max(1, i - r + 1); j <= min(n, i + r - 1); j++) {
                if(cnt[j] == 1) {
                    fl = false;
                    break;
                }
            }
        }
        if(fl && a[i]) {
            ans--;
            for(int j = max(1, i - r + 1); j <= min(n, i + r - 1); j++) {
                cnt[j]--;
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}

C

题目链接:CF1066C Books Queries

思路:乍一看想写splay了...维护两个指针 l 和 r 代表当前往右边选到哪儿,往左边选到哪儿,

每个书的id和l或r用map映射一下,查询的时候输出min(这个id的映射值减去当前右边选到哪儿,当前左边选到哪儿减这个id的映射值)

需要特判一下第一本插入的书

code:

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, idx = 500000, _idx = 500000;

char opt;

map <int, int> mps; // id , num 

int main() {
    scanf("%d", &n);
    bool fl = false;
    for(int i = 1, id; i <= n; i++) {
        cin >> opt >> id;
        if(opt == 'L') {
            if(!fl) {
                fl = true;
                mps[id] = idx;
            }
            else idx--, mps[id] = idx;
        } else {
            if(opt == 'R') {
                if(!fl) {
                    fl = true;
                    mps[id] = idx;
                }
                else _idx++, mps[id] = _idx;
            } else {
                printf("%d\n", min(mps[id] - idx, _idx - mps[id]));
            }
        }
    }
    return 0;
}

D

题目链接:CF1066D Boxes Packing

思路:怎么越来越简单了...从右边直接开始模拟取,方案不可行的时候直接输出

Code:

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int INF = 0x3f3f3f3f;
typedef long long ll;
int n, m, k, a[N];

int main() {
    cin >> n >> m >> k;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    ll tmp = 0, sum = 0;
    for(int i = n; i >= 1; i--) {
        if(sum + a[i] > k) {
            tmp++, sum = a[i];
            if(tmp >= m) {
                printf("%d\n", n - i);
                return 0;
            }
        } else {
            sum += a[i];
        }
    }
    printf("%d\n", n);
    return 0;
}

/*
5 4 2
1 2 1 2 1
*/

转载于:https://www.cnblogs.com/chloristendika/p/9912192.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值