Hdu5303 Delicious Apples 贪心

题目链接:

HDU5303

题意:

有一条环形的长为L的路,仓库在位置0处,

这条路上有n棵苹果树,给出每棵苹果树的位置和苹果数量,

问用 一次最多能装K个苹果的篮子   把这条路上所有苹果采回仓库最少需要走的距离

解题思路:

这条路是环形的,先把果树分为两部分,圆的左半边算一部分,圆的右半边算另一部分

对所有苹果根据距离排序 , 用类似背包的思想,  统计左半边,右半边用 来回走(来回的长度一定小于一个圆环的周长)的方式采集完苹果所需要走的最少距离;

最后 考虑需要走一圈的情况:左边 多出k1( <K)个苹果,右边多出k2 (<k) 个苹果.

判断   来回拿k1所需走的距离+来回拿k2所需走的距离  和 圆周长L 的 大小关系即可得出相应答案

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define maxn 100050
using namespace std;
int dis[maxn],cnt;
int ldis[maxn],rdis[maxn];
int cnt1,cnt2;
LL lsum[maxn],rsum[maxn];

int cmp(int a,int b)
{
    return a<b;
}
int main()
{
//    freopen("in.txt","r",stdin);
    int T,m,n,k,a,b,L;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&L,&m,&k);
        cnt=cnt1=cnt2=0;
        while(m--)
        {
            scanf("%d%d",&a,&b);
            if(a==0)
                continue;
            if((a<<1)<=L)
                for(int i=1;i<=b;i++)
                    ldis[++cnt1]=a;
            else
                for(int i=1;i<=b;i++)
                    rdis[++cnt2]=L-a;
        }
        sort(ldis+1,ldis+cnt1+1,cmp);
        sort(rdis+1,rdis+cnt2+1,cmp);
        cnt=cnt1+cnt2;

        for(int i=1;i<=cnt1;i++)            //左半边 通过来回的方式拿完
            if(i<=k) lsum[i]=ldis[i];
            else  lsum[i]=ldis[i]+lsum[i-k];

        for(int i=1;i<=cnt2;i++)            //右半边.......
            if(i<=k) rsum[i]=rdis[i];
            else  rsum[i]=rdis[i]+rsum[i-k];

        LL ans=(lsum[cnt1]+rsum[cnt2])<<1;

        k=min(k,cnt);
        int l_,r_;
        for(int i=1;i<=cnt1&&i<=k;i++)     //枚举走一圈时,左半边拿的苹果数量
        {
            l_=cnt1-i,r_=max(0,cnt2-k+i);
            ans=min(ans,L+((lsum[l_]+rsum[r_])<<1));
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-09 16:29:11

Hdu5303 Delicious Apples 贪心的相关文章

[2015hdu多校联赛补题]hdu5303 Delicious Apples

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 题意:在一个长为L的环形路径上种着一些苹果树,告诉你苹果树的位置(题目中以0~L指示坐标)及苹果树上的苹果数,现在你要采集所有的苹果(大概是你有一个容量为K的筐,你采集的苹果要装在筐里,采集完要回到坐标0的家里清空筐),问你最少需要走多少路 解:可以想象最多需要走一次整环,剩下的是走两个半环,那么分别对两个半环贪心(由于数据量较小,有很优雅的姿势~) 然后考虑走的这一次整环,由于这次整环的花费

HDU 5303 Delicious Apples (贪心 枚举 好题)

Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 199    Accepted Submission(s): 54 Problem Description There are n apple trees planted along a cyclic road, which is L metres l

hdu5303(2015多校2)--Delicious Apples(贪心+枚举)

Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 587    Accepted Submission(s): 188 Problem Description There are n apple trees planted along a cyclic road, which is L metres

HDU 5303 Delicious Apples (2015多校第二场 贪心 + 枚举)

Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 321    Accepted Submission(s): 95 Problem Description There are  apple trees planted along a cyclic road, which is  metres lon

Delicious Apples (hdu 5303 贪心+枚举)

Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 395    Accepted Submission(s): 122 Problem Description There are n apple trees planted along a cyclic road, which is L metres

2015 Multi-University Training Contest 2 hdu 5303 Delicious Apples

Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1057    Accepted Submission(s): 354 Problem Description There are n apple trees planted along a cyclic road, which is L metres

2015 Multi-University Training Contest 2 1004 Delicious Apples

Delicious Apples Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5303 Mean: 一条长为L的环形路上种着n棵苹果数. 第i棵苹果数的位置在xi,有ai个苹果,苹果树的位置按顺时针顺序给出,且都是整数. 在0位置有一个仓库,有一个容量为k的篮子,需要使用这个篮子将所有苹果收到仓库中来,求全过程的最短路. analyse: 略 Time complexity: O(N) Source code: 

HDOJ 5303 Delicious Apples 枚举+DP

暴力枚举+DP 虽然是在环上,但最多只需要走一圈... dp[0][i]表示从1...i从起点逆时针走取完i个的花费,有 dp[0][i]=dp[0][i-k]+dist[i]*2 dp[1][i]表示从i...n从起点顺时针走取完n-i+1个的花费 dp[1][i]=dp[1][i+k]+(L-dist[i])*2 枚举哪些点顺时针哪些点逆时针: ans=min(ans,dp[0][i]+dp[1][i+1]); 然后枚举从哪个点开始走一圈:ans=min(ans,dp[0][max(0,i-

HDU 5303 Delicious Apples(贪心 + 背包 2015多校啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 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,