【贪心】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 ...
随机推荐
- HDU - 5327 Olympiad(一维前缀和)
Olympiad Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem ...
- SQL SERVER 常用公式
SQL SERVER 获取当前月的天数 SELECT -DAY(getdate()+-DAY(getdate())) SQL server 除法计算百分比[整数乘1.0否则结果为0或1] CONVER ...
- Tornado 安装及简单程序示例
1.安装步骤:tar xvzf tornado-3.2.tar.gz cd tornado-3.2 python setup.py build sudo python setup.py install ...
- [Leetcode Week14]Maximum Binary Tree
Maximum Binary Tree 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/maximum-binary-tree/description/ ...
- python基础===open()文件处理使用介绍
本文转自:Python open()文件处理使用介绍 1. open()语法open(file[, mode[, buffering[, encoding[, errors[, newline[, c ...
- Linux内核通知链机制的原理及实现【转】
转自:http://www.cnblogs.com/armlinux/archive/2011/11/11/2396781.html 一.概念: 大多数内核子系统都是相互独立的,因此某个子系统可能对其 ...
- Leetcode 之Largest Rectangle in Histogram(40)
又是一道构思巧妙的题,暴力求解复杂度太高,通过构造一个递增栈来解决:如果当前元素小于栈顶元素,则说明栈内已经构成一个 递增栈,则分别计算以每个元素为最低值的面积:反之,则入栈. int largest ...
- Leetcode 之Add Binary(29)
比较简单,细节:先将字符串翻转,注意进位. string addBinary(string a, string b) { string result; int len = a.size() > ...
- Django如何使文件在django自动执行
1. Django admin源码中 admin.py from django.contrib import admin # Register your models here. 在admin源码中 ...
- OpenFalcon-SuitAgent
http://www.cnblogs.com/qianlong2016/archive/2016/09/08/5853899.html