As you may know from the comic “Asterix andthe Chieftain’s Shield”, Gergovia consists of one street, and every inhabitantof 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 eachinhabitant decides how much wine he wants to buy or sell. Interestingly, demandand supply is always the same, so that each inhabitant gets what he wants.There is one problem, however: Transporting wine from one house to anotherresults in work. Since all wines are equally good, the inhabitants of Gergoviadon’t care which persons they are doing trade with, they are only interested inselling or buying a specific amount of wine. They are clever enough to figureout a way of trading so that the overall amount of work needed for transportsis minimized. In this problem you are asked to reconstruct the trading duringone day in Gergovia. For simplicity we will assume that the houses are builtalong a straight line with equal distance between adjacent houses. Transportingone bottle of wine from one house to an adjacent house results in one unit ofwork.

Input

The input consists of several test cases.Each test case starts with the number of inhabitants n (2 ≤ n ≤ 100000). Thefollowing line contains n integers ai (−1000 ≤ ai ≤ 1000). If ai ≥ 0, it means that theinhabitant living in the i-th house wants to buy ai bottles of wine, otherwiseif ai < 0, he wants to sell −ai bottles of wine. You mayassume that the numbers ai sum up to 0. The last test case is followed by aline containing ‘0’.

Output For each test case print the minimumamount 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++ youcan 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

【题意】

给定长度为n且总和一定为0的序列,大于0代表出售,小于0代表买入,要求把序列的每一项都变为0,将某个数值x移动到相邻的位置后(比如从i移到i+1,a[i]-x,a[i+1]+x)花费的代价是x,求得一个最小代价来解决整个问题。

【思路】

从左向右模拟计算即可,写完了看看别人的代码真的好简单,有时候自己还是把简单的问题想的复杂了。下面是我自己的代码。

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;const int maxn = 100050;int n;
int a[maxn];int main() {while (scanf("%d", &n) == 1 && n) {for (int i = 0; i < n; ++i) scanf("%d", &a[i]);ll ans = 0, num;int i = 0, flag;while (1) {while (0 == a[i]) { ++i; }if (i >= n) break;flag = a[i] > 0 ? 1 : -1;num = a[i];while (num*(ll)flag > 0LL) {ans += abs(num);num += a[++i];}if (i >= n) break;else a[i] = num;}printf("%lld\n", ans);}return 0;
}

转载于:https://www.cnblogs.com/wafish/p/10465465.html

Uva 11054 - Wine trading in Gergovia(模拟)相关推荐

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

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

  2. uva 11054——Wine trading in Gergovia

    题意:有n个村庄,每个村庄要么买酒(+),要么卖酒(-),要求供需平衡,求最小代价(代价k为把k个单位的酒运到相邻的村庄). 思路:贪心.可以把第1个村庄的所有酒都从第2个村庄运来,那么12可以合二为 ...

  3. UVA 11054 Wine trading in Gergovia

    题意: 一条街上住着连续的n户人家,没相邻的两户人相隔一个单位.街上的每户人都需要买一定数量的葡萄酒或者卖掉葡萄酒,保证所有人家买进的总量与卖出的数量一致.每户可以选择与其他任何家交易.但是因为相隔路 ...

  4. 11054 - Wine trading in Gergovia

    Wine trading in Gergovia As you may know from the comic "Asterix and the Chieftain's Shield&quo ...

  5. POJ2940 HDU1489 UVA11054 Wine Trading in Gergovia【Ad Hoc】

    Wine Trading in Gergovia Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3541   Accepte ...

  6. Wine Trading in Gergovia (贪心)

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

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

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

  8. Uva 11292 The Dragon of Loowater 模拟题

    题目大意:你的王国里有一条有n个头的恶龙,你希望雇一些骑士来把它杀死(即砍掉所有的头).村里有m个骑士可以雇佣,一个能力值为x的骑士可以砍掉恶龙一个直径不超过x的头,且需要支付x个金币.如何雇佣骑士才 ...

  9. uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列

    题目很简单,给一个队列以及文件的位置,然后一个一个检查,如果第一个是优先级最高的就打印,否则放到队列后面,求所要打印的文件打印需要花费多长时间. 这里我用数组模拟队列实现,考虑到最糟糕的情况,必须把数 ...

最新文章

  1. linux 下 php 安装 Gearman
  2. p1209 Barn Repair
  3. VTK:Math之NormalizeVector
  4. 时间排序_你懂使用C ++ STL在线性时间内查找未排序数组的中位数吗
  5. Linux环境下Tomcat部署Solr4.x
  6. c# String 前面不足位数补零的方法 (转贴)
  7. PHP将图片转换成base64编码,hash函数
  8. 《21世纪英汉汉英双向词典》《朗文当代英语辞典第五版》《牛津高阶英汉双解词典第7版》...
  9. 【ArcGIS教程】土地利用转移矩阵及土地利用数据获取
  10. 含泪整理最优质立秋海报设计素材,你想要的这里都有
  11. win10平板模式_win10电脑投屏到手机
  12. 使用EFR32作为Zigbee/Thread的sniffer的用法
  13. 【205期推荐】HIS实施感悟-提升自己综合能力是正道
  14. 中国科学院计算机研究所李华,李华-中国科学院大学-UCAS
  15. 【§解码器Win7codecs设置方法安装与使用教程§】
  16. 如何拿到阿里、华为、美团等6个大厂的顶级offer?
  17. PHP面试技巧——如何克服面试中紧张的情绪?
  18. 有关nginx设置默认目录的坑
  19. JAVA面向接口的编程思想与具体实现
  20. Spring Boot 1.0 升级到 2.0 的时候遇到一些问题

热门文章

  1. c# combobox集合数据不显示_C#实战036:各种泛型的定义和使用详解
  2. 风变Python6---布尔值,break,continue,pass,else等语句的学习
  3. 目标检测(二十一)--FCN
  4. axios上传图片到php报500,vue项目中使用axios上传图片等文件
  5. 0基础python入门书籍 excel_零基础学Python3(23):Excel 基础操作(上)
  6. 高中信息技术——GoldWave音频处理刷题点整理
  7. 软件工程大学大三课表_专业选修课 | 面向大二、大三同学的专业选修课全面介绍来啦!...
  8. 经典领导选举算法:Bully 算法
  9. Hive基本查询语法
  10. Linux_快速查找文件