Submission #167886

# Submission time Handle Problem Language Result Execution time Memory
167886 2019-12-10T17:38:29 Z muhi1112 Pismo (COCI18_pismo) C++17
30 / 70
1000 ms 40824 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=1e5+5;
const int INF=1e9+7;

int n,dizi[N],lok[N],a,b,c,d;
pair<int,int>minst[N][25],maxst[N][25];

void loki(){
	for(int i=2;i<N;i++){
		lok[i]=lok[i/2]+1;
	}
}
void build(){
	for(int j=1;j<25;j++){
		for(int i=0;i+(1<<j)<N;i++){
			minst[i][j]=min(minst[i][j-1],minst[i+(1<<(j-1))][j-1]);
			maxst[i][j]=max(maxst[i][j-1],maxst[i+(1<<(j-1))][j-1]);
		}
	}
}
pair<int,int> qmax(int l,int r){
	int j=lok[(r-l+1)];
	return max(maxst[l][j],maxst[r-(1<<j)+1][j]);
}
pair<int,int> qmin(int l,int r){
	int j=lok[(r-l+1)];
	return min(minst[l][j],minst[r-(1<<j)+1][j]);
}

int solve(int l,int r){
	//cout<<"as"<<endl;
	if(l>=r){
		return INF;
	}
	pair<int,int> mini=qmin(l,r);
	pair<int,int> maxi=qmax(l,r);
	//cout<<l<<" "<<r<<" "<<mini.s2<<" "<<maxi.s2<<endl;
	int ans=INF;
	ans=min(ans,maxi.f1-mini.f1);
	ans=min(solve(l,mini.s2-1),ans);
	ans=min(solve(mini.s2+1,r),ans);
	ans=min(solve(l,maxi.s2-1),ans);
	ans=min(solve(maxi.s2+1,r),ans);
	if(ans<=0){
		cout<<ans;
		exit(0);
	}
	return ans;
}
int main(){
	//fri("in.txt");
	//fro("out.txt");
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>dizi[i];
		minst[i][0]={dizi[i],i};
		maxst[i][0]={dizi[i],i};
	}
	loki();
	build();
	//cout<<qmax(1,2).f1<<" "<<qmax(1,2).s2<<endl;
	cout<<solve(0,n-1)<<endl;

return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 75 ms 39800 KB Output is correct
2 Correct 77 ms 39928 KB Output is correct
3 Execution timed out 1090 ms 39928 KB Time limit exceeded
4 Correct 622 ms 39928 KB Output is correct
5 Execution timed out 1086 ms 40700 KB Time limit exceeded
6 Execution timed out 1072 ms 40824 KB Time limit exceeded
7 Execution timed out 1088 ms 40696 KB Time limit exceeded