HDU 5303 Delicious Apples(思维题)

Delicious Apples

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 555    Accepted Submission(s): 180


Problem Description
There are 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, clockwise from position 0. There are ai delicious apple(s) on the ith tree.

You only have a basket which can contain at most K apple(s). You are 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?

1n,k105,ai1,a1+a2+...+an105
1L109
0x[i]L

There are less than 20 huge testcases, and less than 500 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



    题意:一个周长为l的圆,里面有m棵树,每棵树上分别有果子,如今有一个篮子最多能放k个苹果。起点在0处,问从起点出发把全部的苹果全都摘回来须要的最短距离是多少。
    思路:以为有的须要绕圆一圈摘到的果子的距离会比以摘到果子原路返回要更加优,想想就知道绕一圈的情况最多仅仅有一次,假设以整个树作为处理的对象非常难决定,所以能够将每个苹果作为处理的对象,由于最多有100000个果子,这种话分别处理两边的果子,寻找里面的最小的一个值就是要求的解。



#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

int l,m,k;
int pl[100100],pr[100001];
int tree,num;
int pcnt[100010];
__int64 len_l[100010],len_r[100100];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(len_l,0,sizeof(len_l));
        memset(len_r,0,sizeof(len_r));
        int ans = 0;
        scanf("%d%d%d",&l,&m,&k);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&tree,&num);
            for(int j=0;j<num;j++)
            {
                pcnt[ans++] = tree;
            }
        }
        int lsize = 0,rsize = 0;
        for(int i=0;i<ans;i++)
        {
            if(pcnt[i]*2<l)
            {
                pl[lsize++] = pcnt[i];
            }
            else
            {
                pr[rsize++] = l-pcnt[i];
            }
        }
        sort(pl,pl+lsize);
        sort(pr,pr+rsize);
        for(int i=0;i<lsize;i++)
        {
            len_l[i+1] = i+1<=k? pl[i]:len_l[i+1-k]+pl[i];
        }
        for(int i=0;i<rsize;i++)
        {
            len_r[i+1] = i+1<=k? pr[i]:len_r[i+1-k]+pr[i];
        }
        __int64 sum = (len_l[lsize] + len_r[rsize])*2;
        for(int i=0;i<=k;i++)
        {
            if(i>lsize)
            {
                break;
            }
            int ll = lsize - i;
            int rr = max(0,rsize-(k-i));
            sum = min(sum,(len_l[ll] + len_r[rr])*2 + l);
        }
        printf("%I64d
",sum);
    }
    return 0;
}


 

原文地址:https://www.cnblogs.com/llguanli/p/6789974.html