31002023-02-15 16:55:45horvathabelFasor (40)cpp17Accepted 40/40199ms34352 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
base40/40
1Accepted0/03ms1828 KiB
2Accepted0/08ms3288 KiB
3Accepted2/23ms2236 KiB
4Accepted2/23ms2448 KiB
5Accepted2/23ms2660 KiB
6Accepted2/23ms2888 KiB
7Accepted2/23ms2872 KiB
8Accepted2/24ms3140 KiB
9Accepted2/28ms4912 KiB
10Accepted2/29ms4776 KiB
11Accepted2/24ms3836 KiB
12Accepted2/24ms3924 KiB
13Accepted2/217ms7332 KiB
14Accepted2/217ms7588 KiB
15Accepted2/232ms11296 KiB
16Accepted2/232ms11552 KiB
17Accepted2/235ms11680 KiB
18Accepted2/235ms11800 KiB
19Accepted2/235ms11632 KiB
20Accepted2/216ms8140 KiB
21Accepted2/232ms11984 KiB
22Accepted2/2199ms34352 KiB