https://vjudge.net/problem/UVA-11054

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 i-th 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”, 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

Water...

 #include<iostream>
 #include<cstdio>
 #include<cmath>
 using namespace std;
 ;
 int num[maxn],n;
 int main()
 {
     &&n){
         ;i<=n;i++){
             scanf("%d",&num[i]);
         }
         ;
         ;i<n;i++){
             ans+=abs(num[i]);
             num[i+]+=num[i];
         }
         printf("%lld\n",ans);
     }
     ;
 }

UVa11054 Gergovia的酒交易 Wine trading in Gergovia-递推的更多相关文章

  1. UVA 11054 Wine trading in Gergovia 葡萄酒交易 贪心+模拟

    题意:一题街道上很多酒店,交易葡萄酒,正数为卖出葡萄酒,负数为需要葡萄酒,总需求量和总售出量是相等的,从一家店到另外一家店需要路费(路费=距离×运算量),假设每家店线性排列且相邻两店之间距离都是1,求 ...

  2. UVa11054 Gergovia的酒交易(数学归纳法)

    直线上有\(n\)个等距村庄,每个村庄要么买酒,要么卖酒.设第\(i\)个村庄对酒的需求为\(A_i\)(\(-1000 \leqslant A_i \leqslant 1000\)),其中\(A_i ...

  3. 8-5 Wine trading in Gergovia Gergovia的酒交易 uva11054

    等价转换思维题 题意: 直线上有n(2<=n<=100000)个等距的村庄  每个村庄要么买酒 要么卖酒  设第i个村庄对酒的需求量为ai  绝对值小于一千  其中ai大于0表示买酒   ...

  4. UVA - 11054 Wine trading in Gergovia (Gergovia 的酒交易)(贪心+模拟)

    题意:直线上有n(2<=n<=100000)个等距的村庄,每个村庄要么买酒,要么卖酒.设第i个村庄对酒的需求为ai(-1000<=ai<=1000),其中ai>0表示买酒 ...

  5. uva11054 - Wine trading in Gergovia(等价转换,贪心法)

    这个题看上去麻烦,实际上只要想清楚就很简单.关键是要有一种等价转换的思维方式.其实题意就是个一排数,最后通过相邻的互相移动加减使得所有数都变成零,移动过程中每次都耗费相应值,让耗费的值最小.虽然从实际 ...

  6. uva 11054 wine trading in gergovia (归纳【好吧这是我自己起的名字】)——yhx

    As you may know from the comic \Asterix and the Chieftain's Shield", Gergovia consists of one s ...

  7. UVA 11054 Wine trading in Gergovia(思维)

    题目链接: https://vjudge.net/problem/UVA-11054 /* 问题 输入村庄的个数n(2=<n<=100000)和n个村庄的数值,正代表买酒,负代表卖酒,k个 ...

  8. UVa 11054 Gergovia的酒交易

    https://vjudge.net/problem/UVA-11054 题意:直线上有n个等距的村庄,每个村庄要么买酒,要么卖酒.设第i个村庄对酒的需求为ai,ai>0表示买酒,ai<0 ...

  9. UVa 11054 Wine trading in Gergovia【贪心】

    题意:给出n个等距离的村庄,每个村庄要么买酒,要么卖酒,买酒和卖酒的总量相等, 把k个单位的酒从一个村庄运送到相邻的村庄,需要耗费k个单位劳动力,问怎样运送酒使得耗费的劳动力最少 买     卖    ...

随机推荐

  1. JS URL 使用base64加密与解密

    JS编码方式: <script type="text/javascript"> document.write(encodeURI("http://www.w3 ...

  2. JavaScript中Call()以及Apply()的应用

    apply()和call()的真正用武之地是能够扩充函数赖以运行的作用域 三点说明: 1.每个函数都包含两个非继承而来的方法:apply()和call(). 2.他们的用途相同,都是在特定的作用域中调 ...

  3. TFS Build Error: CSC : fatal error CS0042: Unexpected error creating debug information file 'xxxx.PDB'

    CSC : fatal error CS0042: Unexpected error creating debug information file 'xxxx.PDB' -- 'c:\Builds\ ...

  4. API爬虫--Twitter实战

    本篇将从实际例子出发,展示如何使用api爬取twitter的数据. 1. 创建APP 进入https://apps.twitter.com/,创建自己的app.只有有了app才可以访问twitter的 ...

  5. HDU 1671 Phone List (Trie·数组实现)

    链接:http://blog.csdn.net/acvay/article/details/47089657 题意  给你一组电话号码  判断其中是否有某个电话是另一个电话的前缀 字典树的基础应用   ...

  6. HTML document对象(2)

    五.相关元素操作: var a = document.getElementById("id");找到a: var b = a.nextSibling,找a的下一个同辈元素,注意包含 ...

  7. linux命令(5)文件操作:ls命令、显示文件总个数

    一:ls命令是最常用的linux命令了:下面是ls --help里面的用法 在提示符下输入ls --help ,屏幕会显示该命令的使用格式及参数信息: 先介绍一下ls命令的主要参数: -a 列出目录下 ...

  8. DPI和像素

    像素(Pixel) 对于计算机的屏幕设备而言,像素(Pixel)或者说px是一个最基本的单位,就是一个点.其它所有的单位,都和像素成一个固定的比例换算关系.所有的长度单位基于屏幕进行显示的时候,都统一 ...

  9. JS常用各种正则表达式

    1.非负整数         /^\d+$/ 2.正整数            /^[0-9]*[1-9][0-9]*$/ 3.非正整数        /^((-\d+)|(0+))$/ 4.负整数  ...

  10. tar.gz和rpm安装文件(转载)

    from:http://bbs.chinaunix.net/thread-2277750-1-1.html Linux软件的二进制分发是指事先已经编译好二进制形式的软件包的发布形式,其优点是安装使用容 ...