标签:algorithm
Frog
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144
K (Java/Others)
Total Submission(s): 1086 Accepted Submission(s): 285
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
总是莫名其妙的wa,当相邻两块石头距离大于l时,有l+1的周期,先在当前位置前放一块石头,l+1处放一块石头,这样两块石子就必须跳了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int maxn=1000000+100;
int a[maxn];
int main()
{
int t,n,m,l;
scanf("%d",&t);
int ca=1;
while(t--)
{
scanf("%d%d%d",&n,&m,&l);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
int fr = 0;
int ans = 0;
a[n]=m;
int pre = -l;
int now;
for (int i = 0;i <=n; i++)
{
now = a[i];
int t2=(now-fr)/(l + 1);
pre+=t2*(l + 1);
ans+=t2 * 2;
if (now- pre>l)
{
pre=fr + t2*(l + 1);
fr= now;
ans++;
}
else
{
fr = now;
}
}
printf("Case #%d: %d\n",ca++,ans);
}
return 0;
}
hdu 5037 Frog(北京网络赛)
标签:algorithm
原文地址:http://blog.csdn.net/caduca/article/details/39500837