zoukankan      html  css  js  c++  java
  • HDU 5037 Frog(贪心)

    题意比较难懂,一只青蛙过河,它最多一次跳L米,现在河中有石头,距离不等,上帝可以往里加石头,青蛙非常聪明,它一定会选择跳的次数最少的路径。问怎么添加石头能让青蛙最多的次数。输出青蛙跳的最多的次数。

    考虑对于长度L+1,上帝一定会让青蛙跳两次。那么只需要尽可能的构造L+1就行了。那么就需要求多少个L+1就行了。还有就是需要记录上一次跳的距离,如果上一次跳的距离加上这次的距离小于L+1的话,那么上次一定会跳到当前这个点,而不是跳到上次那个点,所以更新一下上次的距离。也就是这两种情况:

    1)上一步pre加这一步余数y大于L,则最后剩余部分需要单独跳;

    2)上一步pre加这一步余数y小于等于L,最后剩余部分可以并进上一步,即pre+y。

    代码如下:

    #include <cstdio>
    #include <algorithm>
    using namespace std;
    const int maxn = 2 * 1e5 + 10;
    int a[maxn];
    int main()
    {
        int T, n, m, L, kase = 0;
        scanf("%d", &T);
        while (T--)
        {
            scanf("%d %d %d", &n, &m, &L);
            for (int i = 1; i <= n; i++)
                scanf("%d", &a[i]);
            a[0] = 0; a[++n] = m;
            sort(a, a + n);
            int ans = 0, pre = L;
            for (int i = 1; i <= n; i++)
            {
                int x = (a[i] - a[i - 1]) / (L + 1);
                int y = (a[i] - a[i - 1]) % (L + 1);
                if (y + pre >= L + 1)
                {
                    pre = y;
                    ans += 2 * x + 1;
                }
                else
                {
                    pre = pre + y;
                    ans += 2 * x;
                }
            }
            printf("Case #%d: %d
    ", ++kase, ans);
            
        }
        
        return 0;
    }
  • 相关阅读:
    css知多少(10)——display
    css知多少(9)——float下篇
    centos-lynx
    linux-redis
    springMVC robots.txt 处理
    .net core
    docker快速入门+搭建javaweb环境
    maven+spring+springMVC+mybatis+dubbox
    微信群之社群经济的魔力
    maven项目部署打包
  • 原文地址:https://www.cnblogs.com/Howe-Young/p/4834654.html
Copyright © 2011-2022 走看看