UVALive 7960|Gym 101201I|Postman|贪心
来源:互联网 发布:知之深爱之初 编辑:程序博客网 时间:2024/06/14 05:22
Description
A postman delivers letters to his neighbors in a one-dimensional world.
The post office, which contains all of the letters to begin with, is located at
The postman must start at the post office, pick up some number of letters less than or equal to his carrying capacity, and then travel to some of the houses dropping off letters. He must then return to the post office, repeating this process until all letters are delivered. At the end he must return to the post office.
The postman can travel one unit of distance in one unit of time.
What is the minimum amount of time it will take the postman to start at the post office, deliver all the letters, and return to the post office?
Translation
一个邮递员给一个一维世界里的邻居派送邮件。
邮局在
Input
The first line of input contains two space-separated integers
Each of the next n lines contains two space-separated integers
Output
Print, on a single line, the minimum amount of time it will take to complete the mail delivery route
Sample 1
Sample Input
4 10-7 5-2 35 79 5
Sample Output
42
Sample 2
Sample Input
7 19400000 100000009500000 100000009600000 100000009700000 100000009800000 100000009900000 1000000010000000 10000000
Sample Output
1358000000000000
Solution
贪心:显然是先往远的送,因为送多了回来的时候就可以顺路给路过的地方。如果一边送完了还有多的也重新送,因为反正肯定要路过邮局才会到另一边运,顺便重新带k封邮件是没有问题的。
Code
#include <cstdio>#include <algorithm>using namespace std;#define st first#define nd secondtypedef long long ll;pair<int, int> p[1005];int deliver(int &d, int i, int k) { if (p[i].nd <= d) d -= p[i].nd; else { p[i].nd -= d; ans += (1ll * p[i].nd / k + (p[i].nd % k != 0)) * (2ll * llabs(p[i].first)); if (p[i].nd % k != 0) d = k - p[i].nd % k; else d = 0; }}int main() { ll d, ans = 0; int n, k, i; scanf("%d%d", &n, &k); for (i = 1; i <= n; ++ i) scanf("%d%d", &p[i].st, &p[i].nd); sort(p + 1, p + n + 1); for (d = 0, i = 1; p[i].st < 0; ++ i) deliver(d, i, k); for (d = 0, i = n; p[i].st > 0; -- i) deliver(d, i, k); printf("%I64d\n", ans); return 0;}
- UVALive 7960|Gym 101201I|Postman|贪心
- UVALive 7959|Gym 101201H|Paint|DP|贪心
- GYM 100247 I. Meteor Flow(贪心)
- GYM 100030 I.Prohibition(贪心)
- GYM 100090 I.Retakes(贪心+优先队列)
- Gym 100712E 贪心
- UVAlive 2911 Maximum(贪心)
- uvalive 2322(贪心)
- uvalive 2326(贪心)
- uvalive 2519(贪心)
- uvalive 3971(贪心)
- uvalive 3635(贪心)
- uvalive 2911(贪心)
- uvalive 2949(二分+贪心)
- uvalive 4725(贪心 + 二分)
- uvalive 4850(贪心)
- uvalive 3266(贪心)
- uvalive 4094(贪心)
- 检测评价函数 intersection-over-union ( IOU )
- 父类对象到底能不能强制转换为子类对象
- 磁盘共享之NFS
- 【Scikit-Learn 中文文档】预测目标 (y) 的转换
- 第十一周项目一——图基本算法库
- UVALive 7960|Gym 101201I|Postman|贪心
- 【Scikit-Learn 中文文档】数据集加载工具
- 通过@ConfigurationProperties使用外部配置填充Bean属性的几种方法
- 单源点最短路径(弗洛伊德)
- zip伪加密
- 计算日期差
- NYOJ67三角形面积
- Debian Package Manager
- 关于SimpleDateFormat