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. 如何把SQLServer数据库从高版本降级到低版本?

    http://blog.csdn.net/dba_huangzj/article/details/7952403 由于目前还广泛使用着SQLServer2000,很多公司又想使用新的SQLServer ...

  2. [工具开发] Perl 爬虫脚本--从美国国家漏洞数据库抓取实时信息

    一.简介 美国国家漏洞数据库收集了操作系统,应用软件的大量漏洞信息,当有新的漏洞出现时,它也会及时发布出来. 由于信息量巨大,用户每次都需要到它的网站进行搜索,比较麻烦.如果能有个工具,每天自动分析它 ...

  3. 常​用​的​邮​箱​服​务​器​(​S​M​T​P​、​P​O​P​3​)​地​址​、​端​口

    常用的邮箱服务器(SMTP.POP3)地址.端口 gmail(google.com)  POP3服务器地址:pop.gmail.com(SSL启用 端口:995)  SMTP服务器地址:smtp.gm ...

  4. c++101rule

    组织策略0,不拘于小结缩进, 行的长度,命名,注释,空格,制表,1-4,高警告级别干净利落地进行编译,使用构建系统,使用版本控制,代码审查风格5,一个实体应该只有一个紧凑的职责. (依赖性管理,继承, ...

  5. SparkSql官方文档中文翻译(java版本)

    1 概述(Overview) 2 DataFrames 2.1 入口:SQLContext(Starting Point: SQLContext) 2.2 创建DataFrames(Creating ...

  6. spark RDD transformation与action函数整理

    1.创建RDD val lines = sc.parallelize(List("pandas","i like pandas")) 2.加载本地文件到RDD ...

  7. 502 bad gateway 可能的错误原因

    1.PHP程序的执行时间超过了Nginx的等待时间,可以适当增加nginx.conf配置文件中FastCGI的timeout时间 #http代码段中增加 fastcgi_connect_timeout ...

  8. 转-Android微信支付

    http://blog.fangjie.info/android微信支付/ Android微信支付 2014-08-09 一.使用微信官方的提供的demo里的appid等 1.微信接口上手指南:(从“ ...

  9. Mac下finder中无法关闭的小条

    就是这个恶心的小东西,根本无法关闭.其实就是在finder中无意中输入了中文导致的(不是按Alt+F , 就是打开finder然后输入中文) 可以通过继续输入中文然后按esc退出.也可以用下面的设置尽 ...

  10. Visual Stadio 2015创建WebApplication应用和运行赏析

    专题图: 1,创建一个WebApplication应用 2,项目结构和布局  3,运行项目 作者:ylbtech出处:http://ylbtech.cnblogs.com/本文版权归作者和博客园共有, ...