首页 > 代码库 > (白书训练计划)UVa 11054 Wine trading in Gergovia(等价转换)
(白书训练计划)UVa 11054 Wine trading in Gergovia(等价转换)
题目地址:UVa 11054
很巧妙的一道题,这题是利用的等价转换,对每一条路来说,假如右边生产的比左边的多x,那么不管起点是哪,终点是哪,都可以把左右两侧的看成两个点,要从这条路上运送x个劳动力。再由于总和是0,所以只需要算出一端的总和就可以,这样只要遍历一遍就可以算出来了。写出代码就很简单了。。。
代码如下:
#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <algorithm> #include <queue> using namespace std; #define LL long long int main() { LL n, i, s, x, a; while(scanf("%lld",&n)!=EOF&&n) { s=0; x=0; for(i=0;i<n;i++) { scanf("%lld",&a); s+=abs(x); x+=a; } printf("%lld\n",s); } return 0; }
(白书训练计划)UVa 11054 Wine trading in Gergovia(等价转换)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。