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 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

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

2015 Multi-University Training Contest 2

Recommend

wange2014   |   We have carefully selected several similar problems for you:  5309 5308 5307 5306 5305

题意:在一个圆上有n个苹果树,告诉苹果树的位置和每棵树上的苹果个数,还有一个容量为K的篮子,用篮子去摘苹果,起点在位置0,反复去摘直到把所有的苹果都摘回到0,问走的最短距离为多少。

思路:首先将圆一分为二,在圆形两侧能拿满的话肯定就是只走半边再回去,这样比走整圈划算,另外还要想到最后两边都不足K个了,这个时候最多需要走一个整圈,我们不知道这个整圈拿了哪几个苹果,那么就枚举K个。比赛时只是想到了贪心,最后那一部分没有枚举,另外这里的苹果进行了离散化,因为苹果总数只有1e5,大大简化了代码,自己当时写的太冗余=-=

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 1e5+10;
const int MAXN = 2005;
const int N = 1005;

ll Len,n,k,cnt;
ll pos[maxn],dp_l[maxn],dp_r[maxn]; //dp[i]表示拿完前i个苹果要走的的距离和
vector<ll>posl,posr;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
    ll i,j,t;
    scanf("%lld",&t);
    ll x,num;
    while (t--)
    {
        cnt=1;
        scanf("%lld%lld%lld",&Len,&n,&k);
        posl.clear();
        posr.clear();
        for (i=0;i<n;i++)
        {
            scanf("%lld%lld",&x,&num);
            for (j=0;j<num;j++)
                pos[cnt++]=x;
        }
        cnt--;
        k=min(k,cnt);
        for (i=1;i<=cnt;i++)
        {
            if (2*pos[i]<Len)
                posr.push_back(pos[i]);
            else
                posl.push_back(Len-pos[i]);
        }
        sort(posl.begin(),posl.end());
        sort(posr.begin(),posr.end());
        dp_l[0]=dp_r[0]=0;
        for (i=0;i<(ll)posl.size();i++)
        {
            if (i+1<=k)
                dp_l[i+1]=posl[i];
            else
                dp_l[i+1]=dp_l[i+1-k]+posl[i];
        }
        for (i=0;i<(ll)posr.size();i++)
        {
            if (i+1<=k)
                dp_r[i+1]=posr[i];
            else
                dp_r[i+1]=dp_r[i+1-k]+posr[i];
        }
        ll ans=(dp_l[posl.size()]+dp_r[posr.size()])*2;
        for (i=0;i<=posr.size()&&i<=k;i++)
        {
            ll right=posr.size()-i;
            ll left=max((ll)0,(ll)(posl.size()-(k-i)));
            ans=min(ans,(ll)Len+(dp_l[left]+dp_r[right])*2);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

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

时间: 2024-08-07 05:29:23

Delicious Apples (hdu 5303 贪心+枚举)的相关文章

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

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

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

HDU 5303(Delicious Apples- 环上折半dp+贪心)

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

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-

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

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

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

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: 

HDU 4932 贪心

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 191    Accepted Submission(s): 38 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by