Delicious Apples

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1371    Accepted Submission(s): 448

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
 
Author
XJZX
 
Source

题意:给定一个环,以下标为处为起始点,距离起始点Xi位置种植一颗苹果树,该树有a个苹果,篮子的最大容量为K,那么求摘完全部苹果所需的最短距离。

当时大致知道方向,但是没想到具体方法。首先贪心左右半边,推出len-k时的最短路程,最后则可以直接走一圈取完。然后再与左右分别取相比较,取较小。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX 100050
long long sum_left[MAX];
long long sum_right[MAX];
int l[MAX],r[MAX]; long long ans;
int Left,Right;
int n,k,len,T,a,b; int main()
{
scanf("%d",&T);
while(T--)
{
memset(sum_left,0,sizeof(sum_left));
memset(sum_right,0,sizeof(sum_right)); scanf("%d%d%d",&len,&n,&k);
Left=0,Right=0;
for(int i=0; i<n; i++)
{
scanf("%d%d",&a,&b);
for(int j=0; j<b; j++)
{
if(a*2<len)
l[++Left]=a;
else
r[++Right]=len-a;
}
}
sort(l+1,l+Left+1);
sort(r+1,r+Right+1);
for(int i=1; i<=Left; i++)
{
if(i<=k)
sum_left[i]=l[i];
else
sum_left[i]=sum_left[i-k]+l[i];
}
for(int i=1; i<=Right; i++)
{
if(i<=k)
sum_right[i]=r[i];
else
sum_right[i]=sum_right[i-k]+r[i];
}
ans=(sum_left[Left]+sum_right[Right])*2; for(int i=0; i<=k && i <= Left; i++)
{
long long lll = (sum_left[Left-i]+sum_right[max(0,Right-(k-i))])*2;
ans=min(ans,len+lll);
}
printf("%I64d\n",ans);
}
return 0;
}

  

2015 多校联赛 ——HDU5303(贪心)的更多相关文章

  1. 2015 多校联赛 ——HDU5360(贪心+优先队列)

    Sample Input 4 8 4 1 3 2 2 1 0 3 5 3 6 4 2 1 7 6 8 3 3 2 0 5 0 3 6 4 5 2 7 7 6 7 6 8 2 2 3 3 3 0 0 2 ...

  2. 2015 多校联赛 ——HDU5334(构造)

    Virtual Participation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  3. 2015 多校联赛 ——HDU5302(构造)

    Connect the Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  4. 2015 多校联赛 ——HDU5294(最短路,最小切割)

    Tricks Device Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  5. 2015 多校联赛 ——HDU5325(DFS)

    Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Tota ...

  6. 2015 多校联赛 ——HDU5316(线段树)

    Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an ...

  7. 2015 多校联赛 ——HDU5323(搜索)

    Solve this interesting problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  8. 2015 多校联赛 ——HDU5319(模拟)

    Painter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  9. 2015 多校联赛 ——HDU5301(技巧)

    Your current task is to make a ground plan for a residential building located in HZXJHS. So you must ...

随机推荐

  1. IQKeyboardManager使用方法

    使用方法: 将IQKeyboardManager 和 IQSegmentedNextPrevious类文件加进项目中.在AppDelegate文件中写下以下一行代码: [IQKeyBoardManag ...

  2. listview 与 button 焦点 在item添加下列属性

    android:descendantFocusability="blocksDescendants" http://zhaojianping.blog.51cto.com/7251 ...

  3. 【iOS】swift 让程序挂起后,能在后台继续运行任务

    1,程序的挂起和退出 由于iOS设备资源有限.当用户点击了home键,或者另一个应用程序启动了.那么原先那个程序便进入后台被挂起,不是退出,只是停止执行代码,同时它的内存被锁定.当应用程序恢复时,它会 ...

  4. 原生ajax的请求函数

    ajax:一种请求数据的方式,不需要刷新整个页面:ajax的技术核心是 XMLHttpRequest 对象:ajax 请求过程:创建 XMLHttpRequest 对象.连接服务器.发送请求.接收响应 ...

  5. java 注解的实现机制

    一.什么是注解: 注解是标记,也可以理解成是一种应用在类.方法.参数.属性.构造器上的特殊修饰符.注解作用有以下三种: 第一种:生成文档,常用的有@param@return等. 第二种:替代配置文件的 ...

  6. java 1.7新特性

    try( ... ){ ... } catch(xxx e){ ... } java1.7特性,叫做try-with-resource,实现了AutoCloseable接口的实例可以放在try(... ...

  7. 关于网页设计的css+html相对定位和决定定位的理解

    css中有很多定位,其中最重要的是相对定位和绝对定位: 定位很重要,不搞好,网页就会很乱,显示的完全不是自己想要的效果,自己必须掌握: 首先说一个重要的结论:绝对定位,是不占位置的,总是相对离自己最近 ...

  8. EasyUI DataGrid 实现单行/多行编辑功能

    要实现 EasyUI DataGrid 的可编辑很简单,在需要编辑的列添加 editor [编辑器]就可以了. 单行编辑 // 初始化数据列表 function initDatagrid() { $( ...

  9. 新概念英语(1-69)The car race

    新概念英语(1-69)The car race Which car was the winner in 1995 ? There is  car race near our town every ye ...

  10. 新概念英语(1-55)The Sawyer family

    新概念英语(1-55)The Sawyer family When do the children do their homework? The Sawyers live at 87 King Str ...