A15115 | Odd Subarrays
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
For an array $[b_1, b_2, \ldots, b_m]$ define its number of inversions as the number of pairs $(i, j)$ of integers such that $1 \le i < j \le m$ and $b_i>b_j$ . Let's call array $b$ odd if its number of inversions is odd.
For example, array $[4, 2, 7]$ is odd, as its number of inversions is $1$ , while array $[2, 1, 4, 3]$ isn't, as its number of inversions is $2$ .
You are given a permutation $[p_1, p_2, \ldots, p_n]$ of integers from $1$ 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?
For example, array $[4, 2, 7]$ is odd, as its number of inversions is $1$ , while array $[2, 1, 4, 3]$ isn't, as its number of inversions is $2$ .
You are given a permutation $[p_1, p_2, \ldots, p_n]$ of integers from $1$ 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?
输入格式
The first line of the input contains a single integer $t$ ( $1 \le t \le 10^5$ ) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ( $1 \le n \le 10^5$ ) — the size of the permutation.
The second line of each test case contains $n$ integers $p_1, p_2, \ldots, p_n$ ( $1 \le p_i \le n$ , all $p_i$ are distinct) — the elements of the permutation.
The sum of $n$ over all test cases doesn't exceed $2\cdot 10^5$ .
The first line of each test case contains a single integer $n$ ( $1 \le n \le 10^5$ ) — the size of the permutation.
The second line of each test case contains $n$ integers $p_1, p_2, \ldots, p_n$ ( $1 \le p_i \le n$ , all $p_i$ are distinct) — the elements of the permutation.
The sum of $n$ over all test cases doesn't exceed $2\cdot 10^5$ .
输出格式
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.
输入输出样例
输入 #1
5 3 1 2 3 4 4 3 2 1 2 1 2 2 2 1 6 4 5 6 1 2 3
输出 #1
0 2 0 1 1
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]$ , both of which are odd since their numbers of inversions are $1$ .
In the fourth test case, we can split our permutation into a single subarray $[2, 1]$ , which is odd.
In the fifth test case, we can split our permutation into subarrays $[4, 5], [6, 1, 2, 3]$ . The first subarray has $0$ inversions, and the second has $3$ , so it is odd.
In the second test case, we can split our permutation into subarrays $[4, 3], [2, 1]$ , both of which are odd since their numbers of inversions are $1$ .
In the fourth test case, we can split our permutation into a single subarray $[2, 1]$ , which is odd.
In the fifth test case, we can split our permutation into subarrays $[4, 5], [6, 1, 2, 3]$ . The first subarray has $0$ inversions, and the second has $3$ , so it is odd.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted