hdu 5037

http://acm.hdu.edu.cn/showproblem.php?pid=5037

贪心  最初WA了一次,做法就是看石头间距pos[i+1]-pos[i] <=L直接跳,否则ans++;非常愚蠢

1,2,3  这个例子就能破掉这种策略

所以其实一个比较重要的问题在于处理上一步到下一步的剩余问题

参考了http://blog.csdn.net/u014569598/article/details/39471913

正确的策略是,记录上一次跳的长度,如果这次遇到的石子的间距x   x+last_len <=L  那么上次就能把这个x也跳掉,

要说从这道题里收获了什么,应该就是 找出两次决策之间的关系,想想怎么处理上次决策剩余。尤其别忽略相邻两次决策的关系

所以就是下面的代码,注意注释

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <set>
#include <queue>
#include <cmath>

using namespace std;
const int MAXN = 2*1e5+50;

int pos[MAXN];
int main()
{
    int ncase,ic=0;
    int n,m,L;
    scanf("%d",&ncase);
    while(ncase--)
    {
        scanf("%d%d%d",&n,&m,&L);
        for(int i=1;i<=n;i++)
            scanf("%d",&pos[i]);
        int ans=0,dis=L;
        pos[0]=0;
        pos[++n]=m;
        sort(pos,pos+n);
        for(int i=0;i<n;i++)
        {
            //dis+=pos[i+1]-pos[i];
            int x=(pos[i+1]-pos[i])%(L+1);
            int y=(pos[i+1]-pos[i])/(L+1);
            if(dis+x>=L+1)
            {
                dis=x;//虽然dis<=L  但是需要跳一步,因为下一块石子的距离加上的话,会超过L
                ans+=1+2*y; //L+1需要跳两步
            }
            else
            {
                dis+=x; // dis+x<=L  所以一步就跳过好几个石子
                ans+=2*y;
            }
            //if(pos[i+1]-pos[i] <=L)continue;
            //ans+=(pos[i+1]-L-pos[i]);
        }
        printf("Case #%d: %d\n",++ic,ans);
    }
    return 0;
}
时间: 2024-08-05 12:15:13

hdu 5037的相关文章

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 贪心 2014北京网络赛F

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

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(贪心)

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

HDU 5037(Frog-贪心青蛙跳石子)

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

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