hdu 5037 贪心(难)

题目链接:https://vjudge.net/problem/HDU-5037
转自:https://blog.csdn.net/hyczms/article/details/44230083
题意:有一条小河长为M的小河。能够看作一维轴,小河里存在N个石头,有一个每次能跳L米的小青蛙。任意加入石头保证青蛙能从头跳到尾的,问青蛙使用最优策略跳到对岸最多须要多少次。、
思路:首先,每一步之间看看有多少l+1,因为l+1的距离最多需要2步。在分情况讨论。情况1:上一步跳的步数last+这一步跳的余数>=l+1,这时候应该多跳一步。情况2:上一步跳的步数last+这一步跳的余数<l+1,也就是说上一步跳的余数可以跟这一步跳的余数合并为一步。这时候就不需要多跳一步。

#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
int n,m,l;
int num[maxn];
int main()
{
    int T,kase=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&l);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&num[i]);
        }
        num[0]=0;
        num[++n]=m;
        int ans=0;
        int last=l;
        sort(num,num+n);
        for(int i=1; i<=n; i++)
        {
            int x=(num[i]-num[i-1])%(l+1);
            int y=(num[i]-num[i-1])/(l+1);
            if(last+x>=l+1)
            {
                last=x;
                ans+=2*y+1;
            }
            else if(last+x<l+1)
            {
                last=last+x;
                ans+=2*y;
            }
        }
        printf("Case #%d: %d\n",++kase,ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值