码迷,mamicode.com
首页 > 移动开发 > 详细

Delicious Apples (hdu 5303 贪心+枚举)

时间:2015-07-24 12:58:11      阅读:208      评论:0      收藏:0      [点我收藏+]

标签: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 i技术分享th tree is planted at position x技术分享i技术分享技术分享, clockwise from position 0技术分享. There are a技术分享i技术分享技术分享 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,k10技术分享5技术分享,a技术分享i技术分享1,a技术分享1技术分享+a技术分享2技术分享+...+a技术分享n技术分享10技术分享5技术分享技术分享
1L10技术分享9技术分享技术分享
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 x技术分享i技术分享,a技术分享i技术分享技术分享.
 

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
 

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


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

Delicious Apples (hdu 5303 贪心+枚举)

标签:delicious apples   hdu 5303   贪心+枚举   

原文地址:http://blog.csdn.net/u014422052/article/details/47037273

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!