HDU 5303 Delicious Apples(思维题)
Delicious Apples
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 555 Accepted Submission(s): 180
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.
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.
2
10 3 2
2 2
8 2
5 1
10 4 1
2 2
8 2
5 1
0 10000
18
26
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h> using namespace std; int l,m,k;
int pl[100100],pr[100001];
int tree,num;
int pcnt[100010];
__int64 len_l[100010],len_r[100100]; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(len_l,0,sizeof(len_l));
memset(len_r,0,sizeof(len_r));
int ans = 0;
scanf("%d%d%d",&l,&m,&k);
for(int i=0;i<m;i++)
{
scanf("%d%d",&tree,&num);
for(int j=0;j<num;j++)
{
pcnt[ans++] = tree;
}
}
int lsize = 0,rsize = 0;
for(int i=0;i<ans;i++)
{
if(pcnt[i]*2<l)
{
pl[lsize++] = pcnt[i];
}
else
{
pr[rsize++] = l-pcnt[i];
}
}
sort(pl,pl+lsize);
sort(pr,pr+rsize);
for(int i=0;i<lsize;i++)
{
len_l[i+1] = i+1<=k? pl[i]:len_l[i+1-k]+pl[i];
}
for(int i=0;i<rsize;i++)
{
len_r[i+1] = i+1<=k? pr[i]:len_r[i+1-k]+pr[i];
}
__int64 sum = (len_l[lsize] + len_r[rsize])*2;
for(int i=0;i<=k;i++)
{
if(i>lsize)
{
break;
}
int ll = lsize - i;
int rr = max(0,rsize-(k-i));
sum = min(sum,(len_l[ll] + len_r[rr])*2 + l);
}
printf("%I64d\n",sum);
}
return 0;
}
HDU 5303 Delicious Apples(思维题)的更多相关文章
- HDU 5303 Delicious Apples (贪心 枚举 好题)
Delicious Apples Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- 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 ...
- HDU 5303 Delicious Apples (2015多校第二场 贪心 + 枚举)
Delicious Apples Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- HDU 5303 Delicious Apples(贪心 + 背包 2015多校啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 Problem Description There are n apple trees plan ...
- HDU 5303 Delicious Apples 美味苹果 (DP)
题意: 给一个长为L的环,起点在12点钟位置,其他位置上有一些苹果,每次带着一个能装k个苹果的篮子从起点出发去摘苹果,要将全部苹果运到起点需要走多少米? 思路: 无论哪处地方,只要苹果数超过k个,那么 ...
- [多校2015.02.1004 dp] hdu 5303 Delicious Apples
题意: 在一个长度为L的环上有N棵苹果树.你的篮子容量是K个苹果. 每棵苹果树上都有a[i]个苹果. 问你从0点出发最少要走多少距离能拿完所有的苹果. 思路: 我们考虑dp,dp[0][i]代表顺时针 ...
- hdu 5303 Delicious Apples
这道题贪心 背包 假设在走半圆之内能够装满,那么一定优于绕一圈回到起点.所以我们从中点将这个分开,那么对于每一个区间由于苹果数非常少,所以能够利用pos[x]数组记录每一个苹果所在的苹果树位置,然后将 ...
- 多校第二场 1004 hdu 5303 Delicious Apples(背包+贪心)
题目链接: 点击打开链接 题目大意: 在一个周长为L的环上.给出n棵苹果树.苹果树的位置是xi,苹果树是ai,苹果商店在0位置,人的篮子最大容量为k,问最少做多远的距离可以把苹果都运到店里 题目分析: ...
- hdu - 1172 猜数字 (思维题)
http://acm.hdu.edu.cn/showproblem.php?pid=1172 这个题换一种想法,可以找出四位数中所有满足条件的数看是否只有一个. #include <iostre ...
随机推荐
- mvn在线仓库搜索
使用Maven进行开发的时候,比较常见的一个问题就是如何寻找我要的依赖,比如说,我想要使用activeMQ,可是我不知道groupId,artifactId,和合适的version.怎么办呢?本文介绍 ...
- 通过修改VHD文件的位置来提升性能
昨天用VHD装了一个Win 10的预览版体验了一下,感觉磁盘操作非常慢,便用HD Tune测试了一下,发现速度只有物理硬盘的一半都不到. 这个倒大出我意料之外,由于VHD的便利性,我 ...
- UINavigationController 详解
// 导航控制器 // 1. 比较常用的视图控制器管理类 // 2. 以栈的形式管理视图控制器, 先进后出 // 3. 创建navigation后, 视图控制器上会多出一个导航栏 // 4. 导航栏高 ...
- JavaScript Dictionary
Excellent. The 4guysfromrolla example is very helpful, thanks. I've pasted a complete javascript.j ...
- 关于判断语句中如:while not xx: 或者:if not xx: 的含义及用法解析
关于判断语句中如:while not xx: 或者:if not xx: 的含义及用法解析 name='' while not name: name=raw_input(u'请输入姓名:') prin ...
- UML分析设计顺序
1.用例图:最简单的模型,与设计无关 2.活动图:类似流程图 3.用例图:对1的细化,分解后的Actor及Use Case 4.用例图:分解后的Actor及抽取的数据实体 5.类图:数据结构图 6.顺 ...
- netmap 编译
参考博客:http://www.cnblogs.com/ruo-yu/p/5083290.html 我用的内核版本是4.4.39 step 1:编译内核. step 2:根据参考博客并没有成功编译出我 ...
- VB 在Visio 2010 以编程方式创建子进程图
在2010年Visio以编程方式创建子进程图 Office 2010 https://msdn.microsoft.com/en-us/library/gg650651.aspx 简介: 学习如 ...
- CEdit自动换行和状态栏添加
CEdit自动换行在对话框的属性中是可以直接设置的. Auto HScroll设置为False Auto VScroll设置为True Mulitline设置为True Want Return设置为T ...
- vs2012升级到vs2013后,sql server 无法通过IP登录解决方案
因项目需要,vs升级到2013,可是数据库通过IP却无法连接,一直报内存异常,困扰许久,原来是.net框架出了问题,可以通过用管理员身份运行:netsh winsock reset解决!