Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5303
Test instructions: Some apple trees are planted on a ring path long as L, tell you the location of the apple tree (with 0~l in the title) and the number of apples on the apple tree, and now you are going to collect all the apples (presumably you have a basket with a capacity of k, and the apples you collect will be in the basket, and you will have to go back to the home at coordinates 0 to empty the baskets). Ask you at least how many roads you need to walk
Solution: You can imagine the most need to walk the whole ring, the rest is to walk two half ring, then the two half-ring greedy (due to the small amount of data, there is a very elegant posture ~)
And then consider the whole loop, because the cost of the whole ring is fixed (L), then can replace the higher the cost (k) the better, need to enumerate how many of these k are left, the rest is the right side
1 /*2 * problem:hdu5303 Delicious Apples3 * AUTHOR:SHJWUDP4 * Created TIME:2015/8/10 Monday 21:46:105 * File name:1006.cpp6 * state:accepted7 * Memo: Greedy + enumeration8 */9#include <iostream>Ten#include <cstdio> One#include <vector> A#include <cstring> -#include <algorithm> - the using namespacestd; - - intL, N, K; -vector<Long Long> fl, FR;//F[i]: Minimum cost of getting the first I apples + intMain () { - #ifndef Online_judge +Freopen ("inch","R", stdin); A //freopen ("Out", "w", stdout); at #endif - intT; -scanf"%d", &T); - while(t--) { -scanf"%d%d%d", &l, &n, &K); - fl.clear (); Fr.clear (); inFl.push_back (0);//forced to make fl[0]=0; -Fr.push_back (0);//forced to make fr[0]=0; to for(intI=0; i<n; i++) { + intA, B; -scanf"%d%d", &a, &b); the while(b--) { * if(A *2<L) Fl.push_back (a); $ ElseFr.push_back (la);Panax Notoginseng } - } the sort (Fl.begin (), Fl.end ()); + sort (Fr.begin (), Fr.end ()); A for(intI=k; i< (int) Fl.size (); i++) fl[i]+=fl[i-K]; the for(intI=k; i< (int) Fr.size (); i++) fr[i]+=fr[i-K]; + Long LongAns= (Fl.back () +fr.back ()) <<1; - for(intx=0; x<=k; X + +) {//on the left, there's X left, and the right side is k-x an apple. $ intA=max (0, (int) Fl.size ()-1-x); $ intB=max (0, (int) Fr.size ()-1-(K-x)); - Long LongTmp= (Fl[a]+fr[b]) <<1; -Ans=min (ans, l +tmp); the } -printf"%i64d\n", ans);Wuyi } the return 0; -}hdu5303
[2015HDU Multi-School League supplementary question]hdu5303 Delicious Apples