HDU——5037——Frog

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

贪心,一个月没做题了,这道题卡了半天0 0~

只要考虑清楚走两个时,为了保证最优,使得距离要是L+1,如果小于这个值,会导致能直接走到这里,就不是最优了

题目要求最差情况下青蛙走的最佳情况,所以每次开始都使他只走1,然后走N

#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 2e5 + 10;
int main()
{
    int N, M, L;
    int T;
    int a[MAXN];
    scanf("%d", &T);
    int cas = 1;
    while(T--){
        scanf("%d%d%d", &N, &M, &L);
        a[N+1] = M;
        for(int i = 1; i <= N; i++)
            scanf("%d", &a[i]);
        sort(a + 1 , a + N + 2);
        int pre = L, now = 0;
        int cnt = 0;
        int flag = 1;
        for(int i = 1; i <= N + 1; i++){
            int len = a[i] - now;
            if(len == 0) continue;
            if(len + pre <= L){
                pre = len + pre;
                now = a[i];
            }
            else if(len <= L){
                now = a[i];
                pre = len;
                cnt++;
            }
            else{
              //    printf("%d %d\n", now, cnt);
                    int k1, k2;
                    k1 = len % (L+1);
                    k2 = (len - k1)/(L+1);
                    cnt += k2*2;
                    if(k1 + pre <= L){
                        pre = k1 + pre;
                        now = a[i];
                    }
                    else {
                        pre = k1;
                        now = a[i];
                        cnt++;
                }
            }
        }
            printf("Case #%d: %d\n",cas++, cnt);
        }
        return 0;
    }

  

 

转载于:https://www.cnblogs.com/zero-begin/p/4954076.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值