刚刚问了一下舍友他学校OJ哪题不会,我给他发个博客,于是就选了这一题.
首先我们来分析一下题意,就是以最少的防御罩个数来覆盖所有的太空基地,所以这是一道贪心的区间覆盖问题.
翻了翻以前自己写的代码也是五味杂陈,八九十行可太秀了,于是重新写了一个,不过心太急了做了好几遍才写出来2333
那么下面上代码
/**
* 校OJ: 1311 防御罩
* 贪心区间覆盖
**/
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 100+10;
int pos[maxn], ans, n, r;
int main() {
scanf("%d %d", &n, &r);
for(int i = 1; i <= n; i++)
scanf("%d", &pos[i]);
if(n == 1) ans = 1; //特判
int last = pos[1] + r, i = 1;
while( last < pos[n] ) {
ans++;
int t = 0;
if(pos[i] > last) { //如果这个位置超过了防御防御之内
last = pos[i]+r;
while(pos[i] <= last) t = max(t, pos[i++]+r);
}
else while(pos[i] <= last) t = max(t, pos[i++]+r);
last = t;
while(pos[i] <= last) i++;
//printf("Build %d---%d\n", last-2*r, last);
}
printf("%d\n", ans);
return 0;
}