poj 2940
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 3187 | Accepted: 1454 |
Description
As you may know from the comic “Asterix and the Chieftain’s Shield”, Gergovia consists of one street, and every inhabitant of the city is a wine salesman. You wonder how this economy works? Simple enough: everyone buys wine from other inhabitants of the city. Every day each inhabitant decides how much wine he wants to buy or sell. Interestingly, demand and supply is always the same, so that each inhabitant gets what he wants.
There is one problem, however: Transporting wine from one house to another results in work. Since all wines are equally good, the inhabitants of Gergovia don’t care which persons they are doing trade with, they are only interested in selling or buying a specific amount of wine. They are clever enough to figure out a way of trading so that the overall amount of work needed for transports is minimized.
In this problem you are asked to reconstruct the trading during one day in Gergovia. For simplicity we will assume that the houses are built along a straight line with equal distance between adjacent houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work.
Input
The input consists of several test cases.
Each test case starts with the number of inhabitants n (2 ≤ n ≤ 100000). The following line contains n integers ai (−1000 ≤ ai ≤ 1000). If ai ≥ 0, it means that the inhabitant living in the ith house wants to buy ai bottles of wine, otherwise if ai < 0, he wants to sell −ai bottles of wine. You may assume that the numbers ai sum up to 0.
The last test case is followed by a line containing 0.
Output
For each test case print the minimum amount of work units needed so that every inhabitant has his demand fulfilled. You may assume that this number fits into a signed 64-bit integer (in C/C++ you can use the data type “long long” or “__int64”, in JAVA the data type “long”).
Sample Input
5
5 -4 1 -3 1
6
-1000 -1000 -1000 1000 1000 1000
0
Sample Output
9
9000 思路:最终每个位置上的数应该是0;
设now为当前剩余要搬的葡萄酒数目;ans为目前最小工作量。初始时now和ans为0;
顺序扫描每个房间i(0<=i&&i<n):
ans+=now;now+=a[i];
最后得出的ans为最小工作量。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define PI 3.141592653589792128462643383279502
#define N 100005
int main(){
//#ifdef CDZSC_June
freopen("in.txt","r",stdin);
//#endif
//std::ios::sync_with_stdio(false);
long long n,a[N],ans;int now;
while(scanf("%lld",&n),n){
ans=;now=;
for(int i=;i<n;i++){
scanf("%lld",a+i);
ans+=abs(now);
now+=a[i];
}
cout <<ans<<endl;
} return ;
}
poj 2940的更多相关文章
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- POJ 2752 Seek the Name, Seek the Fame [kmp]
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17898 Ac ...
- poj 2352 Stars 数星星 详解
题目: poj 2352 Stars 数星星 题意:已知n个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时 ...
随机推荐
- 【NOIP】普及组2011 表达式的值
[算法]动态规划+后缀表达式 [题解] 先把算式转为后缀表达式后进行DP 令f[s][0]表示使表达式答案为0的方案数 f[s][1]表示使表达式答案为1的方案数 (加法) f[a+b][1]=f[a ...
- UIImageView与UIScrollView的关系图
UIImageView与UIScrollView的关系图 https://www.evernote.com/shard/s227/sh/0af9f23c-08e6-4be6 ...
- BTA 常问的 Java基础40道常见面试题及详细答案(山东数漫江湖))
八种基本数据类型的大小,以及他们的封装类 引用数据类型 Switch能否用string做参数 equals与==的区别 自动装箱,常量池 Object有哪些公用方法 Java的四种引用,强弱软虚,用到 ...
- redis基础之开机自启动和监听(二)
redis安装好后,每次手动启动很不方便,配置开机自启动. 方法一:设置启动命令到/etc/rc.d/rc.local rc.local文件是系统全局脚本文件,会在其他开机进程脚本文件执行完毕后执行该 ...
- FastDFS介绍和配置过程
由于网站使用nfs共享方式保存用户上传的图片,附件等资料,然后通过apache下载的方式供用户访问,在网站架构初期,使用这种简单的方式实现了静态资源的读写分离,但随着网站数据量的增加,图片服务器渐渐成 ...
- [Leetcode Week17]Copy List with Random Pointer
Copy List with Random Pointer 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/copy-list-with-random- ...
- 数据库===轻量级mysql数据库管理工具
已经上传至: https://download.csdn.net/download/bo_mask/10276952
- [New learn]AutoLayout调查基于code
代码https://github.com/xufeng79x/TestAutolayout-code2 0.插在前面 必须关闭view的自动缩放掩码,自动缩放掩码是autolayout出现之前系统管理 ...
- [How to]集成SQLite3
1.简介 本文将介绍IOS的开发过程中如何集成Sqlite的方法,目前Sqlite的版本为3,所以我们称之为Sqlite3. 在本文中我将介绍Sqlite3的开发配置,本地Sqlite3数据库的建立通 ...
- 查询 IDE 的版本 方便安装第三方的时候选择
TMD 很多第三方软件 的版本 定义 与 delphi是不一致的. 1.有的是以这个为准.