UVa11054 Wine trading in Gergovia

本文解析了UVa11054葡萄酒交易问题,这是一个经典的贪心算法题目,与NOIP2002均分纸牌问题类似。文中提供了一个简洁的C++代码实现,用于最小化交易过程中酒的运输工作量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UVa11054 Wine trading in Gergovia

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

Solution

就是一个最简单的贪心,跟noip2002均分纸牌很像

  • 代码
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef long long LL;
const int maxn = 100005;
LL ans, a[maxn];
int n;

int main()
{
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
    register int i;
    while(scanf("%d", &n) == 1) {
      if(n == 0) break;
      for(i = 1; i <= n; i++)
        scanf("%lld", &a[i]);
      ans = 0;
      for(i = 1; i < n; i++)
        if(a[i]) {
          ans += abs(a[i]);
          a[i + 1] += a[i];
        }
      printf("%lld\n", ans);
    }
    return 0;
}
标题基于SpringBoot的在线网络学习平台研究AI更换标题第1章引言介绍基于SpringBoot的在线网络学习平台的研究背景、意义、国内外现状、论文研究方法及创新点。1.1研究背景与意义阐述在线网络学习平台的重要性及其在教育领域的应用价值。1.2国内外研究现状分析当前国内外在线网络学习平台的发展状况及趋势。1.3研究方法与创新点说明本研究采用的方法论和在研究过程中的创新之处。第2章相关理论技术概述SpringBoot框架、在线教育理论及相关技术基础。2.1SpringBoot框架概述介绍SpringBoot框架的特点、优势及其在Web应用中的作用。2.2在线教育理论阐述在线教育的基本理念、教学模式及其与传统教育的区别。2.3相关技术基础介绍开发在线网络学习平台所需的关键技术,如前端技术、数据库技术等。第3章在线网络学习平台设计详细描述基于SpringBoot的在线网络学习平台的整体设计方案。3.1平台架构设计给出平台的整体架构图,并解释各个模块的功能及相互关系。3.2功能模块设计详细介绍平台的主要功能模块,如课程管理、用户管理、在线考试等。3.3数据库设计说明平台的数据库设计方案,包括数据表结构、数据关系等。第4章平台实现与测试阐述平台的实现过程及测试方法。4.1平台实现详细介绍平台的开发环境、开发工具及实现步骤。4.2功能测试对平台的主要功能进行测试,确保功能正常且符合预期要求。4.3性能测试对平台的性能进行测试,包括响应时间、并发用户数等指标。第5章平台应用与分析分析平台在实际应用中的效果及存在的问题,并提出改进建议。5.1平台应用效果介绍平台在实际教学中的应用情况,包括用户反馈、使用情况等。5.2存在问题及原因分析分析平台在运行过程中出现的问题及其原因,如技术瓶颈、用户体验等。5.3改进建议与措施针对存在的问题提出具体的改进建议和措施,以提高平台的性能和用户满意度
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值