hdu 5037 Frog(贪心)

题意:

有一条小河长为M的小河,可以看作一维轴,小河里存在N个石头,有一个每次能跳L米的小青蛙,随意添加石头保证青蛙能从头跳到尾的,问青蛙使用最优策略跳到对岸最多需要多少次。

思路:

不妨假设青蛙每个石头都要经过一次,用step表示青蛙上一次跳的步长,x表示下一点的距离x。

(L+1) * y + x = a[i] - a[i-1],以(L+1)的周期跳,每个周期能跳2次,易证可以保证解最优,然后相当于把石头向前平移y * (L+1)个单位,cnt += 2*y;

x + step <= L,表明青蛙可以一次跳到这,更新step,step += x,cnt保持不变。
若大于,证明青蛙至少需要再跳一次,cnt++,step = x;

AC代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
int n, m, L;
int a[N];
inline void read(int &x) {
    int flag = 0;
    x = 0;
    char c = getchar();
    if(c == '-')
        flag = 1;
    while(c < '0' || c > '9') {
        if(c == '-')
            flag = 1;
        c = getchar();
    }
    while(c >= '0' && c <= '9')
        x = x * 10 + c - '0', c = getchar();
    if(flag) x = -x;
}

int main() {
    int T, cas = 1;
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d%d", &n, &m, &L);
        for(int i = 1; i <= n; i++) {
            read(a[i]);
        }
        a[0] = 0;
        a[++n] = m;
        int step = L;
        int ans = 0;

        sort(a, a+n+1);
        for(int i = 0; i < n; i++) {
            int x = (a[i+1] - a[i]) % (L+1);
            int y = (a[i+1] - a[i]) / (L+1);
            ans += y*2;
            if(step + x <= L) {
                step += x;
            }else {
                step = x;
                ans++;
            }
        }
        printf("Case #%d: %d\n", cas++, ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值