Delicious Apples

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

Total Submission(s): 321    Accepted Submission(s): 95

Problem Description
There are  apple
trees planted along a cyclic road, which is  metres
long. Your storehouse is built at position  on
that cyclic road.

The 

rev=2.4-beta-2" alt="" style="">th
tree is planted at position 

rev=2.4-beta-2" alt="" style="">,
clockwise from position .
There are 

rev=2.4-beta-2" alt="" style=""> delicious
apple(s) on the 

rev=2.4-beta-2" alt="" style="">th
tree.



You only have a basket which can contain at most 

rev=2.4-beta-2" alt="" style=""> 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?



rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">



There are less than 20 huge testcases, and less than 500 small testcases.

 
Input
First line: ,
the number of testcases.

Then  testcases
follow. In each testcase:

First line contains three integers, 

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">.

Next 

rev=2.4-beta-2" alt="" style=""> lines,
each line contains 

rev=2.4-beta-2" alt="" style="">

rev=2.4-beta-2" alt="" style="">.

 
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
 
解题思路:
注意到,最多仅仅有一次会绕整个圈走一次。因此,先贪心的处理左半环和右半环。然后枚举绕整圈的时候从左側摘得苹果和从右側摘得苹果的数目。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#define LL long long
using namespace std;
const int MAXN = 100000 + 10;
int L, N, K;
LL x[MAXN];
LL ld[MAXN], rd[MAXN];
vector<LL>l, r;
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d%d", &L, &N, &K);
l.clear(); r.clear();
int pos, num, m = 0;
for(int i=1;i<=N;i++)
{
scanf("%d%d", &pos, &num);
for(int i=1;i<=num;i++)
x[++m] = (LL)pos;
}
for(int i=1;i<=m;i++)
{
if(2 * x[i] < L) l.push_back(x[i]);
else r.push_back(L - x[i]);
}
sort(l.begin(), l.end()); sort(r.begin(), r.end());
int lsz = l.size(), rsz = r.size();
memset(ld, 0, sizeof(ld)); memset(rd, 0, sizeof(rd));
for(int i=0;i<lsz;i++)
ld[i + 1] = (i + 1 <= K ? l[i] : ld[i + 1 - K] + l[i]);
for(int i=0;i<rsz;i++)
rd[i + 1] = (i + 1 <= K ? r[i] : rd[i + 1 - K] + r[i]);
LL ans = (ld[lsz] + rd[rsz]) * 2;
for(int i=0;i<=lsz&&i<=K;i++)
{
int p1 = lsz - i;
int p2 = max(0, rsz-(K-i));
ans = min(ans, 2*(ld[p1] + rd[p2]) + L);
}
cout << ans << endl;
}
return 0;
}

HDU 5303 Delicious Apples (2015多校第二场 贪心 + 枚举)的更多相关文章

  1. hdu 5305 Friends(2015多校第二场第6题)记忆化搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5305 题意:给你n个人,m条关系,关系可以是online也可以是offline,让你求在保证所有人on ...

  2. HDU 5301 Buildings(2015多校第二场)

    Buildings Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota ...

  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. HDU 5303 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. HDU 5303 Delicious Apples(贪心 + 背包 2015多校啊)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5303 Problem Description There are n apple trees plan ...

  7. [多校2015.02.1004 dp] hdu 5303 Delicious Apples

    题意: 在一个长度为L的环上有N棵苹果树.你的篮子容量是K个苹果. 每棵苹果树上都有a[i]个苹果. 问你从0点出发最少要走多少距离能拿完所有的苹果. 思路: 我们考虑dp,dp[0][i]代表顺时针 ...

  8. 多校第二场 1004 hdu 5303 Delicious Apples(背包+贪心)

    题目链接: 点击打开链接 题目大意: 在一个周长为L的环上.给出n棵苹果树.苹果树的位置是xi,苹果树是ai,苹果商店在0位置,人的篮子最大容量为k,问最少做多远的距离可以把苹果都运到店里 题目分析: ...

  9. hdu 5308 (2015多校第二场第9题)脑洞模拟题,无语

    题目链接:http://acm.hdu.edu.cn/listproblem.php?vol=44 题意:给你n个n,如果能在n-1次运算之后(加减乘除)结果为24的输出n-1次运算的过程,如果不能输 ...

随机推荐

  1. Redis:持久化之RDB和AOF

    Redis:持久化之RDB和AOF RDB(Redis DataBase) 在指定的时间间隔内将内存中的数据集快照写入硬盘 也就是行话讲的Snapshot快照,它恢复时是将快照文件直接读到内存里. R ...

  2. xpee.vbs

    xpee.vbs Win 8安装之后每一次重启桌面都会有一个360浏览器的快捷方式,终于找到原因了, 在Windows/System下面有这么个文件: Set ws = CreateObject(&q ...

  3. 外网主机如何将数据包发送到共用一个公网IP的局域网某特定主机上的

    内网的一台电脑要上因特网对外开放服务或接收数据.都须要port映射.port映射分为动态和静态. 动态port映射:内网中的一台电脑要訪问站点.会向NAT网关发送数据包.包头中包含对方站点IP.por ...

  4. CSS文本简单设置

    文本的设置直接影响到用户对界面的感受,好的文本设置能够让用户对界面有一种赏心悦目的感受,在这地方我们来简单的说说说对文本设置的时候,有哪些格式. 文本设置的时候我们应该注意什么: 平时我们文本设置的时 ...

  5. php利用href进行页面传值的正确姿势

    首先在a.php中 <?php $a = "world"; echo "<a href='b.php?m=$a'>删除</a>"; ...

  6. IntelliJ IDEA中JAVA连接MYSQL

    1.下载mysql包 2.项目中引入mysql包 3.连接数据库,查询结果 看jdbc数据库连接类 package Facade; import java.sql.*; /** * Created b ...

  7. CoreData 从入门到精通(四)并发操作

    通常情况下,CoreData 的增删改查操作都在主线程上执行,那么对数据库的操作就会影响到 UI 操作,这在操作的数据量比较小的时候,执行的速度很快,我们也不会察觉到对 UI 的影响,但是当数据量特别 ...

  8. 实现一个类似360的button

    通过改写一个buttonst类,实现360效果的button. 主要可以完成:frame,hover,face效果,并且支持menu,tooltips 1)派生新的类QButton.添加虚函数,设置自 ...

  9. requireJS实现原理分析

    下面requireJS实现的基本思路  项目地址https://github.com/WangMaoling/require var require = (function(){ //框架版本基本信息 ...

  10. 你不知道的JavaScript演示代码Github地址

    你不知道的JavaScript博文相关代码托管至Github,每次写完博客会把代码提交上去. 代码地址:https://github.com/rongbo-j/you-dont-know-js 点击D ...