题意
一题街道上很多酒店,交易葡萄酒,正数为卖出葡萄酒,负数为需要葡萄酒,总需求量和总售出量是相等的,从一家店到另外一家店需要路费(路费=距离×运算量),假设每家店线性排列且相邻两店之间距离都是1,求最小路费。
思路
开始完全找不到规律,看了题解才qwq
不过可以发现一家可以从左右两侧来买酒
而第一家只能向右侧买(卖)酒
这里买卖其实都一样,只是正负而已
把第一家的酒交给下一家,路费就是酒×1了;或者把负数的酒交给下一家,表示待会计算下家时有酒了就会反方向运过来,路费也是酒x1。然后继续下一家,由于上一家运费和供求都处理完了,第二家也可以同样处理它的当前供求。
所有的实际就是从最左开始运输的总和
代码
// https://blog.csdn.net/hcbbt/article/details/10637183
// https://blog.csdn.net/keshuai19940722/article/details/10266267
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 100000 + 10;
int n;
int wine[ maxn ];
int main () {
while ( ~scanf ( "%d", &n ) && n ) {
for ( int i = 0; i < n; ++i )
scanf ( "%d", &wine[ i ] );
ll money = 0;
for ( int i = 0; i < n; ++i ) {
money += abs ( wine[ i ] );
wine[ i + 1 ] += wine[ i ];
}
printf ( "%lld\n", money );
}
return 0;
}