HDU 5303 Delicious Apples (DP)

Source: Internet
Author: User

Test instructions: An n-tree with a circle of l, each tree coordinates XI, an AI apple, and a basket of size k to put all the apples back, asking at least how many roads to walk

Solution: By the Magic DP teaches a person (actually I compare water, originally thought to the left greedy, the right greedy a bit in the last turn a circle on the water problem =-=)

#include <stdio.h> #include <string.h> #include <algorithm> #define LL __int64#define MIN (b) ((a) < (b)? (a):(B)) const int maxn=100005;using namespace STD;LL dp[3][maxn];struct aaa{int x,a;} Tree[maxn];int CMP (AAA a,aaa b) {return a.x<b.x;}    int main () {int t;    int l,n,k;            while (scanf ("%d", &t)!=-1) {while (t--) {scanf ("%d%d%d", &l,&n,&k);            for (int i=0;i<n;i++) scanf ("%d%d", &tree[i].x,&tree[i].a);            Sort (tree,tree+n,cmp);            dp[0][0]=dp[1][0]=0;            int fr,ed=1;                for (int i=0;i<n;i++)//Clockwise each Apple takes the path of the walk {for (int j=0;j<tree[i].a;j++)                    {fr=ed-k>0?ed-k:0;                    dp[0][ed]=dp[0][fr]+ (2*tree[i].x>l?l:2*tree[i].x);                ed++;            }} ed=1;            ll Sum=0; for (int i=n-1;i>=0;i--)//counter-clockwise eachThe Apple takes the road to walk {sum+=tree[i].a;                    for (int j=0;j<tree[i].a;j++) {fr=ed-k>0?ed-k:0;                    dp[1][ed]=dp[1][fr]+ (l-tree[i].x) >l?l:2* (l-tree[i].x));                ed++;            }} ll lenth=1000000000;   for (ll i=0;i<=sum;i++) lenth=min (Lenth,dp[0][i]+dp[1][sum-i]);        Find the smallest printf ("%i64d\n", lenth); }} return 0;}


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

HDU 5303 Delicious Apples (DP)

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.