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 ...
随机推荐
- 【很变态】PHP类实例化对象竟然可以访问类的“静态(static)方法”!!!
之前发现一个PHP的变态问题:PHP中静态(static)调用非静态方法详解 这次看了下 ThinkPHP 的源码 function.inc.php ,里面有个函数: /** * 取得对象实例 支持调 ...
- Vue 小项目的最佳实践
项目简介 目前一期只是为App内某个模块资讯模块文章的分享和APP下载,后续还会有更多的功能,为了项目可扩展.可伸缩结合了我以前的实践搭建了此项目项目地址,如果这个简单的项目能给您带来帮助请给小哥哥⭐ ...
- linux环境php将word转换成pdf
原文地址:http://www.niu12.com/article/15 ubuntu.java环境.openoffice.jodConverter.php 1.安装java环境 a. jdk下载(我 ...
- sonar如何添加自定义JAVA规则
参考: 1.https://segmentfault.com/a/1190000008659108 2.https://docs.sonarqube.org/display/DEV/Adding+Co ...
- C语言-对一个结构体中的字段进行排序
这是帮别人做的一个题目,好久没有接触过C语言了.有点发怵,只是似乎找回点当时学C语言,做课程设计的感觉. 题目:定义一个数组(学生结构体数组),里面包括学号.姓名.身份证和三科学生成绩.要求写一个函数 ...
- Delphi中Frame的使用方法(1)
Frame是组件面板上的第一个组件,但不是每个人都知道怎么用它,因为它不像Button和Label一样简单明了.实际上,Frame按钮只是打开一个Frame的列表,如果你没有创建任何的Frame,自然 ...
- PHP之is_a()函数执行代码之总结
今天看到云舒在群里贴的漏洞公告,原始的文章在 http://www.byte.nl/blog/2011/09/23/security-bug-in-is_a-function-in-php-5-3-7 ...
- Javascript函数式编程的一些例子[转载]
函数式编程风格 通常来讲,函数式编程的谓词(关系运算符,如大于,小于,等于的判断等),以及运算(如加减乘数等)都会以函数的形式出现,比如: a > b通常表示为: gt(a, b)/ ...
- HDU5312 Sequence
题意:t组数据,每组数据给个m.问m最少能由几项形如3*n*(n-1)+1的数表示 eg 7=1(n=1)+1(n=1)+1(n=1)+1(n=1)+1(n=1)+1(n=1)+1(n=1); ...
- Android跟踪NDK崩溃信息
1.NDK编译jni时增加调试信息: $NDK_ROOT/ndk-build -B NDK_DEBUG=1 2.发生崩溃时,logcat中收集日志: - ::): Added shared lib / ...