题意:一个邮递员从数轴上原点出发,每次最多带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的更多相关文章

  1. Gym 101201I Postman (贪心)

    题意:有个邮递员,要送信,每次最多带 m 封信,有 n 个地方要去送,每个地方有x 封要送,每次都到信全送完了,再回去,对于每个地方,可以送多次直到送够 x 封为止. 析:一个很简单的贪心,就是先送最 ...

  2. 贪心 Gym 100502E Opening Ceremony

    题目传送门 /* 题意:有一堆砖块,每一次操作可以选择消去任意一行,也可以选择消去任意一列.求要消去所有的砖块需要最小的操作数 贪心:首先清楚的是消去最高列的最佳,消去第一行最佳,行列的顺序只对中间过 ...

  3. Codeforces Gym 100803C Shopping 贪心

    Shopping 题目连接: http://codeforces.com/gym/100803/attachments Description Your friend will enjoy shopp ...

  4. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  5. codeforces Gym 100187F F - Doomsday 区间覆盖贪心

    F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F ...

  6. hdu-5695 Gym Class(贪心+拓扑排序)

    题目链接: Gym Class Time Limit: 6000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) ...

  7. Codeforces Gym 100269E Energy Tycoon 贪心

    题目链接:http://codeforces.com/gym/100269/attachments 题意: 有长度为n个格子,你有两种操作,1是放一个长度为1的东西上去,2是放一个长度为2的东西上去 ...

  8. 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 ...

  9. Gym 101775B - Scapegoat - [贪心+优先队列]

    题目链接:http://codeforces.com/gym/101775/problem/B Aori is very careless so she is always making troubl ...

随机推荐

  1. git 配置多用户

    .ssh 下的 config.txt 内容 # 配置github.com Host github.com HostName github.com IdentityFile ~/.ssh/id_rsa_ ...

  2. DIV+CSS左右列高度自适应问题

    其实解决DIV+CSS左右两列高度自适应的方法就是要注意两点:一是在最外层加上overflow:hidden,然后在左边列加上margin-bottom:-9999px;padding-bottom: ...

  3. GDB实战

    程序中除了一目了然的Bug之外都需要一定的调试手段来分析到底错在哪.到目前为止我们的调试手段只有一种:根据程序执行时的出错现象假设错误原因,然后在代码中适当的位置插入 printf ,执行程序并分析打 ...

  4. 自定义shell开头PS1

    vim /etc/profile export PS1="flag:\W \u\$" \h是主机名,并不全,域 \W是当前所在目录名 \u 是当前shell用户名

  5. 2.ubuntu的使用

    1. CTRL+ALT+T 可以将命令模式打开 2. 有可能没办法进行yum ,它会告诉你操作的方法 3. 有些操作需要获得root的权限才可以,我们得进入root状态 --> sudo pas ...

  6. Linux 日志系统及分析

    简介 在Centos 7.x / RHEL 7.x 的版本,系统日志是由一个名为 rsyslog的服务管理的,默认的日志守护进程为 rsyslog , rsyslog 是 syslog 的升级版本,默 ...

  7. Context-Aware Network Embedding for Relation Modeling

    Context-Aware Network Embedding for Relation Modeling 论文:http://www.aclweb.org/anthology/P17-1158 创新 ...

  8. hibernate连接Oracle rac

    连接方式与普通的数据库不一样.connection.url 中使用了LOAD-BALANCE = yes 要不然会报错 <hibernate-configuration> <sess ...

  9. java的IO流之字符流

    # 原创,转载请留言联系 输出流 FileWriter类 常见的构造方法: FileWriter(String fileName)     根据给定的文件名构造一个 FileWriter 对象.Fil ...

  10. JWT认证不通过导致不能访问视图的解决方案

    在做商城项目的购物车模块时,发现了一个问题. 需求:当用户登录时,添加商品到购物车的数据保存在redis.当用户未登录时,添加商品到购物车的数据保存在cookies.两个功能都写在一个视图里面.以JW ...