题意:有个邮递员,要送信,每次最多带 m 封信,有 n 个地方要去送,每个地方有x 封要送,每次都到信全送完了,再回去,对于每个地方,可以送多次直到送够 x 封为止。

析:一个很简单的贪心,就是先送最远的,如果送完最远的还剩下,那么就送次远的,如果不够了,那么就加上回来的距离,重新带够 m 封信,对于左半轴和右半轴都是独立的,两次计算就好。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
//#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define FOR(x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
#define pii pair<int, int>
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 10;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} struct Node{
int pos;
LL val;
Node(int p = 0, LL v = 0) : pos(p), val(v) { }
bool operator < (const Node rhs) const{
return pos > rhs.pos;
}
}; Node node1[maxn], node2[maxn]; int main(){
scanf("%d %d", &n, &m);
int cnt1 = 0, cnt2 = 0;
for(int i = 0; i < n; ++i){
int a, b; scanf("%d %d", &a, &b);
if (a < 0) node1[cnt1++] = Node(-a, b);
else node2[cnt2++] = Node(a, b);
}
sort(node1, node1 + cnt1);
sort(node2, node2 + cnt2);
LL ans = 0;
LL pre_left = 0;
for(int i = 0; i < cnt1; ++i){
if (pre_left >= node1[i].val){
pre_left -= node1[i].val;
node1[i].val = 0;
continue;
}
node1[i].val -= pre_left;
LL num = (node1[i].val - 1) / m + 1;
ans += num * node1[i].pos * 2;
pre_left = num * m - node1[i].val;
}
pre_left = 0;
for (int i = 0; i < cnt2; ++i){
if (pre_left >= node2[i].val){
pre_left -= node2[i].val;
node2[i].val = 0;
continue;
}
node2[i].val -= pre_left;
LL num = (node2[i].val - 1) / m + 1;
ans += num * node2[i].pos * 2;
pre_left = num * m - node2[i].val;
}
printf("%I64d\n", ans);
return 0;
}

  

Gym 101201I Postman (贪心)的更多相关文章

  1. 【贪心】Gym - 101201I - Postman

    题意:一个邮递员从数轴上原点出发,每次最多带K封信,往n个地方送信,每个地方有一定的需求的信件数,问你最少要跑的距离的总和是多少?一趟可以给多个地方去送. 显然优先往远的地方送比较优越,近的地方可以顺 ...

  2. Codeforces Gym 100803C Shopping 贪心

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

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

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

  4. Gym - 100338E Numbers 贪心

    Gym - 100338E 题意:给你n,k问在1-n中能整出k的字典序最小的数.范围1018 思路:比较简单的贪心了,枚举10的幂m,然后加上k-m%k, 更新答案就可以了,数据一定要用unsign ...

  5. Gym - 100989H (贪心)

    After the data structures exam, students lined up in the cafeteria to have a drink and chat about ho ...

  6. HDU 5695 ——Gym Class——————【贪心思想,拓扑排序】

    Gym Class Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  7. Gym - 100801G: Graph (贪心+set+拓扑)(好题)

    题意:给定一个N点M边的有向图,叫你加最多K条边,使得最小拓扑序最大. 思路:不是那么简单的题.  参照了别人的代码, 最后想通了. 贪心原则: 用两个单调队列维护, 第一个序列S1单增, 表示当前入 ...

  8. GYM 101933I(贪心、大整数)

    我读题有问题呀,题目中到底哪里保证数据一定至少是2倍关系了Orz--然后既然题意就是保证了那贪心一下即可,因为如果当前这个大的不选,那剩下一堆逐渐小于上一代的1/2的,凑起来都不如这个大的,更别说答案 ...

  9. Codeforces Gym 100203E bits-Equalizer 贪心

    原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑到交换可以减少一次操作,那么可以 ...

随机推荐

  1. 获取bing带swim的网址列表

    需求背景: 应老婆要求,搜集带有swim关键字的网站.实现过程: 使用requests模块通过bing接口搜索swim关键,将返回内容按需求进行处理,得到网站列表. 注:代码比较拙,老司机就不要弄废时 ...

  2. ffmpeg V4L2_BUF_FLAG_ERROR的解决方法

    利用ffmpeg进行视频采集时经常出现“V4L2_BUF_FLAG_ERROR”的错误,并不再进行下帧的采集.通过借鉴下面的方法,对ffmpeg3.0.7版本进行补丁,能解决此类问题. 当某帧出错后, ...

  3. centos6.6 myphpadmin

    基本环境为:Centos6.6+Apache2.2.15+php5.3.3+Mysql5.1.73 开始下载了网站上最新版本myPhpAdmin4.3.8安装后打开浏览器为空白页,后百度后都讲是与PH ...

  4. debian8下pgsql的主备同步热切手动脚本

    9以后,通过流复制直接做 主:192.168.1.111 从:192.168.1.222 需要在postgres帐号下先配置ssh互信,双机都配置 sh-keygen -t rsa ssh-copy- ...

  5. Java 获取字符串指定下标位置的值 charAt()

    Java手册 charAt public char charAt(int index) 返回指定索引处的 char 值.索引范围为从 0 到 length() - 1.序列的第一个 char 值位于索 ...

  6. Django学习---Form组件认证

    Form组件认证 能够帮助我们做用户认证. 以前写我们自己写用户认证的时候,我们自己写HTML的form表单,点击提交,数据就被发送到后台,后台进行验证.在验证过程中我们就需要自己去写正则表达式去匹配 ...

  7. Supervisor安装与配置

    Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统 ...

  8. Cascade属性的取值

    Cascade属性的取值有:1.none:忽略其他关联的对象,默认值.2.save-update:当session通过save(),update(),saveOrUpdate()方法来保存或更新对象时 ...

  9. sqlserver数据库导出成insert语句

    点击数据库名称右键=========>任务========>生成脚本 一.表结构导出成sql语句 二.数据导出成sql语句

  10. jQuery load() 判断 iframe 是否加载完毕

    判断 iframe 是否加载完毕  方法.jQuery load() var frm = document.getElementById('myiframe'); $(frm).load(functi ...