This commit is contained in:
Zengtudor 2024-11-20 15:20:54 +08:00
parent 2c7198078f
commit 1042f508dd

View File

@ -1,51 +1,44 @@
#include <bits/stdc++.h> #include <bits/stdc++.h>
using ll = int64_t; using ll = int64_t;
using std::cin,std::cout; using namespace std;
const ll maxn{ll(2e5+5)},inf{ll(1)<<60}; const ll maxn{ll(2e5+5)},inf{ll(1)<<60};
ll n,a[maxn],fl[maxn],fr[maxn],ans{inf},gl[maxn],gr[maxn]; ll n,a[maxn],d[maxn],p[maxn],s[maxn],ans{inf};
template<class T> template<class T>
void pa(T *t){ void pa(T *t){
for(ll i{1};i<=n;i++)cout<<t[i]<<' '; for(ll i{1};i<=n;i++)cout<<t[i]<<' ';
cout<<'\n'; cout<<'\n';
} }
template<class T,class ...Args>
void pa(T&&t,Args&&...args){
pa(t);
pa(args...);
}
int main(){ int main(){
std::iostream::sync_with_stdio(false),std::cin.tie(nullptr),std::cout.tie(nullptr); iostream::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n; cin>>n;
for(ll i{1};i<=n;i++)cin>>a[i];
for(ll i{1};i<=n;i++){ for(ll i{1};i<=n;i++){
cin>>a[i]; d[i]=a[i]-a[i-1];
} if(d[i]<=0){
for(ll i{2};i<=n;i++){ p[i]=p[i-1]+1-d[i];
if(a[i-1]+fl[i-1]>=a[i]){ }else{
fl[i]=fl[i-1]+(a[i-1]-a[i])+1; p[i]=p[i-1];
}
gl[i]=gl[i-1];
if(fl[i-1]<fl[i]){
gl[i]+=fl[i]-fl[i-1];
} }
} }
for(ll i{n-1};i>=1;i--){ for(ll i{n};i>=1;i--){
if(a[i+1]+fr[i+1]>=a[i]){ if(d[i]>=0){
fr[i]=fr[i+1]+(a[i+1]-a[i])+1; s[i]=s[i+1]+d[i]+1;
} }else{
gr[i]=gr[i+1]; s[i]=s[i+1];
if(fr[i+1]<fr[i]){
gr[i]+=fr[i]-fr[i+1];
} }
} }
// pa(fl); // pa(d,p,s);
// pa(fr);
// pa(gl);
// pa(gr);
for(ll i{1};i<=n;i++){ for(ll i{1};i<=n;i++){
ans=std::min( ans=min(ans,max(p[i],s[i+1]));
ans,
(gl[i]+gr[i])
);
} }
cout<<ans<<'\n'; cout<<ans<<'\n';
} }