30972023-02-15 16:54:32horvathabelFasor (40)cpp17Time limit exceeded 38/40248ms34344 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
	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/03ms1812 KiB
2Accepted0/010ms3300 KiB
3Accepted2/23ms2212 KiB
4Accepted2/23ms2428 KiB
5Accepted2/23ms2644 KiB
6Accepted2/23ms2860 KiB
7Accepted2/23ms3088 KiB
8Accepted2/24ms3500 KiB
9Accepted2/29ms5156 KiB
10Accepted2/212ms5024 KiB
11Accepted2/27ms3940 KiB
12Accepted2/26ms3940 KiB
13Accepted2/245ms7684 KiB
14Accepted2/241ms7896 KiB
15Accepted2/270ms11716 KiB
16Accepted2/281ms11860 KiB
17Accepted2/286ms11880 KiB
18Accepted2/286ms12172 KiB
19Accepted2/286ms11832 KiB
20Accepted2/228ms8076 KiB
21Accepted2/271ms11840 KiB
22Time limit exceeded0/2248ms34344 KiB