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. Linux while和for循环简单分析

    一.循环重定向 最近遇到了一种新的循环重定向写法,由于没看懂,说以网上搜索了一下,然后再此分享一下: while read line do ...... done  < file 刚开始看,不明 ...

  2. HDU_1711_初识KMP算法

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. 获取url后面的路径

    function GetUrlRelativePath() { var url = document.location.toString(); var arrUrl = url.split(" ...

  4. 【转】虚拟化(二):虚拟化及vmware workstation产品使用

    vmware workstation的最新版本是10.0.2.相信大家也都使用过,其中的简单的虚拟机的创建,删除等,都很简单,这里就不再详细说明了,下面我将简单介绍下vmware workstatio ...

  5. S-HR二开基础

    检测是否某个类部署上去了:http://10.3.0.115:6888/easportal/tools/getclassurl.jsp?className=com.kingdee.eas.hr.ser ...

  6. /proc目录介绍

    1. /proc目录 Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以 ...

  7. 当svn检出项目检出一半时停止,如何继续检出

    1.当svn检出项目时,发现中断,又不想重新检出可以在已检出的项目目录下右键 2.然后点击 之后直接update你的项目就可以了

  8. 【例题 4-5 uva 512】Spreadsheet Tracking

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个操作对与一个点来说变化是固定的. 因此可以不用对整个数组进行操作. 对于每个询问,遍历所有的操作.对输入的(x,y)进行相应的变 ...

  9. WebFont与页面font-icon图标研究

    当你打开(绝大部分)网站,页面上将会有许多形形色色的小图标(icon),适当的icon的可以达到一图胜千言的目的,使网页的表现效果更佳. 关于页面icon的制作,比较传统的方法是,让设计师去设计一个个 ...

  10. (33)Spring Boot 监控和管理生产环境【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] spring-boot-actuator模块提供了一个监控和管理生产环境的模块,可以使用http.jmx.ssh.telnet等拉管理和监控应用.审计 ...