HDU 5303 Delicious Apples Delicious apple (ring-shaped sequence, logic)

Source: Internet
Author: User

Test instructions: Give a ring long as L, starting at 12 o'clock O ' rest, there are some apples in the other position, each time with a basket that can pack K apples from the starting point to pick apples, how many meters will it take to get all the apples to the starting point?

Idea: Initially thought is the network flow, modelling cannot. Feeling is greedy, but it is a ring, a little difficult to do, give up directly (life is the most annoying ring, all kinds of situations to consider, that is, you: the clock problem!!!) )。

No matter where, as long as the number of apples more than K, then must be a special trip to transport away! So you can start with their mod K, the part that is removed first to calculate.

Then the rest of the situation to use greed to do. Apple tree Apple split into a single to see, up to 10w, then just save each Apple to the beginning of the distance. Consider the following two scenarios:

(1) Do not spare the whole circle, only half a circle: To do with greed, similar to the backpack, Dist[i-k]+pos[i] said the end of the first Apple will take the road length. Consider the situation of i<k.

(2) Circle only 1 times: calculation (1), around 1 times is the basket is dissatisfied, then the whole ring may be shorter. Walk a circle, the enumeration on the left side with I, then the right side with K-i, will not be less than (1) calculated results, the smallest.

1#include <bits/stdc++.h>2 #defineLL Long Long3 #definePII pair<int,int>4 #defineINF 0x7f7f7f7f5 using namespacestd;6 Const intn=105000;7 8 intApp[n], L, N, K;9 LL Dist1[n], dist2[n];Ten  One intMain () A { -Freopen ("E://input.txt","R", stdin); -     intT, a, B; theCin>>T; -      while(t--) -     { -memset (Dist1,0,sizeof(Dist1)); +memset (Dist2,0,sizeof(Dist2)); -memset (App,0,sizeof(APP)); +  Ascanf"%d%d%d", &l, &n, &k); at  -         intCnt=0; -          for(intI=0; i<n; i++) -         { -scanf"%d%d", &a, &b); -              while(b--) App[cnt++]=a;//split into a single, record distance in         } -  tovector<int>seq1, SEQ2; +          for(intI=0; i<cnt; i++) -         { the             if(2*app[i]<=l) seq1.push_back (App[i]); *             ElseSeq2.push_back (lapp[i]); $         }Panax Notoginseng  - sort (Seq1.begin (), Seq1.end ()); the sort (Seq2.begin (), Seq2.end ()); +  A          for(intI=0; I<seq1.size (); i++) thedist1[i+1]= (i+1<=k? Seq1[i]:d ist1[i+1-k]+seq1[i]); +  -          for(intI=0; I<seq2.size (); i++)//similarly $dist2[i+1]= (i+1<=k? Seq2[i]:d ist2[i+1-k]+seq2[i]); $  -LL len= (Dist1[seq1.size ()]+dist2[seq2.size ()) *2;//This is the worst case scenario. Do not loop around.  -  the          for(intI=0; i<=k; i++)//take a lap and see how much fits.  -         {Wuyi             intLeft=max (0, (int) seq1.size ()-i);//take I from the left when you are in a circle.  the             intRight=max (0, (int) Seq2.size ()-(k-i));//In addition, only from the right side of the k-i.  -Len=min (Len,2* (Dist1[left]+dist2[right]) +l); Wu         } -printf"%lld\n", Len); About     } $     return 0; -}
AC Code

HDU 5303 Delicious Apples Delicious apple (ring-shaped sequence, logic)

Related Article

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.