Algorithm


B. Odd Subarrays
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

For an array [b1,b2,,bm][�1,�2,…,��] define its number of inversions as the number of pairs (i,j)(�,�) of integers such that 1i<jm1≤�<�≤� and bi>bj��>��. Let's call array b odd if its number of inversions is odd.

For example, array [4,2,7][4,2,7] is odd, as its number of inversions is 11, while array [2,1,4,3][2,1,4,3] isn't, as its number of inversions is 22.

You are given a permutation [p1,p2,,pn][�1,�2,…,��] of integers from 11 to n (each of them appears exactly once in the permutation). You want to split it into several consecutive subarrays (maybe just one), so that the number of the odd subarrays among them is as large as possible.

What largest number of these subarrays may be odd?

Input

The first line of the input contains a single integer t (1t1051≤�≤105)  — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n (1n1051≤�≤105)  — the size of the permutation.

The second line of each test case contains n integers p1,p2,,pn�1,�2,…,�� (1pin1≤��≤�, all pi�� are distinct)  — the elements of the permutation.

The sum of n over all test cases doesn't exceed 21052⋅105.

Output

For each test case output a single integer  — the largest possible number of odd subarrays that you can get after splitting the permutation into several consecutive subarrays.

Example
input
Copy
5
3
1 2 3
4
4 3 2 1
2
1 2
2
2 1
6
4 5 6 1 2 3
output
Copy
0
2
0
1
1
Note

In the first and third test cases, no matter how we split our permutation, there won't be any odd subarrays.

In the second test case, we can split our permutation into subarrays [4,3],[2,1][4,3],[2,1], both of which are odd since their numbers of inversions are 11.

In the fourth test case, we can split our permutation into a single subarray [2,1][2,1], which is odd.

In the fifth test case, we can split our permutation into subarrays [4,5],[6,1,2,3][4,5],[6,1,2,3]. The first subarray has 00 inversions, and the second has 33, so it is odd.

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include<bits/stdc++.h>
using namespace std;


#define ll long long
#define endl '\n'
#define debug(n) cout<<(n)<<endl;
const ll INF = 2e18 + 99;

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;
  cin>>t;
  while(t--){
    int n;
    cin>>n;

    int arr[n];
    for(int i = 0; i < n; i++){
      cin>>arr[i];
    }
    if(n == 1){
      cout<<0<<endl;
      continue;
    }

    int count = 0;
    int i = 0, j = 1;
    while(i < n-1 && j < n){
      if(arr[i] > arr[j]){
        count++;
        i += 2;
        j += 2;
      }
      else{
        i++;
        j++;
      }
    }
    cout<<count<<endl;
  }

}
Copy The Code & Try With Live Editor

Input

x
+
cmd
5
3
1 2 3
4
4 3 2 1
2
1 2
2
2 1
6
4 5 6 1 2 3

Output

x
+
cmd
0
2
0
1
1
Advertisements

Demonstration


Codeforcess Solution 1686-B B. Odd Subarrays ,C++, Java, Js and Python ,1686-B,Codeforcess Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+