HDU 5037

       一道贪心的题目,由于前后关系考虑需要细心一些,所以纠结了我好久!

      总之,一道很好的贪心题。不过要注意,所有的石头都在整数点上,这点题目没交代清楚。

     贴组数据(来源:http://blog.youkuaiyun.com/u011032846/article/details/39496587):

    3
    4 17 3
    4 7 12 16
    4 18 3
    4 7 12 16
    4 19 3
    4 7 12 16

代码(C++):

#include <cstdio>
#include <iostream>
#include <algorithm>

#define MAX 200009
using namespace std;

int point[MAX];

int main()
{
    //freopen("in.txt","r",stdin);

    int t,n,m,len,ca,i,now,res,tmp,pre,d;
    scanf("%d",&t);
    for(ca=1;ca<=t;ca++)
    {
        scanf("%d %d %d",&n,&m,&len);
        for(i=0;i<n;i++) scanf("%d",&point[i]);
        sort(point,point+n);
        point[n]=m;
        now=0;
        res=0;
        pre=-len;
        for(i=0;i<=n;i++)
        {
            if(i<n&&point[i]-now<=len&&point[i+1]-now>len)
            {
                res++;
                pre=now;
                now=point[i];
            }else if(point[i]-now>len)
            {
                 tmp=(point[i]-now)/(len+1);
                 res+=tmp*2-1;
                 d=now-pre;
                 now+=tmp*(len+1)-d;
                 pre=now-(len+1-d);
                 i--;
            }
        }
        if(now<point[n]) res++;
        printf("Case #%d: %d\n",ca,res);
    }
    return 0;
}

题目( http://acm.hdu.edu.cn/showproblem.php?pid=5037):

Frog

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)


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
  
2 1 10 5 5 2 10 3 3 6
 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值