fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. ll e=0;
  5. int main() {
  6. int t;
  7. cin >> t;
  8. while (t--) {
  9. ll n,ind,ans;
  10. cin >> n>>ind;vector<ll>a(n+1,0);
  11. for(int i=1;i<n+1;i++){cin>>a[i];}
  12.  
  13.  
  14. auto it = upper_bound(a.begin(), a.end(), a[ind]);
  15.  
  16. if (it != a.end()) {
  17. ll x=distance(a.begin(),it);
  18. if(x>ind){ans=x-2;}
  19. else{
  20. iter_swap(a.begin() + ind, a.begin() + x);
  21. ll temp =distance(a.begin()+x,upper_bound(a.begin(), a.end(), a[x]));
  22. if(x==1){temp--;}
  23. ans =max(max(x-2,e),temp);
  24. }
  25. } else {
  26. ans=n-1;goto output;
  27. }
  28.  
  29. output:
  30. cout<<ans<<endl;
  31. }
  32.  
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0s 5288KB
stdin
3
6 1
12 10 14 11 8 3
6 5
7 2 727 10 12 13
2 2
1000000000 1
stdout
1
2
0