HDU 5303 Delicious Apples (2015-school second greedy + enumeration)

Source: Internet
Author: User

Delicious Apples total Submission (s): 321    accepted submission (s): 95


Problem Descriptionthere isApple trees planted along a cyclic road, which ismetres long. Your Storehouse is built at positionOn that cyclic road.
TheTh tree is planted at position, clockwise from position. There isDelicious Apple (s) on theTh tree.

You are only having a basket which can contain at the mostApple (s). You is to start from your storehouse, pick all the apples and carry them back to your storehouse using your basket. What is your minimum distance travelled?





There is less than-huge testcases, and less than, small testcases.

Inputfirst Line:The number of testcases.
ThenTestcases follow. In each testcase:
First line contains three integers,.
NextLines, each line contains.
Outputoutput total distance in a line for each testcase.
Sample Input
210 3 22 28 25 110 4 12 28 25 10 10000

Sample Output
1826
The idea of solving problems: Notice that at most one time it will go around the whole circle. Therefore, first greedy to deal with the left and right half ring. It then enumerates the number of apples that are picked from the left side and picked from the right side around the entire circle.
#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <vector > #include <queue> #include <stack> #include <cmath> #include <algorithm> #define LL Long longusing namespace Std;const int maxn = 100000 + 10;int L, N, K; LL X[MAXN];    LL LD[MAXN], Rd[maxn];vector<ll>l, R;int main () {int T;    scanf ("%d", &t);        while (t--) {scanf ("%d%d%d", &l, &n, &k); L.clear ();        R.clear ();        int pos, num, m = 0;            for (int i=1;i<=n;i++) {scanf ("%d%d", &pos, &num);        for (int i=1;i<=num;i++) x[++m] = (LL) pos;            } for (int i=1;i<=m;i++) {if (2 * X[i] < L) L.push_back (X[i]);        else R.push_back (L-x[i]); } sort (L.begin (), L.end ());        Sort (R.begin (), R.end ());        int lsz = L.size (), Rsz = R.size (); memset (LD, 0, sizeof (LD));        memset (Rd, 0, sizeof (RD)); for (inti=0;i<lsz;i++) Ld[i + 1] = (i + 1 <= K L[i]: Ld[i + 1-k] + l[i]);        for (int i=0;i<rsz;i++) Rd[i + 1] = (i + 1 <= K R[i]: Rd[i + 1-k] + r[i]);        LL ans = (Ld[lsz] + Rd[rsz]) * 2;            for (int i=0;i<=lsz&&i<=k;i++) {int p1 = lsz-i;            int p2 = max (0, rsz-(k-i));        ans = min (ans, ld[p1] + rd[p2) + L);    } cout << ans << endl; } return 0;}


HDU 5303 Delicious Apples (2015-school second greedy + enumeration)

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.