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
long. Your storehouse is built at position  on
that cyclic road.

The th
tree is planted at position ,
clockwise from position .
There are  delicious
apple(s) on the th
tree.

You only have a basket which can contain at most  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?

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

Input

First line: ,
the number of testcases.

Then  testcases
follow. In each testcase:

First line contains three integers, .

Next  lines,
each line contains .

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

解题思路:

注意到,最多仅仅有一次会绕整个圈走一次。因此,先贪心的处理左半环和右半环。然后枚举绕整圈的时候从左側摘得苹果和从右側摘得苹果的数目。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#define LL long long
using namespace std;
const int MAXN = 100000 + 10;
int L, N, K;
LL x[MAXN];
LL ld[MAXN], rd[MAXN];
vector<LL>l, r;
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d%d", &L, &N, &K);
        l.clear(); r.clear();
        int pos, num, m = 0;
        for(int i=1;i<=N;i++)
        {
            scanf("%d%d", &pos, &num);
            for(int i=1;i<=num;i++)
                x[++m] = (LL)pos;
        }
        for(int i=1;i<=m;i++)
        {
            if(2 * x[i] < L) l.push_back(x[i]);
            else r.push_back(L - x[i]);
        }
        sort(l.begin(), l.end()); sort(r.begin(), r.end());
        int lsz = l.size(), rsz = r.size();
        memset(ld, 0, sizeof(ld)); memset(rd, 0, sizeof(rd));
        for(int i=0;i<lsz;i++)
            ld[i + 1] = (i + 1 <= K ? l[i] : ld[i + 1 - K] + l[i]);
        for(int i=0;i<rsz;i++)
            rd[i + 1] = (i + 1 <= K ? r[i] : rd[i + 1 - K] + r[i]);
        LL ans = (ld[lsz] + rd[rsz]) * 2;
        for(int i=0;i<=lsz&&i<=K;i++)
        {
            int p1 = lsz - i;
            int p2 = max(0, rsz-(K-i));
            ans = min(ans, 2*(ld[p1] + rd[p2]) + L);
        }
        cout << ans << endl;
    }
    return 0;
}
时间: 2024-08-26 06:57:09

HDU 5303 Delicious Apples (2015多校第二场 贪心 + 枚举)的相关文章

hdu 5305 Friends(2015多校第二场第6题)记忆化搜索

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5305 题意:给你n个人,m条关系,关系可以是online也可以是offline,让你求在保证所有人online关系的朋友和offline关系的朋友相等的情况下,这样的情况有多少种. 思路:因为online关系和offline关系的人数相等,而且m最多才28,所以只要枚举每个人的一半的关系是否符合要求即可,而且根据题意m是奇数或者有一个人的总关系为奇数那么就没有符合要求的情况,这样可以排除很多情况.

HDU 5301 Buildings(2015多校第二场)

Buildings Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 759    Accepted Submission(s): 210 Problem Description Your current task is to make a ground plan for a residential building located

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 (贪心 枚举 好题)

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多校啊)

题目链接: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,

多校第二场 1004 hdu 5303 Delicious Apples(背包+贪心)

题目链接: 点击打开链接 题目大意: 在一个周长为L的环上.给出n棵苹果树.苹果树的位置是xi,苹果树是ai,苹果商店在0位置,人的篮子最大容量为k,问最少做多远的距离可以把苹果都运到店里 题目分析: 首先我们能够(ˇ?ˇ) 想-,假设在走半圆之内能够装满,那么一定优于绕一圈回到起点.所以我们从中点将这个圈劈开.那么对于每一个区间由于苹果数非常少,所以能够利用belong[x]数组记录每一个苹果所在的苹果树位置,然后将苹果依照所在的位置排序,那么也就是我们知道每次拿k个苹果的代价是苹果所在的最远

[多校2015.02.1004 dp] hdu 5303 Delicious Apples

题意: 在一个长度为L的环上有N棵苹果树.你的篮子容量是K个苹果. 每棵苹果树上都有a[i]个苹果. 问你从0点出发最少要走多少距离能拿完所有的苹果. 思路: 我们考虑dp,dp[0][i]代表顺时针取i个苹果的最短距离. dp[1][i]代表逆时针取i个苹果的最短距离. 那么设苹果的总是为sum 那么ans=dp[0][i]+dp[sum-i]  (0<=i<=sum) 代码: #include"stdio.h" #include"algorithm"

2015多校第二场 1004( Delicious Apples )

题意:有一条圆形的路,你的仓库在0点,这条路长l,然后有n个苹果树,每个数的坐标是xi(顺时针),每棵树上有ai个苹果.你有个篮子,能装k个苹果,问你用这个篮子将所有苹果装回仓库所走的最短路为多少? 1≤n,k≤105,ai≥1,a1+a2+...+an≤105 1≤L≤109 0≤x[i]≤L 请特别注意上面的苹果个数的条件.因为我的学长 就是从这个条件成功做出了这道题,orz! 因为苹果数不超过10^5,所以将每个苹果当作点. 用一个数组pos记录每个苹果距0点的距离,排序后,然后就可以用另

hdu 5308 (2015多校第二场第9题)脑洞模拟题,无语

题目链接:http://acm.hdu.edu.cn/listproblem.php?vol=44 题意:给你n个n,如果能在n-1次运算之后(加减乘除)结果为24的输出n-1次运算的过程,如果不能输出-1. 思路:乍看起来,没什么规律,但是可以想象的是(n+n+n+n)/n=4,(n+n+n+n+n+n)/n=6,(n-n)*n*n*·····*n=0所以在n大于15的时候结果基本是固定的,只要对小于15的数一一输出就行(但是这题真是无语,算这种题目真是累,脑洞啊~~) 代码: 1 #incl