标签:http io os 使用 ar for 2014 sp on
题目链接;点击打开链接
题意:有一条小河长为M的小河,可以看作一维轴,小河里存在N个石头,有一个每次能跳L米的小青蛙,随意添加石头保证青蛙能从头跳到尾的,问青蛙使用最优策略跳到对岸最多需要多少次。
思路:不妨假设青蛙每个石头都要经过一次,用step表示青蛙上一次跳的步长,每跳一次对目前点到下一点的距离和step的和与L做比较,如果小与,证明青蛙可以一次跳到这,更新step和青蛙位置,cnt保持不变,若大于,证明青蛙至少需要再跳一次,若lenth<=l,则直接跳,更新step=lenth,cnt++;若lenth>l,则让青蛙以l+1-step,step,的周期跳,易证可以保证解最优,然后相当于把石头向后平移x*(l+1)个单位。and so on。详情请看代码,非常明了~比赛的时候手残wa到哭!!!
cpp:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int data[200010],T,n,m,l;
void slove()
{
sort(data,data+n+1);
int step=l,now=0,cnt=0;
for(int i=0;i<=n;i++){
int lenth=data[i]-now;
if(lenth==0) continue;
if(lenth+step<=l){
now=data[i];
step=lenth+step;
}
else if(lenth<=l){
now=data[i];
step=lenth;
cnt++;
}
else if(lenth>l){
int tp=lenth%(l+1);
cnt+=(lenth/(l+1))*2;
if(tp+step<=l){
step=tp+step;
now=data[i];
}
else {
step=tp;
now=data[i];
cnt++;
}
}
}
printf("%d\n",cnt);
}
int main()
{
//freopen("data.in","r",stdin);
int cas=0;
scanf("%d",&T);
while (T--){
printf("Case #%d: ",++cas);
scanf("%d%d%d",&n,&m,&l);
for(int i=0;i<n;i++){
scanf("%d",data+i);
}
data[n]=m;
slove();
}
return 0;
}
标签:http io os 使用 ar for 2014 sp on
原文地址:http://blog.csdn.net/alpc_paul/article/details/39457193