[Multi-school 2015.02.1004 DP] HDU 5303 Delicious Apples

Source: Internet
Author: User

Test instructions

There are n apple trees on a ring of length L. Your basket capacity is K apples.

Every apple tree has a a[i apple.

Ask you how much distance you need to go from 0 to get all the apples.

Ideas:

We consider dp,dp[0][i] to represent the shortest distance of I apples clockwise.

Dp[1][i] represents the shortest distance I apples are counterclockwise.

So the apple is always the sum

So Ans=dp[0][i]+dp[sum-i] (0<=i<=sum)

Code:

#include "stdio.h" #include "algorithm" #include "string.h" #include "iostream" #include "queue" #include "map" #include " Vector "#include" string "#include" Cmath "using namespace std; #define LL __int64ll l;struct node{int x,s;} ap[123456];ll D P[2][123456];int CMP (node A,node b) {return a.x<b.x;}    int main () {int t;    cin>>t;        while (t--) {int n,k;        scanf ("%i64d%d%d", &l,&n,&k);        for (int i=0; i<n; i++) scanf ("%d%d", &AMP;AP[I].X,&AMP;AP[I].S);        Memset (Dp,0,sizeof (DP));        Sort (ap,ap+n,cmp);       int tep=1,sum=0;                TEP representative took a few apples because must be incremented for (int i=0; i<n; i++) {for (int j=0; j<ap[i].s; J + +) {                if (tep-k<0) dp[0][tep]=dp[0][0]+min (l,2ll*ap[i].x);                else Dp[0][tep]=dp[0][tep-k]+min (l,2ll*ap[i].x);            tep++;        }} tep=1;            for (int i=n-1; i>=0; i--) {sum+=ap[i].s; for (int j=0; j<ap[i].s; j + +)            {if (tep-k<0) dp[1][tep]=dp[1][0]+min (l,2ll* (l-ap[i].x));                Else Dp[1][tep]=dp[1][tep-k]+min (l,2ll* (l-ap[i].x));            tep++;        }} ll Ans=999999999999999999ll;        for (int i=0;i<=sum;i++) ans=min (Ans,dp[0][i]+dp[1][sum-i]);    printf ("%i64d\n", ans); } return 0;}


[Multi-school 2015.02.1004 DP] HDU 5303 Delicious Apples

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.