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 ...
随机推荐
- sourceinsight常用快捷键
Alt+l 激活语法窗口 ,搜索语法(当前打开文件的函数.宏定义搜索输入区域), Alt+, Alt+. 分别表示后退工作区.前进工作区 shift+F5 标记一个单词 shift ...
- JS isNaN()函数
1.作用 isNaN() 函数用于检查其参数是否是非数字值. 2.JS <script> document.write(isNaN(123)); document.write(isNaN( ...
- war后缀的文件
其实war文件就是Java中web应用程序的打包.借用一个老兄的话,"当你一个web应用程序很多的时候,如果你想把它部署到别的机器上,来回拷这些文件是件挺郁闷的事情,如果要是一个文件就好了. ...
- [转]双TOP二分法生成分页SQL类(支持MSSQL、ACCESS)
本文转自:http://www.cnblogs.com/jitian/archive/2011/03/22/1991961.html 博客开张,先发以前的几个老物件儿,以前写下来的,现在发上来权当记录 ...
- ExtJS初学笔记---Ext.Msg.alert无效果
最近开始学ExtJS,书上的第一个例子是: 1 2 3 Ext.onReady(function(){ Ext.Msg.alert('Hello.', 'Hello'); }); 这个是Ext ...
- iOS:quartz2D绘图(显示绘制在PDF上的图片)
quart2D既可以用来绘制图像到pdf上,也可以从pdf上读取图像并显示出来.在使用这种方式之前,还有一种方式可以用来读取显示pdf上的图像,即使用UIWebView网页视图控件- (void)lo ...
- git 统计代码量 shell脚本
#!/bin/bash # 统计代码量 # 使用方法: sh gitstat.sh "2017-11-01" "2017-11-30" "JamKon ...
- [置顶] struts2+hibernate+spring整合(annotation版)
本博文使用struts2,hibernate,spring技术整合Web项目,同时分层封装代码,包含model层,DAO层,Service层,Action层. 在整合hibernate时使用annot ...
- An incompatible version 1.1.14 of APR based Apache Tomcat Native library is installed, while Tomcat
启动tomcat 7.0, 看到日志里出现严重警告, An incompatible version 1.1.14 of APR based Apache Tomcat Native library ...
- (剑指Offer)面试题37:两个链表的第一个公共结点
题目: 输入两个链表,找出它们的第一个公共结点. 链表结点的定义如下: struct ListNode{ int val; ListNode* next; ListNode(int x):val(x) ...