HDOJ 5037 Frog

Frog

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

Total Submission(s): 634    Accepted Submission(s): 152

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

Source

2014 ACM/ICPC Asia Regional Beijing Online

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

using namespace std;

int n,m,l;
int stone[200200];

int main()
{
    int T_T,cas=1;
    scanf("%d",&T_T);
    while(T_T--)
    {
        scanf("%d%d%d",&n,&m,&l);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",stone+i);
        }
        n++;
        stone[0]=0; stone[n]=m;
        n++;
        sort(stone,stone+n);
        int k=l,ans=0;
        for(int i=1;i<n;i++)
        {
            int x=(stone[i]-stone[i-1])%(l+1);
            int y=(stone[i]-stone[i-1])/(l+1);
            if(k+x>=l+1)
            {
                k=x;
                ans+=y*2+1;
            }
            else
            {
                k=x+k;
                ans+=y*2;
            }
        }
        printf("Case #%d: %d\n",cas++,ans);
    }
    return 0;
}
时间: 2024-10-15 00:48:48

HDOJ 5037 Frog的相关文章

hdoj 5037 Frog 【万恶的贪心】

题目:hdoj 5037 Frog 题意:一直聪明的青蛙,每次能条 l 的长度,河宽为m,喝中心有一些陆地,它会选择尽量少的次数跳,现在上帝可以任意往喝里面放陆地(视为点),让青蛙跳的次数最多,求最大次数? 分析:1:首先如果加入大于(l+1)的距离,那么上帝会给他分成(l+1)的段,因为我给他(l+1),它一下子跳不过去,必然中间还要一个点,让青蛙跳,这样肯定步数最大.(贪心策略) 2:贪心的时候以每一段剩余区间开始点作为贪心的开始点,假如它和后面区间的和大于等于(l+1)时,那么可以让它跳过

hdu 5037 Frog(高效)

题目链接:hdu 5037 Frog 题目大意:给定N,M,L,表示有一只条宽为M的河,青蛙要从0跳到M,青蛙的最大跳跃能力为L,现在已经存在了N块石头,现在要放任意数量的石头,使得说青蛙需要用最多的步数跳到M. 解题思路:维护两个指针,pos表示青蛙当前的位置,mv表示青蛙在前一个位置能跳到的最远位置.然后周期L+1的长度可以让青蛙需要跳两次.但注意每一段长度的最后一个周期需要特判,因为有可能只需要一步. #include <cstdio> #include <cstring>

hdu 5037 Frog 贪心 dp

哎,注意细节啊,,,,,,,思维的严密性..... 11699193 2014-09-22 08:46:42 Accepted 5037 796MS 1864K 2204 B G++ czy Frog Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 454    Accepted Submission(s): 96 Problem

hdu 5037 Frog (贪心)

Frog Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 789    Accepted Submission(s): 198 Problem Description Once upon a time, there is a little frog called Matt. One day, he came to a river.

hdu 5037 Frog

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 2040    Accepted Submission(s): 616 Problem Description Once upon a time, there is a little frog called Matt. One day, he came to a river. The

HDU 5037 FROG (贪心)

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 (

hdu 5037 Frog(北京网络赛)

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, the

HDU 5037 FROG 贪心 2014北京网络赛F

题目链接:点击打开链接 题意:有一条小河长为M的小河,可以看作一维轴,小河里存在N个石头,有一个每次能跳L米的小青蛙,随意添加石头保证青蛙能从头跳到尾的,问青蛙使用最优策略跳到对岸最多需要多少次. 思路:不妨假设青蛙每个石头都要经过一次,用step表示青蛙上一次跳的步长,每跳一次对目前点到下一点的距离和step的和与L做比较,如果小与,证明青蛙可以一次跳到这,更新step和青蛙位置,cnt保持不变,若大于,证明青蛙至少需要再跳一次,若lenth<=l,则直接跳,更新step=lenth,cnt+

hdu 5037 Frog(贪心)

分析:贪心吧,让三个石头第一个和第三个距离为L+1,并让每次跳的点尽量小. 石头是可能无序的,比赛是实在没发现,就加了个排序过了,哎... 和代码一起讲做法吧,假设情况1 :上一步k加这一步余数x大于L,则最后剩余部分需要单独跳:情况2:上一步k加这一步余数x小于等于L,最后剩余部分可以并进上一步,即k+x. 画了一张图: 代码: #include<iostream> #include<queue> #include<map> #include<cstdio>