HDU 5303 Delicious Apples 2015 multi-school Joint training tournament 2 Dp+ Enumeration

Source: Internet
Author: User

Delicious Apples Time limit:5000/3000 MS (java/others) Memory limit:524288/524288 K (java/others)
Total submission (s): 311 Accepted Submission (s): 92


Problem Description There is n apple trees planted along a cyclic road, which is L metres long. Your Storehouse is built at position 0 on that cyclic road.
The ith tree is planted at position Xi and clockwise from position 0. There is AI delicious apple (s) on the ith tree.

You are only having a basket which can contain at the most K Apple (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?

1≤n,k≤105,ai≥1,a1+a2+...+an≤105
1≤l≤109
0≤x[i]≤l

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

Input first Line:t, the number of testcases.
Then T testcases follow. In each testcase:
First line contains three integers, l,n,k.
Next n lines, each line contains Xi,ai.
Output output total distance in a line for each testcase.
Sample Input

2 10 3 2 2 2 8 2 5 1 10 4 1 2 2 8 2 5 1 0 10000
Sample Output
18 26
Source multi-university Training Contest 2


Test instructions

There is a circle where there is a certain number of apples in different places, and the apples are at a distance from the beginning. The initial position is at 0 points. There is a basket that can be loaded with K apples at a time. Ask, to put all the apples in baskets to the starting point, at least to go how far away.


Analysis:

The total number of apples <= 100000. Then sort each apple sequentially.

Press clockwise, and counter-clockwise to handle separately.

Dp[0][i] means that you can only walk clockwise, counterclockwise back to the starting point. Take the clockwise first I apples back to the origin of the minimum distance

So dp[0][i] = Dp[0][i-k] + 2*dist[i]-----------Dist[i] denotes the distance of the first Apple clockwise distance from the beginning

The same anti-clockwise transfer equation is

Dp[1][i] = Dp[1][i+k] + (L-dist[i])

Then enumerate the I apples is the dividing line, I apples and before the apple is clockwise to obtain, i+1 and after the Apple is counterclockwise obtained

Then ans = min (ans,dp[0][i]+dp[1][i+1])

However, there is such a situation, take the first apple of the time, not the original way back, the second is to go straight forward to the end.

Then ans = min (ans,dp[0][i-k]+dp[1][i+1] + L)

Last ans is the answer.

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace
Std
    #define LL Long long #define MAXN 100007 struct point{int x;
int num;
};
Point p[100007];
Point PX[MAXN];
int CMP (point A,point B) {return a.x < b.x;} ll DP[2][MAXN];
    int main () {int t,l,n,k;
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d%d", &l,&n,&k);
        int cnt = 1,x,y;
        for (int i = 0;i < n; i++) {scanf ("%d%d", &px[i].x,&px[i].num);
        } sort (px,px+n,cmp);
            for (int i = 0;i < N, i++) {while (px[i].num--) {p[cnt++].x = px[i].x;
        }} memset (Dp[0],0,sizeof (LL) * (cnt+3));
        Memset (dp[1],0,sizeof (LL) * (cnt+3));
        for (int i = 1;i < cnt; i++) {Dp[0][i] = Dp[0][max (0,i-k)] + p[i].x * 2; } for (int i = cnt-1;i > 0; i--) {Dp[1][i] = Dp[1][min (cnt,i+k)] + (l-p[i].x) *2;
        } ll ans = min (dp[0][cnt-1],dp[1][1]);
            for (int i = 1;i < cnt; i++) {ans = min (ans,dp[0][i]+dp[1][i+1]);
        ans = min (Ans,dp[0][max (0,i-k)] + dp[1][i+1] + L);
    } printf ("%i64d\n", ans);
} return 0;
 }






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.