Delicious Apples

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)

Total Submission(s): 395    Accepted Submission(s): 122

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
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  

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 贪心+枚举)的更多相关文章

  1. HDU 5303 Delicious Apples (贪心 枚举 好题)

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

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

    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. hdu5303(2015多校2)--Delicious Apples(贪心+枚举)

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

  5. HDU 5303 Delicious Apples(思维题)

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

  6. [2015hdu多校联赛补题]hdu5303 Delicious Apples

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 题意:在一个长为L的环形路径上种着一些苹果树,告诉你苹果树的位置(题目中以0~L指示坐标)及苹果 ...

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

  8. 解题报告 之 HDU5303 Delicious Apples

    解题报告 之 HDU5303 Delicious Apples Description There are n apple trees planted along a cyclic road, whi ...

  9. POJ 1018 Communication System 贪心+枚举

    看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...

随机推荐

  1. 通过Ajax和SpringBoot交互的示例

    转自:https://blog.csdn.net/oppo5630/article/details/52093898/

  2. php write excel

    /** * 写excel方法 */ function writeExcel($tabArr, $dataArr,$path) { require_once CODE_BASE2 . '/util/ph ...

  3. clone的rails目录下命令无效问题

    异常坑爹,在公司克隆自己的项目.然后在项目目录下rails s还有一大堆命令无效,提示 Usage: rails new APP_PATH [options]   找了半天总算找到解决办法了,在项目目 ...

  4. IPv6系列-初学者的10个常见困扰

    本文是<IPv6系列>文章的第二篇<常见困扰>,紧接<入门指南>,用于解答IPv6的10个常见困扰. 小慢哥的原创文章,欢迎转载 目录 ▪ 本文缘由 ▪ 困扰1. ...

  5. APPStore 发布流程及证书申请

    最新的 iOS 申请证书与发布流程(2016.7) 今天刚好客户定制的APP需要上架,也提供了新的开发者账号,所以就顺带把申请证书与发布流程写一遍. 证书是什么? 上面这个就是我们申请好证书后,下载到 ...

  6. POJ 3253 STL优先队列

    题目大意:FJ需要修补牧场的围栏,他需要 N 块长度为 Li 的木头(N planks of woods).开始时,FJ只有一块无限长的木板,因此他需要把无限长的木板锯成 N 块长度 为 Li 的木板 ...

  7. Oracle11g聚合函数

    聚合函数就是基于多行数据返回一行结果,下面就是Oracle提供的一些列聚合函数: AVG COLLECT CORR CORR_* COUNT COVAR_POP COVAR_SAMP CUME_DIS ...

  8. HTML5标签构成

    一个HTML5文件是由一些列的元素和标签组成的.元素是HTML5文件的重要组成部分,例如title(文件标题).img(图像)及table(表格)等.元素名不区分大小写,而HTML5用标签来规定元素的 ...

  9. Android贝塞尔曲线应用-跳动的水滴

    主要通过6个控制点实现. val startPoint = PointF() val endPoint = PointF() val control1 = PointF() val control2 ...

  10. 【PostgreSQL-9.6.3】psql常用命令

    命令 描述 \l 查看数据库 \c 换库 \d 查看所有表 \dt 只显示匹配的表 \di 只显示匹配的索引 \ds 只显示匹配的序列 \dv 只显示匹配的视图 \df 只显示匹配的函数 \d t1 ...