AcWing 5993. 回文数组
原题链接
中等
作者:
y总好帅
,
2024-11-28 15:04:39
,
所有人可见
,
阅读 69
C++ 代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
LL a[N],b[N];
LL n;
LL ans;
int main() {
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=(n+1)/2;i++){
if(a[i]<a[n-i+1])b[i]=a[n-i+1]-a[i];
else b[n-i+1]=a[i]-a[n-i+1];
}
for(int i=1;i<=n;i++){
int minn=min(b[i],b[i+1]);
b[i]-=minn;
b[i+1]-=minn;
ans+=minn;
ans+=b[i];
}
cout<<ans;
return 0;
}