0
点赞
收藏
分享

微信扫一扫

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


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?

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

There are less than 20 huge testcases, and less than 500 small testcases.


 



Input


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


2015 Multi-University Training Contest 2





    题意:一个周长为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\n",sum);
    }
    return 0;
}





 


举报

相关推荐

0 条评论