HDU 5303 Delicious Apples

Source: Internet
Author: User

This problem is a greedy backpack.
If it can be filled in a semicircle, then it must be better than a circle back to the starting point. So we're going to split this apart, so for each interval because of the small number of apples, you can use the Pos[x] array to record the apple tree location for each apple, and then sort the apples by their location, so we know that each time we fetch K apples, the price is the farthest place the Apple is located.
The main reason is why this choice is the best result.

For example, in a straight line.

POS Num
1 4
2 2
3 3
4 4

Basket size is 6
Then we can choose to take the position 4 and position 2 home to spend 8
The second time we took the location 3 home cost 6
The third time we took the location 1 home cost 2
So the total cost is 16.

If we take it in order, take the big one, like position 4, 4, Position 3, 2 cost 8.
Then with position 3 of one, position 2 of 2, Position 1 3 cost 6
Finally take the position 1 one spent 2
Total Cost is 16
We found no difference is the choice of data too water?

So what's the principle?

xyyy+z >k &&x+z<k  呢  是不是感觉先带走第一个和第三个是最优解我们可以看一下  带走第一个和第三个 那么总花费是 (z+(y+k-1)/k*y)*2如果继续按照顺序从后面往前取  花费是 ( z+(y-k+z+k-1)/k*y+(x-(y-k+z)%k))*2带入几组数 前一个式子>=后面的那么按照顺序从后面往前取的贪心策略是正确的

Note: At the end of the enumeration ring the remaining number of apples is less than or equal to K, the other side is likely to be 0, so when reduced, there may be negative

/ * **********************************************auther:xueaohuicreated time:2015-7-25 13:19:20File name:d.cpp *********************************************** */#include <iostream>#include <fstream>#include <cstring>#include <climits>#include <deque>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <utility>#include <sstream>#include <complex>#include <string>#include <vector>#include <cstdlib>#include <cstdio>#include <ctime>#include <bitset>#include <functional>#include <algorithm>using namespace STD;#define LL Long Long#define N 111111ll l;intN,k;intA[n];ll Dpx[n],dpy[n];ll Pos[n]; vector<ll>x, y;intMain () {intTscanf("%d", &t); while(t--)        {x.clear (); Y.clear ();memset(DPX,0,sizeof(DPX));memset(Dpy,0,sizeof(dpy));scanf("%i64d%d%d", &l,&n,&k);intCNT =0;intAll ll P; for(intI=1; i<=n;i++) {scanf("%i64d%d", &p,&all); for(intj=1;        j<=all;j++) pos[++cnt]=p; }//:p rintf ("cnt = =%d\n", cnt);         for(intI=1; i<=cnt;i++) {ll pp =pos[i]*2;if(pp<=l) X.push_back (Pos[i]);ElseY.push_back (L-pos[i]);       } sort (X.begin (), X.end ()); Sort (Y.begin (), Y.end ());intXsize = X.size ();intYsize = Y.size (); for(intI=1; i<=xsize;i++) {Dpx[i] = (i>k)? Dpx[i-k] + x[i-1]:x[i-1]; } for(intI=1; i<=ysize;i++) {Dpy[i] = (i>k)? Dpy[i-k] + y[i-1]:y[i-1];       } ll ans = dpx[xsize] + dpy[ysize]; Ans *=2; for(intI=0; i<=k&&i<=xsize;i++) {intc = Max (0, ysize+i-k);//Attention Pointans = min (ans, (dpx[xsize-i] + dpy[c]) *2+L); }printf("%i64d\n", ans); }}

Copyright notice: All brothers, please feel free to reprint, please indicate who is the brother

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.