hdoj 5037 Frog【贪心】

来源:互联网 发布:hadoop 书籍 知乎 编辑:程序博客网 时间:2024/05/16 18:01

Frog

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2963    Accepted Submission(s): 776


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
分析:
1:首先如果加入大于(l+1)的距离,那么上帝会给他分成(l+1)的段,因为我给他(l+1),它一下子跳不过去,必然中间还要一个点,让青蛙跳,这样肯定步数最大。(贪心策略)

2:贪心的时候以每一段剩余区间开始点作为贪心的开始点,假如它和后面区间的和大于等于(l+1)时,那么可以让它跳过前面的区间,否则让继续加。

代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<cmath>#include<vector>#define mem(x,y) memset(x,y,sizoef(x))#define SI(x) scanf("%d",&x)#define O_O(x) while(x--)using namespace std;const int maxn=200020;int pp[maxn];int main(){    int t;    SI(t);    int cnt=0;    O_O(t)    {        int n,m,l;        scanf("%d%d%d",&n,&m,&l);        for(int i=1;i<=n;i++)            scanf("%d",&pp[i]);        pp[++n]=m;pp[0]=0;        int sum=0,k=l;        sort(pp,pp+n+1);        for(int i=1;i<=n;i++)        {            int x=(pp[i]-pp[i-1])%(l+1);            int y=(pp[i]-pp[i-1])/(l+1);            sum+=y*2;            if(k+x>=l+1)                k=x,sum++;            else k=x+k;        }        printf("Case #%d: %d\n",++cnt,sum);    }    return 0;}


0 0
原创粉丝点击