Hdu5303 Delicious Apples Greedy

Source: Internet
Author: User

Topic Links:

HDU5303




Test instructions

There is a circular long road for L, warehouse at position 0,

There are n apple trees on the road, giving the location of each apple tree and the number of apples,

Ask for a basket of up to K apples at a time. The minimum distance that all the apples in this road should be taken back to the warehouse




Problem Solving Ideas:

This road is circular, the fruit tree is divided into two parts, the left half of the circle is counted as part of the right half of the circle is another part

For all apples according to the sort of distance, with the idea of similar backpack, statistics left half, the right half to walk back and forth (the length must be less than a ring circumference) way to collect the minimum distance that the apples need to walk;

Finally consider the need to walk around the situation: the left more than K1 (<k) An apple, the right more than K2 (<k) an apple.

Determine the distance you need to go back and forth to take the K1 + K2 The distance you need to go and the size of the circumference long l to get the corresponding answer



Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define LL Long Long#define maxn 100050using namespace Std;int dis[maxn],cnt;int ldis[maxn],rdis[maxn];int cnt1,cnt2; LL lsum[maxn],rsum[maxn];int cmp (int a,int b) {return a<b;}    int main () {//Freopen ("In.txt", "R", stdin);    int t,m,n,k,a,b,l;    scanf ("%d", &t);        while (t--) {scanf ("%d%d%d", &l,&m,&k);        cnt=cnt1=cnt2=0;            while (m--) {scanf ("%d%d", &a,&b);            if (a==0) continue;            if ((a<<1) <=l) for (int i=1;i<=b;i++) ldis[++cnt1]=a;        else for (int i=1;i<=b;i++) rdis[++cnt2]=l-a;        } sort (ldis+1,ldis+cnt1+1,cmp);        Sort (rdis+1,rdis+cnt2+1,cmp);                Cnt=cnt1+cnt2;            for (int i=1;i<=cnt1;i++)//left half by way of back and forth to get through if (i<=k) lsum[i]=ldis[i]; Else LSUM[I]=LDIS[I]+LSUM[I-K];            for (int i=1;i<=cnt2;i++)//Right half ... if (i<=k) rsum[i]=rdis[i];        else Rsum[i]=rdis[i]+rsum[i-k];                LL ans= (Lsum[cnt1]+rsum[cnt2]) <<1;        K=min (K,CNT);        int l_,r_;            for (int i=1;i<=cnt1&&i<=k;i++)//enumeration when walking around, the left half of the apple number {L_=cnt1-i,r_=max (0,cnt2-k+i);        Ans=min (ans,l+ ((lsum[l_]+rsum[r_)) <<1);    } printf ("%i64d\n", ans); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdu5303 Delicious Apples 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.