您的位置:首页 > 其它

HDU——5037——Frog

2015-11-10 19:48 330 查看
http://acm.hdu.edu.cn/showproblem.php?pid=5037

贪心,一个月没做题了,这道题卡了半天0 0~

只要考虑清楚走两个时,为了保证最优,使得距离要是L+1,如果小于这个值,会导致能直接走到这里,就不是最优了

题目要求最差情况下青蛙走的最佳情况,所以每次开始都使他只走1,然后走N

#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 2e5 + 10;
int main()
{
int N, M, L;
int T;
int a[MAXN];
scanf("%d", &T);
int cas = 1;
while(T--){
scanf("%d%d%d", &N, &M, &L);
a[N+1] = M;
for(int i = 1; i <= N; i++)
scanf("%d", &a[i]);
sort(a + 1 , a + N + 2);
int pre = L, now = 0;
int cnt = 0;
int flag = 1;
for(int i = 1; i <= N + 1; i++){
int len = a[i] - now;
if(len == 0) continue;
if(len + pre <= L){
pre = len + pre;
now = a[i];
}
else if(len <= L){
now = a[i];
pre = len;
cnt++;
}
else{
//    printf("%d %d\n", now, cnt);
int k1, k2;
k1 = len % (L+1);
k2 = (len - k1)/(L+1);
cnt += k2*2;
if(k1 + pre <= L){
pre = k1 + pre;
now = a[i];
}
else {
pre = k1;
now = a[i];
cnt++;
}
}
}
printf("Case #%d: %d\n",cas++, cnt);
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: