【贪心】Gym - 101201I - Postman
题意:一个邮递员从数轴上原点出发,每次最多带K封信,往n个地方送信,每个地方有一定的需求的信件数,问你最少要跑的距离的总和是多少?一趟可以给多个地方去送。
显然优先往远的地方送比较优越,近的地方可以顺路给他带过去。
原点两侧是独立的,可以分别处理。
#include<cstdio>
#include<algorithm>
using namespace std;
int Abs(int x){
return x<0 ? (-x) : x;
}
typedef pair<int,int> Point;
typedef long long ll;
ll ans;
int n,K;
Point p[1005];
int main(){
// freopen("i.in","r",stdin);
scanf("%d%d",&n,&K);
for(int i=1;i<=n;++i){
scanf("%d%d",&p[i].first,&p[i].second);
}
sort(p+1,p+n+1);
int now=0;
for(int i=1;p[i].first<0;++i){
if(p[i].second<=now){
now-=p[i].second;
}
else{
p[i].second-=now;
ans+=(ll)(p[i].second/K+(p[i].second%K!=0))*(ll)Abs(p[i].first)*2ll;
if(p[i].second%K!=0){
now=K-p[i].second%K;
}
else{
now=0;
}
}
}
now=0;
for(int i=n;p[i].first>0;--i){
if(p[i].second<=now){
now-=p[i].second;
}
else{
p[i].second-=now;
ans+=(ll)(p[i].second/K+(p[i].second%K!=0))*(ll)Abs(p[i].first)*2ll;
if(p[i].second%K!=0){
now=K-p[i].second%K;
}
else{
now=0;
}
}
}
printf("%I64d\n",ans);
return 0;
}
【贪心】Gym - 101201I - Postman的更多相关文章
- Gym 101201I Postman (贪心)
题意:有个邮递员,要送信,每次最多带 m 封信,有 n 个地方要去送,每个地方有x 封要送,每次都到信全送完了,再回去,对于每个地方,可以送多次直到送够 x 封为止. 析:一个很简单的贪心,就是先送最 ...
- 贪心 Gym 100502E Opening Ceremony
题目传送门 /* 题意:有一堆砖块,每一次操作可以选择消去任意一行,也可以选择消去任意一列.求要消去所有的砖块需要最小的操作数 贪心:首先清楚的是消去最高列的最佳,消去第一行最佳,行列的顺序只对中间过 ...
- Codeforces Gym 100803C Shopping 贪心
Shopping 题目连接: http://codeforces.com/gym/100803/attachments Description Your friend will enjoy shopp ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- codeforces Gym 100187F F - Doomsday 区间覆盖贪心
F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F ...
- hdu-5695 Gym Class(贪心+拓扑排序)
题目链接: Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- Codeforces Gym 100269E Energy Tycoon 贪心
题目链接:http://codeforces.com/gym/100269/attachments 题意: 有长度为n个格子,你有两种操作,1是放一个长度为1的东西上去,2是放一个长度为2的东西上去 ...
- Gym 101194D / UVALive 7900 - Ice Cream Tower - [二分+贪心][2016 EC-Final Problem D]
题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...
- Gym 101775B - Scapegoat - [贪心+优先队列]
题目链接:http://codeforces.com/gym/101775/problem/B Aori is very careless so she is always making troubl ...
随机推荐
- Morley's Theorem (计算几何基础+向量点积、叉积、旋转、夹角等+两直线的交点)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- select下拉箭头改变,兼容ie8/9
各个浏览器下select默认的下拉箭头差别较大,通常会清除默认样式,重新设计 <html> <head> <meta charset="utf-8"& ...
- 集合框架源码学习之HashMap(JDK1.8)
目录: 0-1. 简介 0-2. 内部结构分析 0-2-1. JDK18之前 0-2-2. JDK18之后 0-3. LinkedList源码分析 0-3-1. 构造方法 0-3-2. put方法 0 ...
- Tomcat8配置默认项目
<!-- 配置默认访问项目 --> <Host name="localhost" appBase="webapps" unpackWARs=& ...
- linux中使用mysql数据库
在安装完数据库后,如果没有设置root的mysql密码,在命令行输入mysql即可进入数据库 show databases;(有分号):查看当前存在的数据库 create database 名字:创建 ...
- 64_a2
arquillian-core-parent-1.1.11-6.fc26.noarch.rpm 10-Feb-2017 13:22 12918 arquillian-core-spi-1.1.11-6 ...
- OPENSOLARIS source
http://blog.csdn.net/nemo2011/article/details/8543220 http://fxr.watson.org/fxr/source/?v=OPENSOLARI ...
- Keepalived 安装与简单配置
Keepalived 安装与简单配置 http://sivxy.lofter.com/post/1d21ebb9_7e15000
- MyEclipse部署项目报"Add Deployment". Invalid Subscription Level - Discontinuing this MyEclipse
"Add Deployment". Invalid Subscription Level - Discontinuing this MyEclipse 猜测应该是MyEclipse ...
- FineReport——函数
1.VALUE(tableData,col,row)返回tableData中列号为col的值, 行号为row.备注:先从报表数据集中查找,然后再从服务器数据集中查找,返回的是tableData的行数. ...