30962023-02-15 16:53:01horvathabelFasor (40)cpp17Wrong answer 22/40245ms34812 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};
		}
		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};
				 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, 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,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
base22/40
1Accepted0/03ms1812 KiB
2Accepted0/09ms3292 KiB
3Wrong answer0/23ms2220 KiB
4Wrong answer0/23ms2460 KiB
5Wrong answer0/22ms2512 KiB
6Accepted2/23ms2644 KiB
7Wrong answer0/23ms2876 KiB
8Accepted2/24ms3488 KiB
9Wrong answer0/29ms5080 KiB
10Accepted2/212ms5208 KiB
11Accepted2/27ms4028 KiB
12Wrong answer0/26ms4156 KiB
13Accepted2/243ms7752 KiB
14Wrong answer0/239ms8016 KiB
15Accepted2/271ms11988 KiB
16Accepted2/275ms11992 KiB
17Accepted2/282ms12140 KiB
18Accepted2/282ms12104 KiB
19Accepted2/282ms12240 KiB
20Wrong answer0/228ms8368 KiB
21Accepted2/271ms12040 KiB
22Time limit exceeded0/2245ms34812 KiB