K-th Maximum Problem Code: KMAX2 Codechef Solution
K-th Maximum Problem Code: KMAX2Submit
Read problem statements in Mandarin, Russian, and Vietnamese as well.
You are given a sequence of integers and an integer . Find the number of contiguous subsequences such that and the -th element of the subsequence () is equal to the maximum of all elements of the entire sequence.
Input Format
- The first line of the input contains a single integer denoting the number of test cases. The description of test cases follows.
- The first line of each test case contains two space-separated integers and .
- The second line contains space-separated integers .
Output Format
For each test case, print a single line containing one integer --- the number of contiguous subsequences satisfying the given conditions.
Constraints
- for each valid
- the sum of over all test cases does not exceed
Subtasks
Subtask #1 (10 points)
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: is the only contiguous subsequence such that its -rd element is equal to the maximum of the whole sequence (which is ).
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
Post a Comment