http://acm.hdu.edu.cn/showproblem.php?pid=5037
有只青蛙踩石子过河,河宽m,有n个石子坐标已知。青蛙每次最多跳L。现在可以在河中再放一些石子,使得青蛙过河跳的次数最多。
青蛙是贪心的,如果它现在在cur位置跳不动了,且它上一次所在位置为pre。那么God肯定要把新石子放在
使用模拟+周期优化
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <bitset>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;
const int maxn = 200005;
int n,m,l,s[maxn];
int main (){
int _,cas = 1;
RD(_);
while(_--){
printf("Case #%d: ",cas++);
RD(n);RD2(m,l);
for(int i = 0;i < n;++i)
RD(s[i]);
sort(s,s+n);
s[n++] = m;
int ans = 0,pre = -l,cur = 0;
for(int i = 0;i < n;++i){//cout<<i<<endl;
int t = (s[i] - cur)/(l+1);
pre += t*(l+1);
ans += t*2;
if(s[i] - pre <= l){
cur = s[i];
}else if(s[i] - pre > l){
ans++;
pre = cur + t*(l+1);
cur = s[i];
}
}
printf("%d\n",ans);
}
return 0;
}
浙公网安备 33010602011771号