A
题目链接:[CF1066 A Vova and Train](https://www.luogu.org/problemnew/show/CF1066A)思路: 类似前缀和暴力搞一下
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](https://www.luogu.org/problemnew/show/CF1066B)思路: 贪心的另一种思路,先假设所有的加热器全部开启,此时如果还有地方没有被加热到,输出-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](https://www.luogu.org/problemnew/show/CF1066C)思路:乍一看想写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](https://www.luogu.org/problemnew/show/CF1066D)思路:怎么越来越简单了...从右边直接开始模拟取,方案不可行的时候直接输出
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
*/