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. 用SQL Server验证用户名和密码

    用SQL Server验证用户名和密码,从页面输入的用户名和密码与数据库的用户名和密码进行匹配,正确则登入,错误则提醒. <form action="index.jsp" m ...

  2. (2)dotnet开源电商系统-brnshop VS nopCommerce(dotnet两套电商来PK--第二篇:代码从哪开始-BrnMall3.0Beta)

    看大牛们的源码,对于水平一般的人,还是略微有点难度的.我从我自身读码的亲身体验,写下杂散片语,希望能和大家一同进步,也为了日后记忆上的备查. 先看的是brnMall的源码结构,从哪看起呢? 首先推荐看 ...

  3. OAuth密码模式说明(resource owner password credentials)

    用户向客户端(third party application)提供用户名和密码. 客户端将用户名和密码发给认证服务器(Authorization server),向后者请求令牌(token). 认证服 ...

  4. 常用的 CSS 技巧

    1. 黑白图像 这段代码会让你的彩色照片显示为黑白照片,是不是很酷? img.desaturate { filter: grayscale(%); -webkit-filter: grayscale( ...

  5. Apex语言(三)原始数据类型

    1.原始数据类型(Primitive) 整数:Integer 双精度:Double 单精度:Decimal 长整型:Long 日期:Date 日期时间:Datetime 字符串:String ID:I ...

  6. Postgresql_最新版11.2源码编译安装

    pg官网:https://www.postgresql.org/ yum -y gcc gcc-c++ cmake ncurses-devel perl zlib* .去官网下载源码包. 下载地址:h ...

  7. PHP并发IO编程实践

    PHP相关扩展 Stream:PHP内核提供的socket封装 Sockets:对底层Socket API的封装 Libevent:对libevent库的封装 Event:基于Libevent更高级的 ...

  8. 从命令行配置 Windows 防火墙

    从命令行配置 Windows 防火墙 高级用户可以使用命令行来配置 Windows 防火墙.您可以使用 netsh 命令行工具来进行配置. 下表中的 netsh 命令可用于 Microsoft Win ...

  9. 洛谷——P2038 无线网络发射器选址

    https://www.luogu.org/problem/show?pid=2038 题目描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城 ...

  10. POJ 2007

    直接求凸包,输出即可. #include <iostream> #include <cstdio> #include <cstring> #include < ...