uva11054
2016-06-11 11:44
281 查看
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21418
/* solution: 由于路费和距离相关,所以需要让路程越小越好,那么就可以让每人之和相邻的人交易。 不用考虑他们是要买还是要卖。假设a1要买5个,a2要卖2个,那么就让a1向a2买5个, 不用管a2有多少,不够可以打欠条,就变成-3了,这时a3就要向a3买3个。如此算下去, 只需要从第一个枚举到最后一个便可得到最小的答案。 note: 等价转换的思路. date: 2016/5/20 */ #include <iostream> #include <cstdio> #include <cstdlib> using namespace std; const int maxn = 100000 + 5; int main() { freopen("input.txt", "r", stdin); int n; while(~scanf("%d", &n) && n) { long long ans = 0, last = 0, a; for(int i = 0; i < n; i++) { cin >> a; ans += abs(last); last += a; } cout << ans << endl; } return 0; }
相关文章推荐
- SDUT 3332 下一较大值(一)
- 跨站语句
- s3c2440 LCD驱动编写
- 运维知识:RAID磁盘阵列配置和调优小结
- 1. Two Sum
- Spring基础—Bean容器
- OpenERP|ODOO高德地图应用
- 网络配置与挂载
- 1004 Financial Management
- left join中on和where的区别是什么
- Define your own binary operators
- Javascript 异步加载详解
- Hibernate中的一级缓存、二级缓存和懒加载
- 二叉排序树(三序输出)
- 查找代码错误.java
- 动态计算tablviewcell高度
- java创建邮件模板
- UI Components的理解
- 挂载分区
- 3.Android群英传读书笔记-控件架构与自定义控件