Gym 101201I Postman (贪心)
题意:有个邮递员,要送信,每次最多带 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 (贪心)的更多相关文章
- 【贪心】Gym - 101201I - Postman
题意:一个邮递员从数轴上原点出发,每次最多带K封信,往n个地方送信,每个地方有一定的需求的信件数,问你最少要跑的距离的总和是多少?一趟可以给多个地方去送. 显然优先往远的地方送比较优越,近的地方可以顺 ...
- Codeforces Gym 100803C Shopping 贪心
Shopping 题目连接: http://codeforces.com/gym/100803/attachments Description Your friend will enjoy shopp ...
- Gym 101775B - Scapegoat - [贪心+优先队列]
题目链接:http://codeforces.com/gym/101775/problem/B Aori is very careless so she is always making troubl ...
- Gym - 100338E Numbers 贪心
Gym - 100338E 题意:给你n,k问在1-n中能整出k的字典序最小的数.范围1018 思路:比较简单的贪心了,枚举10的幂m,然后加上k-m%k, 更新答案就可以了,数据一定要用unsign ...
- Gym - 100989H (贪心)
After the data structures exam, students lined up in the cafeteria to have a drink and chat about ho ...
- HDU 5695 ——Gym Class——————【贪心思想,拓扑排序】
Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- Gym - 100801G: Graph (贪心+set+拓扑)(好题)
题意:给定一个N点M边的有向图,叫你加最多K条边,使得最小拓扑序最大. 思路:不是那么简单的题. 参照了别人的代码, 最后想通了. 贪心原则: 用两个单调队列维护, 第一个序列S1单增, 表示当前入 ...
- GYM 101933I(贪心、大整数)
我读题有问题呀,题目中到底哪里保证数据一定至少是2倍关系了Orz--然后既然题意就是保证了那贪心一下即可,因为如果当前这个大的不选,那剩下一堆逐渐小于上一代的1/2的,凑起来都不如这个大的,更别说答案 ...
- Codeforces Gym 100203E bits-Equalizer 贪心
原题链接:http://codeforces.com/gym/100203/attachments/download/1702/statements.pdf 题解 考虑到交换可以减少一次操作,那么可以 ...
随机推荐
- Cocos2d-x调用Java 代码
Java代码: package com.dishu; import com.dishu.org.R; import android.app.Activity; import android.app.A ...
- 让APK 成功在 Windows 运行并可以设置本地文件
让APK 成功在 Windows 运行并可以设置本地文件 安装 ARC Welder. 启动 ARC Welder 后选反apk 文件,下载 zip. 将 zip 解压修改 manifest.json ...
- vCenter 6.5安装
http://guanjianfeng.com/archives/1160269 最近,VMware发布了vSphere 6.5版本,之前的最新版本为6.0.新版本已经开始试行使用HTML5来管理vS ...
- 远程连接Linux虚拟机上的mysql失败的解决方法
今天在虚拟机Ubuntu上折腾了一晚上mysql,然后试着用java连接,搞了很久都没成功,但是同学配好的Debian上却连接成功了,也就是说我的配置有问题. 折腾了很久,最后还是通过理解异常信息来大 ...
- 【转】Java常量池详解
今天My partner问我一个让他头疼的Java question,求输出结果: /** * * @author DreamSea 2011-11-19 */ public class Intege ...
- 关于Android App开发知识体系的一个小总结
前言 本文从热更新.异步并发.性能优化.网络请求等多个方面对Android App开发的知识体系进行了一个分类总结.欢迎大家沟通交流. 热更新 [原]热更新开源项目Tinker源码解析之Dex热更新 ...
- C#遍历XmlDocument对象所有节点名称、类型、属性(Attribute)
C#遍历XmlDocument对象所有节点名称.类型.属性(Attribute) 源码下载 代码 static void Main(string[] args) { System.Xml.XmlDoc ...
- 记录在Python2.7 x64 bit 下 PyQt5.8的编译过程
由于工作需要使用python下面的Qt库.PyQt现在只提供针对Python3.X系列的PyQt,所有需要自己手动编译.防止忘记,特意写下随笔记录备忘. 工 作 环境:Python版本:Python ...
- Python单例模式实现方法
一 简介 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例类的特殊类.通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源 ...
- Beetlsql自定义生成entity,mapper,md代码
三个模板文件 mapper.btl package ${package}; import org.beetl.sql.core.annotatoin.*; import org.beetl.sql.c ...