Hdoj 5037 frog [greedy]

Source: Internet
Author: User

Title: hdoj 5037 frog


A clever frog can have a length of L each time. The river width is m, and the drinking center has some land. He will skip as few times as possible, now, God can place any land in his drink (as a point), so that the frog jumps the most times and the maximum number of times?


Analysis: 1: If we add a distance greater than (L + 1), then God will divide him into (L + 1) segments, because I will give him (L + 1 ), it cannot jump at once, and it must have a point in the middle to let the frog jump, so it is sure that the number of steps is the largest. (Greedy Policy)

2: When greedy, the starting point of each remaining interval is used as the greedy starting point. If the sum between it and the following interval is greater than or equal to (L + 1, you can skip the preceding interval. Otherwise, you can continue to add the interval.


The competition for this question lasted three hours. In fact, at that time, the greedy strategy was right. At that time, only the remainder was retained, but it was not counted from the beginning of the remainder (that is, the initial point of the interval ), after numerous times of the following wa, a random number was generated and found to start from the initial point of the interval. Then I thought about how to deal with it later. At that time, the writing was very complicated and I didn't debug it.

In the middle, I deliberately determined whether it was DP for a period of time, and denied DP. It may be that the previous one was exhausted, and this one was not allowed.


AC code:

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>typedef long 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;}


Hdoj 5037 frog [greedy]

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.