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

题意:

一个长为 L 的环行路线。有 n 颗苹果树,每颗苹果树上有 a[i] 个苹果,一个人在0点(仓库的位置),他有一个篮子,篮子每次最多仅仅能装 k 个苹果。求要装全然部的苹果而且回到仓库的最小路程;给出的苹果树坐标是按顺时针的。

官方题解:

PS:

贪心。把环从中间分为两段。分左右两条线。

利用 a[i] 数组记录每一个苹果所在的苹果树的位置,之后再将苹果依照所在的位置进行排序一下。

所以我们就知道了每次摘 k 个苹果的路程是最远的那个苹果所在的位置。

再用 sum[i] 表示摘第 i 个苹果时的最小代价和。

依据背包的思想得到:

if ( i <= k )

sum[i] = d[i]

else

sum[i] = d[i] + sum[i-k]

注意:

另一种情况是在最后当剩下的苹果少于等于 k 个时,也许一次性绕环一圈拿完最后的k个所需的路程更少。

枚举剩下的最后k个!

代码例如以下:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
#define LL __int64
const int maxn = 100047;
vector <int> v1, v2;
LL a[maxn];
LL sum1[maxn], sum2[maxn]; int main()
{
int t;
int n, k, l;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&l,&n,&k);
int pos, num;
int h = 0;
for(int i = 0; i < n; i++)
{
scanf("%d%d",&pos,&num);
for(int j = 0; j < num; j++)
{
a[++h] = pos;
}
}
k = min(h,k);
v1.clear();
v2.clear();
for(int i = 1; i <= h; i++)
{
if(a[i]*2 < l)
{
v1.push_back(a[i]);
}
else
{
v2.push_back(l - a[i]);
}
}
memset(sum1,0,sizeof(sum1));
memset(sum2,0,sizeof(sum2));
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
int len1 = v1.size(), len2 = v2.size();
for(int i = 0; i < len1; i++)
{
int id = i+1;
if(id <= k)
{
sum1[id] = v1[i];
}
else
{
sum1[id] = v1[i]+sum1[id-k];
}
}
for(int i = 0; i < len2; i++)
{
int id = i+1;
if(id <= k)
{
sum2[id] = v2[i];
}
else
{
sum2[id] = v2[i]+sum2[id-k];
}
} LL ans = 2*(sum1[len1]+sum2[len2]);//来回
int t1, t2;
for(int i = 0; i <= k && i <= len1; i++)
{
t1 = len1 - i;
t2 = len2-(k-i);
if(t2 < 0)
{
t2 = 0;
}
ans = min(ans,2*(sum1[t1]+sum2[t2])+l);//最后不足k个绕行一圈所有摘走
}
printf("%I64d\n",ans);
}
return 0;
}

HDU 5303 Delicious Apples(贪心 + 背包 2015多校啊)的更多相关文章

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

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

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

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  3. 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/Other ...

  4. HDU 5303 Delicious Apples(思维题)

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

  5. hdu 5303 Delicious Apples

    这道题贪心 背包 假设在走半圆之内能够装满,那么一定优于绕一圈回到起点.所以我们从中点将这个分开,那么对于每一个区间由于苹果数非常少,所以能够利用pos[x]数组记录每一个苹果所在的苹果树位置,然后将 ...

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

    题目链接: 点击打开链接 题目大意: 在一个周长为L的环上.给出n棵苹果树.苹果树的位置是xi,苹果树是ai,苹果商店在0位置,人的篮子最大容量为k,问最少做多远的距离可以把苹果都运到店里 题目分析: ...

  7. [多校2015.02.1004 dp] hdu 5303 Delicious Apples

    题意: 在一个长度为L的环上有N棵苹果树.你的篮子容量是K个苹果. 每棵苹果树上都有a[i]个苹果. 问你从0点出发最少要走多少距离能拿完所有的苹果. 思路: 我们考虑dp,dp[0][i]代表顺时针 ...

  8. HDU 5303 Delicious Apples 美味苹果 (DP)

    题意: 给一个长为L的环,起点在12点钟位置,其他位置上有一些苹果,每次带着一个能装k个苹果的篮子从起点出发去摘苹果,要将全部苹果运到起点需要走多少米? 思路: 无论哪处地方,只要苹果数超过k个,那么 ...

  9. Hdu5303 Delicious Apples 贪心

    题目链接: HDU5303 题意: 有一条环形的长为L的路,仓库在位置0处, 这条路上有n棵苹果树,给出每棵苹果树的位置和苹果数量, 问用 一次最多能装K个苹果的篮子   把这条路上全部苹果採回仓库最 ...

随机推荐

  1. 解析浏览器和nodejs环境下console.log()的区别

    写在前面的 在开发调试过程中,我们经常需要调用console.log 方法来打印出当前变量的值,然而,console.log在浏览器环境下 有时会出现一些异常的现象 开撸代码 在浏览器和nodejs环 ...

  2. HDU-1358 Period 字符串问题 KMP算法 求最小循环节

    题目链接:https://cn.vjudge.net/problem/HDU-1358 题意 给一个字符串,对下标大于2的元素,问有几个最小循环节 思路 对每个元素求一下minloop,模一下就好 提 ...

  3. 12个Unity5中优化VR 应用的技巧

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/50176429 作者:car ...

  4. python __future__ 的几种特性

    今天看tensorflow的代码,看到python里面有这么几句: from __future__ import absolute_import from __future__ import divi ...

  5. lower_bound与upper_bound

    昨天一道题目用了lower_bound,大致了解了lower_bound指的是第一个>=x的位置.但是之前对于upper_bound有误解,其实upper_bound指的是第一个>x的位置 ...

  6. 本地代码中使用Java对象

    通过使用合适的JNI函数,你可以创建Java对象,get.set 静态(static)和 实例(instance)的域,调用静态(static)和实例(instance)函数.JNI通过ID识别域和方 ...

  7. CAP定理在分布式系统设计中的最新应用

    本文翻译自国外InfoQ和计算机杂志上一篇2012年旧文,本文就有关数据同步进行了讨论,特别关注业务事务的不变性与一致性如何在分布式系统中巧妙保证,探讨了长时间运行的事务的补偿机制.这些对分布式系统设 ...

  8. Random words

    To choose a random word from the histogram, the simplest algorithm is to build a list with multiple ...

  9. 有关 IE11

    IE10/IE11 中 99% 的 CSS3 属性不需要写 -ms- 前缀: IE9/IE10/IE11 默认开启 GPU 加速,效果比同期 Chrome 版本(如 35)流畅: IE11 的字体渲染 ...

  10. ItemTouchHelper(实现RecyclerView上添加拖动排序与滑动删除的所有事情)

    简单介绍: ItemTouchHelper是一个强大的工具,它处理好了关于在RecyclerView上添加拖动排序与滑动删除的所有事情.它是RecyclerView.ItemDecoration的子类 ...