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



题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303



题目大意:有一个长为L的环。n个苹果树,一个篮子最多装k个苹果,装完要回到起点卸下再出发。给出n个苹果树顺时针的位置及苹果的个数。求摘全然部苹果走的最小路程



题目分析:显然。仅仅有在某种特殊条件下,即两側都还有苹果且能够一次装完且最后的苹果都离起点比較远。这样的情况下。我们直接绕圈可能会更优,也就是说整圈最多绕一次。因此我们能够先对两边贪心。题目的数据显示苹果的数量最多就1e5,显然我们能够把苹果“离散”出来,用x[i]记录第i个苹果到起点的位置。然后对位置从小到大排序,先选择路程小的。选择的时候用dis[i]记录单側装了i个苹果的最小路程,类似背包计数的原理。答案要乘2,由于是来回的,最后在k>=i时。枚举绕整圈的情况,szl-i表示仅仅走左边採的苹果数,szr -
(k - i)表示仅仅走右边採的苹果树。画个图就能看出来了。注意右边这里可能值为负。要和0取最大,然后答案就是(disl[szl-i] + disr[szr - (k - i)])* 2 +L,这里事实上绘图更加直观。最后取最小就可以,注意有几个wa点,一个是要用long long。二是之前说的出现负数和0取大,三是每次要清零

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;
int const MAX = 1e5 + 5;
int L, n, k;
ll x[MAX], disl[MAX], disr[MAX];
vector <ll> l, r; int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(disl, 0, sizeof(disl));
memset(disr, 0, sizeof(disr));
l.clear();
r.clear();
scanf("%d %d %d", &L, &n, &k);
int cnt = 1;
for(int i = 1; i <= n; i++)
{
ll pos, num;
scanf("%lld %lld", &pos, &num);
for(int j = 1; j <= num; j++)
x[cnt ++] = (ll) pos; //离散操作
}
cnt --;
for(int i = 1; i <= cnt; 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 szl = l.size(), szr = r.size();
for(int i = 0; i < szl; i++)
disl[i + 1] = (i + 1 <= k ? l[i] : disl[i + 1 - k] + l[i]);
for(int i = 0; i < szr; i++)
disr[i + 1] = (i + 1 <= k ? r[i] : disr[i + 1 - k] + r[i]);
ll ans = (disl[szl] + disr[szr]) * 2;
for(int i = 0; i <= szl && i <= k; i++)
{
int p1 = szl - i;
int p2 = max(0, szr - (k - i));
ans = min(ans, 2 * (disl[p1] + disr[p2]) + L);
}
printf("%I64d\n", ans);
}
}

HDU 5303 Delicious Apples (贪心 枚举 好题)的更多相关文章

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

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

  2. HDU 5303 Delicious Apples(思维题)

    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(贪心 + 背包 2015多校啊)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 Problem Description There are n apple trees plan ...

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

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

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

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

  7. hdu 5303 Delicious Apples

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

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

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

  9. HDU 5353—— Average——————【贪心+枚举】

    Average Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total S ...

随机推荐

  1. No-9.函数基础

    函数基础 目标 函数的快速体验 函数的基本使用 函数的参数 函数的返回值 函数的嵌套调用 在模块中定义函数 01. 函数的快速体验 1.1 快速体验 所谓函数,就是把 具有独立功能的代码块 组织为一个 ...

  2. PHP基于phpqrcode类生成二维码的方法详解

    前期准备: 1.phpqrcode类文件下载,下载地址:https://sourceforge.net/projects/phpqrcode/2.PHP环境必须开启支持GD2扩展库支持(一般情况下都是 ...

  3. OAuth2.0授权流程

    微信授权 第3步的微信授权中的scope字段: snsapi_base 静默授权,不弹出用户同意框,可直接获取成员的基础信息:    snsapi_userinfo:静默授权,弹出用户同意框,待用户同 ...

  4. Android获取屏幕大小(Px)

    private DisplayMetrics dm = new DisplayMetrics(); TextView tv; Button bu; @Override protected void o ...

  5. Root of AVL Tree

    04-树5 Root of AVL Tree(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, the ...

  6. python接口自动化-multipart/form-data上传图片

    前言 在提交表单操作的时候,经常会遇到图片上传的操作,图片上传是一个单独的接口,本篇以禅道为例,介绍如何上传图片 上传接口 1.以禅道上提交bug为例,在选择图片时,点确定按钮,就是上传图片了 2.用 ...

  7. 自定义UDF函数应用异常

    自定义UDF函数应用异常 版权声明:本文为yunshuxueyuan原创文章.如需转载请标明出处: http://www.cnblogs.com/sxt-zkys/QQ技术交流群:299142667 ...

  8. 2016 Multi-University Training Contest 7 solutions BY SYSU

    Ants 首先求出每个点的最近点. 可以直接对所有点构造kd树,然后在kd树上查询除本身以外的最近点,因为对所有点都求一次,所以不用担心退化. 也可以用分治做,同样是O(NlogN)的复杂度. 方法是 ...

  9. 转 Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)

    转自: http://www.cnblogs.com/huangcong/archive/2011/08/29/2158268.html 黄聪:Python 字符串操作(string替换.删除.截取. ...

  10. Notification通知创建

    Notification通知创建 由于通知是一个远程视图,所以创建通知在状态栏显示需要用到三个主要的对象: 一.PendingIntent对象,用来承载Intent对象的,Intent对象主要是定义通 ...