30992023-02-15 16:55:45horvathabelFasor (40)cpp17Time limit exceeded 38/40201ms34072 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0); 
	int n, k; 
	cin>>n>>k;
	vector<int> fa; 
	map<int, pair<int, int>> indx; 
	for (int i=0; i<n;i++){
		int z;
		cin>>z; 
		fa.push_back(z); 
		if (indx[z].first==0) indx[z].first=i+1;
		indx[z].second=i+1; 
	} 
	indx[-1]={k*-1,k*-1};
	stack<pair<int,int>> b; 
	vector<pair<int,int>> ans(n+1); 
	ans[0]={-1,k*-1};
	for (int i=0; i<n;i++){
		if (b.empty()){
			 b.push({fa[i],i});
			ans[i]={-1, k*-1-1};
		}
		else{
			 if (fa[i]<b.top().first){
				ans[i]=b.top(); 
				b.push({fa[i],i});

			 }
			 else{
				 int cnt=0; 
				 while (!b.empty() && fa[i]>=b.top().first){
					 b.pop();
				 }

				 if (b.empty()) ans[i]={-1,k*-1-1};
				 else ans[i]=b.top(); 
				 b.push({fa[i],i});
				 
			 }
		}
	}
	stack<pair<int,int>> bp; 
	vector<pair<int,int>> ans2(n+1); 
	for (int i=n; i>=0;i--){
		if (bp.empty()){
			 bp.push({fa[i],i});
			
			ans[i]={-1, n+k+1};
		}
		else{
			 if (fa[i]<bp.top().first){
				ans2[i]=bp.top(); 
				bp.push({fa[i],i});

			 }
			 else{
				 while (!bp.empty() && fa[i]>=bp.top().first){
					 bp.pop();
				 }
		
				 if (bp.empty()) ans2[i]={-1,n+k+1};
				 else ans2[i]=bp.top(); 
				 bp.push({fa[i],i});
				 
			 }
		}
	}
	
	for (int i=0; i<n;i++){
		if (ans[i].second+k<i && ans2[i].second-k>i){
			cout<<i+1; 
			return 0; 
		}
	}
	cout<<-1;

}
SubtaskSumTestVerdictTimeMemory
base38/40
1Accepted0/03ms1828 KiB
2Accepted0/08ms3296 KiB
3Accepted2/23ms2268 KiB
4Accepted2/23ms2480 KiB
5Accepted2/23ms2664 KiB
6Accepted2/23ms2748 KiB
7Accepted2/23ms3032 KiB
8Accepted2/24ms3252 KiB
9Accepted2/28ms5024 KiB
10Accepted2/29ms4764 KiB
11Accepted2/24ms3684 KiB
12Accepted2/24ms3692 KiB
13Accepted2/218ms7148 KiB
14Accepted2/217ms7432 KiB
15Accepted2/232ms11272 KiB
16Accepted2/232ms11492 KiB
17Accepted2/234ms11620 KiB
18Accepted2/235ms11696 KiB
19Accepted2/235ms11696 KiB
20Accepted2/216ms7952 KiB
21Accepted2/230ms11580 KiB
22Time limit exceeded0/2201ms34072 KiB