답안 #167910

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
167910 2019-12-10T19:05:55 Z muhi1112 Pismo (COCI18_pismo) C++17
0 / 70
93 ms 65540 KB
#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 ll long long
#define fri(a) freopen(a,"r",stdin);
#define fro(a) freopen(a,"w",stdout);
const int N=1000005;
const int INF=1e9+7;

int n,dizi[N],maxst[N][21],tleft[N],tright[N],ans=INF;
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]);
		}
	}
	return;
}
int qmax(int l,int r){
	int j=log2(r-l+1);
	return max(maxst[l][j],maxst[r-(1<<j)+1][j]);
}
void prepro1(){
	for(int i=0;i<=n;i++){
		while(!st1.empty() && dizi[i]<=dizi[st1.top()]){
			st1.pop();
		}
		tleft[i]=st1.top()+1;
		st1.push(i);
	}
	while(!st1.empty()){
		st1.pop();
	}
	return;
	//cout<<endl;

}
void prepro2(){
	for(int i=n+1;i>0;i--){
		while(!st1.empty() && dizi[i]<=dizi[st1.top()]){
			st1.pop();
		}
		tright[i]=st1.top()-1;
		st1.push(i);
	}
	return;
}
ll 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]:INF);
	}
	return ans;
}
int main(){
	//fri("in.txt");
	//fro("out.txt");
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>dizi[i];
		maxst[i][0]=dizi[i];
	}
	build();
	//cout<<qmax(2,2);
	prepro1();
	prepro2();
	//cout<<tleft[5]<<" "<<tright[5]<<endl;
	cout<<solve()<<endl;

return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 86 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Runtime error 83 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
3 Runtime error 80 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
4 Runtime error 85 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
5 Runtime error 93 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
6 Runtime error 93 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
7 Runtime error 91 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)