http://acm.hdu.edu.cn/showproblem.php?pid=5037
Problem Description
Once upon a time, there is a little frog called Matt. One day, he came to a river.
The river could be considered as an axis.Matt is standing on the left bank now (at position 0). He wants to cross the river, reach the right bank (at position M). But Matt could only jump for at most L units, for example from 0 to L.
As the God of Nature, you must save this poor frog.There are N rocks lying in the river initially. The size of the rock is negligible. So it can be indicated by a point in the axis. Matt can jump to or from a rock as well as the bank.
You don't want to make the things that easy. So you will put some new rocks into the river such that Matt could jump over the river in maximal steps.And you don't care the number of rocks you add since you are the God.
Note that Matt is so clever that he always choose the optimal way after you put down all the rocks.
Input
The first line contains only one integer T, which indicates the number of test cases.
For each test case, the first line contains N, M, L (0<=N<=2*10^5,1<=M<=10^9, 1<=L<=10^9).
And in the following N lines, each line contains one integer within (0, M) indicating the position of rock.
Output
For each test case, just output one line “Case #x: y", where x is the case number (starting from 1) and y is the maximal number of steps Matt should jump.
Sample Input
Sample Output
解题思路:当时比赛的时候没想出来,今天看看网上的博客才知道怎么写。大体思想是:对于任意两个已知的石子ai-1,ai+1来说,他们之间的距离只要是满足整段L+1的部分一律按两次来调,剩下的不足l+1的部分x,加ai-1点与之前一跳的距离k求和,如果大于l中则多跳一步,k值不变,否则小于 l 的话,不多跳,k值变为k+x。
我参考:http://blog.csdn.net/u014569598/article/details/39471913
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
int n,m,l,a[200005];
int main()
{
int T,tt=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&l);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
a[0]=0;a[++n]=m;
sort(a,a+n+1);
int k=l;
int ans=0;
for(int i=1;i<=n;i++)
{
int x=(a[i]-a[i-1])%(l+1);
int y=(a[i]-a[i-1])/(1+l);
if(k+x>l)
{
ans+=2*y+1;
k=x;
}
else
{
ans+=2*y;
k+=x;
}
}
printf("Case #%d: %d\n",++tt,ans);
}
return 0;
}