hdoj problem 5037 Frog(贪心)

来源:互联网 发布:京华科讯软件 编辑:程序博客网 时间:2024/06/05 00:09

Frog

http://acm.hdu.edu.cn/showproblem.php?pid=5037 
Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1796    Accepted Submission(s): 488


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
21 10 552 10 336
 

Sample Output
Case #1: 2Case #2: 4
 

Source
2014 ACM/ICPC Asia Regional Beijing Online
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5126 5125 5124 5123 5122 
/*
当时没有思路:
此段红代码
参考人家写的
*/
#include<cstdio>
#include<cstring>
#include<math.h>
#include<algorithm>
using namespace std;
int a[200005];
int main()
{
int T,Ca=1;
scanf("%d",&T);
while(T--)
{
 int N,M,L;
 int i,j,k;
 scanf("%d%d%d",&N,&M,&L);
 memset(a,0,sizeof(a));
 for(i=1;i<=N;i++)
   scanf("%d",&a[i]);
   a[N+1]=M;
 
   sort(a,a+N+2);
   
   int count=0,t=L;
   for(i=1;i<=N+1;i++)
     {
     int x=(a[i]-a[i-1])%(L+1);
      int y=(a[i]-a[i-1])/(L+1);

      count+=y*2;
       if(t+x>=L+1)
       {
        t=x;
        count++;
}
else
     t+=x;  
 }

   printf("Case #%d: %d\n",Ca++,count);
}
return 0;
}
 
0 0
原创粉丝点击