题目
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5+10;
int a[N], b[N];
int main()
{
int n;
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int i = 1; i <= n / 2; i++)
b[i] = a[i] - a[n+1-i];
ll ans = 0;
for(int i = 1; i <= n / 2; i++)
{
//假设前面已经处理完毕,考虑当前和右邻
if(b[i] > 0 && b[i+1] > 0) //两者都大于0
{ //下面相等也放进去,因为b[i+1]变为0之后属于不用考虑的情况,且操作数一致
if(b[i] >= b[i+1]) //完全按照节省方式b[i+1]能解决,b[i]不行,且后面不构成节省方案要求的相邻,因此必须直接处理
{
ans += b[i];
i++; //右邻已经被处理,跳
}
else
{
ans += b[i]; //这里只节省地处理完毕b[i],b[i+1]留给之后考虑
b[i+1] -= b[i];
}
}
else if(b[i] < 0 && b[i+1] < 0)
{
if(b[i] <= b[i+1])
{
ans += abs(b[i]);
i++;
}
else
{
ans += abs(b[i]);
b[i+1] -= b[i];
}
}
else ans += abs(b[i]);
}
cout << ans;
}