hdu 5037 周期优化

来源:互联网 发布:qq群关系数据库 编辑:程序博客网 时间:2024/05/16 09:13

http://acm.hdu.edu.cn/showproblem.php?pid=5037

有只青蛙踩石子过河,河宽m,有n个石子坐标已知。青蛙每次最多跳L。现在可以在河中再放一些石子,使得青蛙过河跳的次数最多。

青蛙是贪心的,如果它现在在cur位置跳不动了,且它上一次所在位置为pre。那么God肯定要把新石子放在max(cur,pre+L)+1的位置。

使用模拟+周期优化

#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;}



0 0
原创粉丝点击