31022023-02-15 17:08:08horvathabelFasor (40)cpp17Accepted 40/4037ms11588 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); 
	} 
	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/03ms1824 KiB
2Accepted0/04ms2528 KiB
3Accepted2/23ms2252 KiB
4Accepted2/23ms2460 KiB
5Accepted2/23ms2676 KiB
6Accepted2/23ms2884 KiB
7Accepted2/23ms3248 KiB
8Accepted2/23ms3232 KiB
9Accepted2/24ms3724 KiB
10Accepted2/24ms3680 KiB
11Accepted2/24ms3808 KiB
12Accepted2/24ms3892 KiB
13Accepted2/217ms7380 KiB
14Accepted2/217ms7372 KiB
15Accepted2/230ms11260 KiB
16Accepted2/232ms11136 KiB
17Accepted2/234ms11256 KiB
18Accepted2/234ms11136 KiB
19Accepted2/234ms11260 KiB
20Accepted2/214ms7636 KiB
21Accepted2/230ms11472 KiB
22Accepted2/237ms11588 KiB