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
 
题意:给一个圈,设起点为0,给出周长为L,在圈中有 n 棵树,依次给出按顺时针方向与起点的距离,树上的苹果数。给出篮子大小(最多能装的苹果树)。问最少走多远可以采集完所有苹果。
 
思路:
以过起点的直径为界,以苹果树在左半圆和右半圆分别贪心,得到要走的距离。最后枚举最后走一整圈所采集的苹果(详见代码)。比较得出最小值。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 100050
int d_l[maxn],d_r[maxn],l,n,k,t,x,a,L,R;
long long tot_l[maxn],tot_r[maxn],ans,tmp;
int main(){
scanf("%d",&t);
while(t--){
L=R=;
scanf("%d%d%d",&l,&n,&k);
for(int i=;i<n;i++){
scanf("%d%d",&x,&a);
for(int j=;j<a;j++){
if(x*<l) d_l[++L]=x;
else d_r[++R]=l-x;
}
}
sort(d_l+,d_l+L+);
sort(d_r+,d_r+R+);
for(int i=;i<=L;i++){
if(i<=k) tot_l[i]=d_l[i];
else tot_l[i]=tot_l[i-k]+d_l[i];
}
for(int i=;i<=R;i++){
if(i<=k) tot_r[i]=d_r[i];
else tot_r[i]=tot_r[i-k]+d_r[i];
}
ans=(tot_l[L]+tot_r[R])*;
for(int i=;i<=k;i++){
tmp=(tot_l[L-i]+tot_r[max(,R-(k-i))])*;
ans=min(ans,l+tmp);
}
printf("%I64d\n",ans);
}
return ;
}

2015 多校赛 第二场 1004 hdu(5303)的更多相关文章

  1. 2015 多校赛 第二场 1006 (hdu 5305)

    Problem Description There are n people and m pairs of friends. For every pair of friends, they can c ...

  2. 2015 多校赛 第二场 1002 (hdu 5301)

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

  3. 2015 多校赛 第一场 1007 (hdu 5294)

    总算今天静下心来学算法.. Description Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu’s at the e ...

  4. 2015 多校赛 第一场 1002 (hdu 5289)

    Description Tom owns a company and he is the boss. There are n staffs which are numbered from 1 to n ...

  5. 2015 多校赛 第一场 1001 (hdu 5288)

    Description OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l&l ...

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

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

  7. 2019HDU多校赛第二场 H HDU 6598 Harmonious Army(最小割模型)

    参考博客https://blog.csdn.net/u013534123/article/details/97142191 #include<bits/stdc++.h> using na ...

  8. 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)

    以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...

  9. SCNU省选校赛第二场B题题解

    今晚的校赛又告一段落啦,终于"开斋"了! AC了两题,还算是满意的,英语还是硬伤. 来看题目吧! B. Array time limit per test 2 seconds me ...

随机推荐

  1. Eclipse之注释部分代码

    有时为了调试,部分代码需要注释掉,如果一行一行的加注释符特别麻烦. 一.快捷键:ctrl+/ 选择需要注释的部分,点Ctrl+/,这段代码前,自动加注释语. 取消注释也是按Ctrl+/ 步骤: 选择需 ...

  2. 安卓Queue的使用

    Queue的成员函数        add        增加一个元索                     如果队列已满,则抛出一个IIIegaISlabEepeplian异常       rem ...

  3. 读书笔记「Python编程:从入门到实践」_8.函数

    8.1 定义函数 def greet_user(): # def 来告诉Python你要定义一个函数.这是函数定义 """Hello World""& ...

  4. 08--C++拷贝构造函数详解

    C++拷贝构造函数详解 一. 什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是很简单的,例如: [c-sharp] view plain copy int a = 100; int b ...

  5. (转)PostGIS+QGIS+GeoServer+OpenLayers实现数据的存储、服务的发布以及地图的显示

    http://blog.csdn.net/gisshixisheng/article/details/41575833 标题比较长,主要呢是实现以下几点: 1.将shp数据导入到PostGIS中: 2 ...

  6. day37-2元类,单例模式

    目录 元类 造类的第一种形式 class做了什么事 控制元类产生的类 控制元类产生的对象 实例化类 加上元类后类的属性查找顺序 元类控制模版 单例模式 1. 使用类方法的特性 2. 使用装饰器 3. ...

  7. Python学习教程(Python学习视频_Python学些路线):Day06 函数和模块的使用

    Python学习教程(Python学习视频_Python学些路线):函数和模块的使用 在讲解本章节的内容之前,我们先来研究一道数学题,请说出下面的方程有多少组正整数解. $$x_1 + x_2 + x ...

  8. 【JavaScript框架封装】自己动手封装一个涵盖JQuery基本功能的框架及核心源码分享(单文件版本)

    整个封装过程及阅读JQuery源码的过程基本上持续了一个月吧,最终实现了一个大概30%的JQuery功能的框架版本,但是里面涉及的知识点也是非常多的,总共的代码加上相关的注释大概在3000行左右吧,但 ...

  9. 使用timthumb.php截取文章缩略图

    wordpress自带的缩略图功能会对每次上传的所有图片根据设置的图片尺寸进行裁剪,并把原图和裁剪后的图片保存在网站空间中,图片只裁剪一次,更改设置的尺寸不会重新生成,这样不仅占用主机空间,以后改版网 ...

  10. 【ACM】hdu_1096_A+BVIII_201307261748

    A+B for Input-Output Practice (VIII)Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32 ...