#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define f1 first
#define s2 second
#define pb push_back
#define mp make_pair
#define int long long
#define fri(a) freopen(a,"r",stdin);
#define fro(a) freopen(a,"w",stdout);
const int N=100005;
const int INF=INT_MAX;
int n,dizi[N],maxst[N][21],tleft[N],tright[N],ans=INT_MAX;
stack<int>st1;
void build(){
for(int j=1;j<21;j++){
for(int i=0;i+(1<<j)<N;i++){
maxst[i][j]=max(maxst[i][j-1],maxst[i+(1<<(j-1))][j-1]);
}
}
}
int qmax(int l,int r){
int j=log2(r-l+1);
return max(maxst[l][j],maxst[r-(1<<j)+1][j]);
}
void prepro(){
for(int i=0;i<=n;i++){
while(!st1.empty() && dizi[i]<=dizi[st1.top()]){
st1.pop();
}
if(!st1.empty()){
tleft[i]=st1.top()+1;
}
else tleft[i]=1;
st1.push(i);
}
while(!st1.empty()){
st1.pop();
}
//cout<<"sa"<<endl;
for(int i=n+1;i>0;i--){
while(!st1.empty() && dizi[i]<=dizi[st1.top()]){
st1.pop();
}
if(!st1.empty()){
tright[i]=st1.top()-1;
}
else tright[i]=n;
st1.push(i);
}
}
int solve(){
for(int i=1;i<=n;i++){
//cout<<tright[i]<<" "<<tleft[i]<<" "<<qmax(tright[i],tleft[i])<<endl;
ans=min(ans,(tright[i]-tleft[i]>0?(qmax(tleft[i],tright[i])-dizi[i]):INT_MAX));
}
return ans;
}
int32_t main(){
// fri("in.txt");
// fro("out.txt");
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
// memset(dizi,-1,sizeof(dizi));
for(int i=1;i<=n;i++){
cin>>dizi[i];
maxst[i][0]=dizi[i];
}
build();
//cout<<qmax(2,2);
prepro();
//cout<<tleft[2]<<" "<<tright[2]<<endl;
cout<<solve()<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
30 ms |
16760 KB |
Output isn't correct |
2 |
Correct |
29 ms |
16760 KB |
Output is correct |
3 |
Correct |
30 ms |
16888 KB |
Output is correct |
4 |
Correct |
31 ms |
17016 KB |
Output is correct |
5 |
Incorrect |
43 ms |
19832 KB |
Output isn't correct |
6 |
Incorrect |
44 ms |
19192 KB |
Output isn't correct |
7 |
Incorrect |
44 ms |
19252 KB |
Output isn't correct |