K-th Maximum Problem Code: KMAX2 Codechef Solution

 

 K-th Maximum Problem Code: KMAX2
Submit

Read problem statements in MandarinRussian, and Vietnamese as well.

You are given a sequence of integers A1,A2,,AN and an integer K. Find the number of contiguous subsequences AL,AL+1,,AR such that RL+1K and the K-th element of the subsequence (AL+K1) is equal to the maximum of all elements of the entire sequence.

Input Format

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains two space-separated integers N and K.
  • The second line contains N space-separated integers A1,A2,,AN.

Output Format

For each test case, print a single line containing one integer --- the number of contiguous subsequences satisfying the given conditions.

Constraints

  • 1T200
  • 1KN2105
  • |Ai|105 for each valid i
  • the sum of N over all test cases does not exceed 5105

Subtasks

Subtask #1 (10 points)

  • T10
  • N100

Subtask #2 (90 points) original constraints

Sample Input 1 

1
5 3
1 2 3 4 5

Sample Output 1 

1

Explanation

Example case 1: (3,4,5) is the only contiguous subsequence such that its 3-rd element is equal to the maximum of the whole sequence (which is 5).














































Solution in C++


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
ll t,n,k,i;
cin>>t;
while(t--){
cin>>n>>k;
k--;
vector<ll>v(n);
ll mx=INT_MIN;
map<ll,ll>mp;
for(i=0; i<n; i++){
cin>>v[i];
mx = max(mx, v[i]);
}
ll c=0;
for(i=0; i<n; i++){
ll x = i+k;
//cout<<x<<" "<<v[x]<<"\n";
if(x<n && v[x]==mx){
//cout<<x<<" <== \n";
c+=(n-x);
}
}
cout<<c<<"\n";
}
}

Comments