标签:des style color io os java ar strong for
2 1 10 5 5 2 10 3 3 6
Case #1: 2 Case #2: 4
贪心思想:每次总是尽量让青蛙走最近的石头,需要记录当前和上一个青蛙跳的石头位置。
1、如果可以跳到下一个石头能到达,就跳到最远的那个;
2、否则,就需要添加加石头,石头加在max(now+1,pre+l+1)处,也就是pre刚好不能跳到的,now能跳到的最近的那一个。使前一个和后一个距离总为L+1,得到最优解。
#include"stdio.h"
#include"math.h"
#include"string.h"
#include"iostream"
#include"algorithm"
using namespace std;
#define N 200005
const int inf=(int)1e10;
int a[N];
int max(int a,int b)
{
return a>b?a:b;
}
int main ()
{
int n,i,cnt=1,T,m,l,ans;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&l);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
a[0]=0;
a[n+1]=m;
sort(a,a+n+2);
n+=2;
int pre,now;
pre=-inf;
now=0;
ans=0;
i=1;
while(now<m)
{
while(a[i]-now<=l&&i<n)
i++;
if(a[i-1]>now&&a[i-1]<=now+l)
{
pre=now;
now=a[i-1];
ans++;
}
else
{
int w=(a[i]-now)/(l+1)-1;
if(w>0)
{
int t=max(now+1,pre+l+1);
pre=t+(w-1)*(l+1);
now+=w*(l+1);
ans+=w*2;
}
else
{
int t=max(now+1,pre+l+1);
pre=now;
now=t;
ans++;
}
}
}
printf("Case #%d: %d\n",cnt++,ans);
}
return 0;
}
标签:des style color io os java ar strong for
原文地址:http://blog.csdn.net/u011721440/article/details/39480843