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  i th tree is planted at position  xi , clockwise from position  0 . There are  ai  delicious apple(s) on the  i th 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\n",sum);
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值