Delicious Apples (hdu 5303 贪心+枚举)
Delicious Apples
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 395 Accepted Submission(s): 122
apple trees planted along a cyclic road, which is
L
metres long. Your storehouse is built at position
0
on that cyclic road.
The i
tree is planted at position x
clockwise from position 0
There are a
delicious apple(s) on the i
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
,a
i
≥1,a
1
+a
2
+...+a
n
≤10
5
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 x
2
10 3 2
2 2
8 2
5 1
10 4 1
2 2
8 2
5 1
0 10000
18
26
pid=5309" target="_blank">5309
5308 5307 5306 5305题意:在一个圆上有n个苹果树。告诉苹果树的位置和每棵树上的苹果个数,另一个容量为K的篮子,用篮子去摘苹果,起点在位置0,重复去摘直到把全部的苹果都摘回到0,问走的最短距离为多少。
思路:首先将圆一分为二,在圆形两側能拿满的话肯定就是仅仅走半边再回去,这样比走整圈划算。另外还要想到最后两边都不足K个了。这个时候最多须要走一个整圈,我们不知道这个整圈拿了哪几个苹果,那么就枚举K个。
比赛时仅仅是想到了贪心,最后那一部分没有枚举,另外这里的苹果进行了离散化,由于苹果总数仅仅有1e5,大大简化了代码。自己当时写的太冗余=-=
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std; #define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 1e5+10;
const int MAXN = 2005;
const int N = 1005; ll Len,n,k,cnt;
ll pos[maxn],dp_l[maxn],dp_r[maxn]; //dp[i]表示拿完前i个苹果要走的的距离和
vector<ll>posl,posr; int main()
{
#ifndef ONLINE_JUDGE
freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
ll i,j,t;
scanf("%lld",&t);
ll x,num;
while (t--)
{
cnt=1;
scanf("%lld%lld%lld",&Len,&n,&k);
posl.clear();
posr.clear();
for (i=0;i<n;i++)
{
scanf("%lld%lld",&x,&num);
for (j=0;j<num;j++)
pos[cnt++]=x;
}
cnt--;
k=min(k,cnt);
for (i=1;i<=cnt;i++)
{
if (2*pos[i]<Len)
posr.push_back(pos[i]);
else
posl.push_back(Len-pos[i]);
}
sort(posl.begin(),posl.end());
sort(posr.begin(),posr.end());
dp_l[0]=dp_r[0]=0;
for (i=0;i<(ll)posl.size();i++)
{
if (i+1<=k)
dp_l[i+1]=posl[i];
else
dp_l[i+1]=dp_l[i+1-k]+posl[i];
}
for (i=0;i<(ll)posr.size();i++)
{
if (i+1<=k)
dp_r[i+1]=posr[i];
else
dp_r[i+1]=dp_r[i+1-k]+posr[i];
}
ll ans=(dp_l[posl.size()]+dp_r[posr.size()])*2;
for (i=0;i<=posr.size()&&i<=k;i++)
{
ll right=posr.size()-i;
ll left=max((ll)0,(ll)(posl.size()-(k-i)));
ans=min(ans,(ll)Len+(dp_l[left]+dp_r[right])*2);
}
printf("%lld\n",ans);
}
return 0;
}
Delicious Apples (hdu 5303 贪心+枚举)的更多相关文章
- 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 ...
- 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 ...
- hdu5303(2015多校2)--Delicious Apples(贪心+枚举)
Delicious Apples Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- HDU 5303 Delicious Apples(思维题)
Delicious Apples Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- [2015hdu多校联赛补题]hdu5303 Delicious Apples
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 题意:在一个长为L的环形路径上种着一些苹果树,告诉你苹果树的位置(题目中以0~L指示坐标)及苹果 ...
- dp - 2015 Multi-University Training Contest 2 1004 Delicious Apples
Delicious Apples Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5303 Mean: 一条长为L的环形路上种着n棵 ...
- 解题报告 之 HDU5303 Delicious Apples
解题报告 之 HDU5303 Delicious Apples Description There are n apple trees planted along a cyclic road, whi ...
- POJ 1018 Communication System 贪心+枚举
看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...
随机推荐
- django-2的路由层(URLconf)
URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于客户端发来的某个URL调用哪一段逻辑代码 ...
- canvas 和 svg
对于开发人员而言,最直观的区别在于:1.对于画在Canvas上的部件,你需要处理重绘.而SVG则不用,你修改svg dom则系统会自动帮你重绘2.Hittest,即canvas不负责帮你侦测鼠标/触摸 ...
- JS排序之快速排序
JS排序之快速排序 一个数组中的数据,选择索引为(2/数组长度)的那个数据作为基数,数组中的其他数据与它对比,比它数值小的放在做数组,比它数值大的放在右数组,最后组合 左数组+基数+右数组,其中,左数 ...
- Android fragment的切换(解决REPLACE的低效)
在项目中切换Fragment,一直都是用replace()方法来替换Fragment.但是这样做有一个问题,每次切换的时候Fragment都会重新实列化,重新加载一次数据,这样做会非常消耗性能用用户的 ...
- Android 微博sdk接入授权指南
1:首先在微博官方注册账号,官方地址是:http://open.weibo.com/然后创建一个新应用. 2:当然我们得现在自己电脑上创建一个应用,例如包名叫com.winorout.weib ...
- PHP CURL的几种用法
1.抓取无访问控制文件 <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://localhost/mytest/ ...
- dispatch_sync:As an optimization, this function invokes the block on the current thread when possible
两件事情: 1.是否是一个线程: 2.queue task 的目标线程是否有未完成的task. 模型:一个线程处理当前的task还有通过gc d派发来的待执行task. 猜测: 如果目标thread上 ...
- Memcached 之取模与哈希算法命中率实验
当5台memcache服务器中有一台宕机时的命中率实验. 一.php实现代码 1. config.php $server = array( "A" => array(&quo ...
- SGU495Kids and Prizes 数学期望
题意: 有n个奖品,m个人排队来选礼物,对于每个人,他打开的盒子,可能有礼物,也有可能已经被之前的人取走了,然后把盒子放回原处.为最后m个人取走礼物的期望. 题解: 本道题与之前的一些期望 DP 题目 ...
- ListUtil集合操作常用方法类
* 集合操作常用方法类. * <p> * * @author 柯 */ public class ListUtil { /** * 判断List不为空,非空返回true,空则返回false ...