P3051 [USACO12MAR]Haybale Restacking G 题解
0. 前言
话说这题有至少四五倍经验啊,结论都一样。
本人太菜了考试的时候没有推出来。
将式子变形竟出现了
1. 翻译
农夫约翰订购了很多干草,他在农场里标记了
无奈之下,农夫约翰只能自己来移动这些干草。但他必须沿相邻位置来移动干草,每移动一捆干草到一个相邻位置,要消耗约翰一单位的能量。请帮约翰规划一下,他最少消耗多少能量才能让所有位置 的干草数量从
2. 思路
本题与均分纸牌大体一致,除了变成换无他。
维护
3. 代码
#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, a[N], b[N], c[N];
int main(){
cin >> n;
for(int i = 1; i <= n; i ++){
cin >> a[i] >> b[i];
c[i] += (b[i] - a[i] + c[i - 1]);
}
sort(c + 1, c + n +1);
int mid = c[n / 2 + 1];
long long ans = 0;
for(int i = 1; i <= n; i ++){
ans += abs(mid - c[i]);
}
cout << ans;
return 0;
}
非常简短。