题意:给出一个数列,问修改差值最小,使得整个数列为严格递增,严格递减的数列,连续2个数字不能相同
思路:正的,反的跑一遍,记录当前这个如果为峰顶,左边要改多少,右边要改多少,
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N=200005; 5 ll a[N]; 6 ll l[N],r[N]; 7 ll c[N]; 8 9 int main(){ 10 int n; 11 cin>>n; 12 ll Max=0; 13 int sss=0; 14 for(int i=1;i<=n;i++){ 15 scanf("%lld",&a[i]); 16 Max=max(Max,a[i]); 17 } 18 ll M=0; 19 for(int i=1;i<=n;i++){ 20 if(a[i]<=M) 21 { 22 l[i]=l[i-1]+(M+1-a[i]); 23 M++; 24 } 25 else { 26 l[i]=l[i-1]; 27 } 28 M=max(M,a[i]); 29 c[i]=M; 30 // cout<<c[i]<<" "; 31 } 32 //cout<<endl; 33 M=0; 34 for(int i=n;i>=1;i--){ 35 if(a[i]<=M){ 36 r[i]=r[i+1]+M+1-a[i]; 37 M++; 38 } 39 else r[i]=r[i+1]; 40 M=max(M,a[i]); 41 c[i]=max(c[i],M); 42 } 43 ll Min=1e18; 44 Max++; 45 for(int i=1;i<=n;i++){ 46 ll sum=c[i]-a[i]; 47 sum+=l[i-1]+r[i+1]; 48 // cout<<c[i]<<endl; 49 Min=min(Min,sum); 50 } 51 cout<<Min<<endl; 52 }