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. AFNetworking源码解析-https证书相关

    本篇说说安全相关的AFSecurityPolicy模块,AFSecurityPolicy用于验证HTTPS请求的证书,先来看看HTTPS的原理和证书相关的几个问题. HTTPS HTTPS连接建立过程 ...

  2. 去掉二级页面 tabs 菜单, 修改返回按钮

    imports: [ /*引入的模块 依赖的模块*/ BrowserModule, ComponentsModule, IonicModule.forRoot(MyApp,{ tabsHideOnSu ...

  3. Windows上架设自己的ssh代理 — copSSH

    最近网上对ssh代理讨论的比较多, 主要是为了穿墙. 其实在自己的电脑上也可以架设ssh代理, 当然国内自己架的不具有FQ功能, 如果你有国外朋友或是远程win服务器…则可以用此方法架设私有代理. 其 ...

  4. 【转】【Oracle 集群】ORACLE DATABASE 11G RAC 知识图文详细教程之RAC 特殊问题和实战经验(五)

    原文地址:http://www.cnblogs.com/baiboy/p/orc5.html   阅读目录 目录 共享存储 时间一致性 互联网络(或者私有网络.心跳线) 固件.驱动.升级包的一致性 共 ...

  5. json-lib与Jackson的区别和用法分析

    一.Jackson概述 1.jackson包和版本 Jackson fasterxml和codehaus的区别: 他们是Jackson的两大分支.也是两个版本的不同包名.Jackson从2.0开始改用 ...

  6. python 生成HTmL报告页面 V1.2

    上代码 # -*- coding=utf-8 -*- import time,os #数据部分 func_dict={"funcname":"模块1",} fu ...

  7. php第九节课

    面向对象 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  8. Django1.11配合uni-app发起微信支付!

    Django1.11配合uni-app发起微信支付! 经过三天的断断续续的奋战,我终于是干动了微信支付.为了以后不忘记,现在来一篇教程,来来来,开干!!! 一.准备阶段 1.准备阶段我们需要去微信官网 ...

  9. SQL Server 通过备份文件初始化复制 – 听风吹雨

    一.背景 MySQL在对有历史数据的数据库进行搭建复制(Master/Slave)的时候,可以通过在Master服务器备份历史数据,利用这个备份文件 在Slave进行还原:这样做的好处是可以更加快速的 ...

  10. Internal Temporary Tables

    8.4.4 How MySQL Uses Internal Temporary Tables 这是MySQL手册中的一节,尝试补充了一些解释.用的版本是MySQL5.6.15社区版 In some c ...