B. Battle Cows
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
ඞ
There are n n n cows participating in a coding tournament. Cow i i i has a Cowdeforces rating of a i a_i ai (all distinct), and is initially in position i i i. The tournament consists of n − 1 n-1 n−1 matches as follows:
- The first match is between the cow in position 1 1 1 and the cow in position 2 2 2.
- Subsequently, each match i i i is between the cow in position i + 1 i+1 i+1 and the winner of match i − 1 i-1 i−1.
- In each match, the cow with the higher Cowdeforces rating wins and proceeds to the next match.
You are the owner of cow k k k. For you, winning the tournament is not important; rather, you want your cow to win in as many matches as possible. As an acquaintance of the tournament organizers, you can ask them to swap the position of your cow with another cow only once, or you can choose to do nothing.
Find the maximum number of wins your cow can achieve.
Input
Each test contains multiple test cases. The first line contains an integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n n n and k k k ( 2 ≤ n ≤ 1 0 5 , 1 ≤ k ≤ n 2 \le n \le 10^5, 1 \le k \le n 2≤n≤105,1≤k≤n) — the number of cows and your cow’s index.
The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1≤ai≤109) — the Cowdeforces rating of the cows. It is guaranteed that a i a_i ai’s are pairwise different.
It is guaranteed that the sum of n n n over all test cases does not exceed 1 0 5 10^5 105.
Output
For each test case, print one integer: the maximum number of wins cow k k k can achieve if you choose to swap (or do nothing) optimally.
的最大胜利数。
Example
inputCopy
3
6 1
12 10 14 11 8 3
6 5
7 2 727 10 12 13
2 2
1000000000 1
outputCopy
1
2
0
Note
In the first test case, it is optimal to do nothing. Let a ′ a' a′ be the Cowdeforces rating of the cows in the original order (with your cow’s rating bolded), then
- Initially, a ′ = [ 12 , 10 , 14 , 11 , 8 , 3 ] a' = [\mathbf{12}, 10, 14, 11, 8, 3] a′=[12,10,14,11,8,3].
- Your cow plays against the cow with Cowdeforces rating 10 10 10 and wins. a ′ = [ 12 , 14 , 11 , 8 , 3 ] a' = [\mathbf{12}, 14, 11, 8, 3] a′=[12,14,11,8,3].
- Your cow plays against the cow with Cowdeforces rating 14 14 14 and loses.
In total, your cow wins 1 1 1 match.
In the second test case, it is optimal to swap your cow to position 3 3 3. Then, let a ′ a' a′ be the Cowdeforces rating of the cows in the order after the swap.
- Initially, a ′ = [ 7 , 2 , 12 , 10 , 727 , 13 ] a' = [7, 2, \mathbf{12}, 10, 727, 13] a′=[7,2,12,10,727,13].
- The cow with Cowdeforces rating 7 7 7 plays against the cow with Cowdeforces rating 2 2 2 and wins. a ′ = [ 7 , 12 , 10 , 727 , 13 ] a' = [7, \mathbf{12}, 10, 727, 13] a′=[7,12,10,727,13].
- The cow with Cowdeforces rating 7 7 7 plays against your cow, and your cow wins. a ′ = [ 12 , 10 , 727 , 13 ] a' = [\mathbf{12}, 10, 727, 13] a′=[12,10,727,13].
- Your cow plays against the cow with Cowdeforces rating 10 10 10 and wins. a ′ = [ 12 , 727 , 13 ] a' = [\mathbf{12}, 727, 13] a′=[12,727,13].
- Your cow plays against the cow with Cowdeforces rating 727 727 727 and loses.
In total, your cow wins 2 2 2 matches.
AC代码:
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<numeric>
#include<iomanip>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int>PII;
const int N=3e5+10;
const int MOD=1e9 + 7;
const int INF=0X3F3F3F3F;
const int dx[]={-1,1,0,0,-1,-1,+1,+1};
const int dy[]={0,0,-1,1,-1,+1,-1,+1};
const int M = 1e6 + 10;
int t;
int n, k;
int a[N];
int main()
{
cin >> t;
while(t --){
cin >> n >> k;
int f1 = 0, f2 = 0, f3 = 0, flag = 0;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
}
for(int i = 1; i < k; i ++)
{
if(a[i] > a[k])
{
flag ++;
if(flag == 1) f1 = i;
if(flag == 2) f2 = i;
}
}
for(int i = k + 1; i <= n; i ++)
{
if(a[k] < a[i])
{
f3 = i;
break;
}
}
if(!f1 && !f2)
{
if(f3)
cout << f3 - 2 << endl;
else cout << n - 1 << endl;
}
//cout << f1 << " " << f2 << " " << f3 << endl;
if(f1 && !f2)
{
if(f1 == 1)
cout << max({f1 - 2, 0, k - f1 - 1}) << endl;
else
cout << max({f1 - 2, 0, k - f1}) << endl;
}
if(f1 && f2)
{
if(f1 == 1)
cout << max({f1 - 2, 0, f2 - f1 - 1}) << endl;
else
cout << max({f1 - 2, 0, f2 - f1}) << endl;
}
}
return 0;
}