Hdoj 5037 Frog "All evils of greed"

Source: Internet
Author: User
Tags cas

Title: Hdoj 5037 Frog


Ask: Always clever frog, each can the length of L, Hecoin for M, drink center some land, it will choose to jump as few times as possible, now God can go to drink inside the land (as a point), let the frog jump the most times, the maximum number of times.


Analysis: 1: First of all, if you add a distance greater than (l+1), then God will give him the paragraph (l+1), because I give him (l+1), it can not jump past, the inevitable middle of a point, let the frog jump, so sure the number of steps maximum. (Greedy strategy)

2: Greedy time with each section of the remaining interval starting point as the starting point of greed, if it and the back interval and greater than or equal to (l+1), then you can let it skip the front interval, otherwise let continue to add.


It's been three hours since the game was played, in fact, the greedy strategy was right, at that time just retained the remainder, but did not count from the remainder of the first (that is, the interval initial point), after the WA after a few times, the generation of random numbers found to start from the interval initial point. Then think of how to deal with the back, when the writing is very complex, did not debug out.

In the middle deliberately for a period of time to determine whether the DP, negative DP, may be the previous character exhausted, this one is not allowed.


AC Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
typedef long LL;
using namespace std;

const int maxn=200005;
int A[MAXN];

int main ()
{
    int T;
    scanf ("%d", &t);
    for (int cas=1;cas<=t;cas++)
    {
        int n,m,l;
        scanf ("%d%d%d", &n,&m,&l);
        for (int i=1;i<=n;i++)
            scanf ("%d", &a[i]);
        a[++n]=m;a[0]=0;
        int ans=0,k=l;
        Sort (a,a+n+1);
        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);
            ans+=y*2;
            if (k+x>=l+1)
                k=x,ans++;
            else
                k=x+k;
        }
        printf ("Case #%d:%d\n", Cas,ans);
    }
    return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.