Penchick and Satay Sticks

B. Penchick and Satay Sticks

Problem

Penchick and his friend Kohane are touring Indonesia, and their next stop is in Surabaya!

In the bustling food stalls of Surabaya, Kohane bought n n n satay sticks and arranged them in a line, with the i i i-th satay stick having length p i p_i pi. It is given that p p p is a permutation ∗ ^{\text{∗}} of length n n n.

Penchick wants to sort the satay sticks in increasing order of length, so that p i = i p_i=i pi=i for each 1 ≤ i ≤ n 1\le i\le n 1in. For fun, they created a rule: they can only swap neighboring satay sticks whose lengths differ by exactly 1 1 1. Formally, they can perform the following operation any number of times (including zero):

  • Select an index i i i ( 1 ≤ i ≤ n − 1 1\le i\le n-1 1in1) such that ∣ p i + 1 − p i ∣ = 1 |p_{i+1}-p_i|=1 pi+1pi=1;
  • Swap p i p_i pi and p i + 1 p_{i+1} pi+1.

Determine whether it is possible to sort the permutation p p p, thus the satay sticks, by performing the above operation.

∗ ^{\text{∗}} A permutation of length n n n is an array consisting of n n n distinct integers from 1 1 1 to n n n in arbitrary order. For example, [ 2 , 3 , 1 , 5 , 4 ] [2,3,1,5,4] [2,3,1,5,4] is a permutation, but [ 1 , 2 , 2 ] [1,2,2] [1,2,2] is not a permutation ( 2 2 2 appears twice in the array), and [ 1 , 3 , 4 ] [1,3,4] [1,3,4] is also not a permutation ( n = 3 n=3 n=3 but there is 4 4 4 in the array).

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 2 ⋅ 1 0 5 1 \le t \le 2\cdot 10^5 1t2105). The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2\cdot 10^5 1n2105) — the number of satay sticks.

The second line of each test case contains n n n integers p 1 , p 2 , … , p n p_1, p_2, \ldots, p_n p1,p2,,pn ( 1 ≤ p i ≤ n 1 \le p_i \le n 1pin) — the permutation p p p representing the length of the satay sticks.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2\cdot 10^5 2105.

Output

For each test case, output “YES” if it is possible to sort permutation p p p by performing the operation. Otherwise, output “NO”.

You can output the answer in any case (upper or lower). For example, the strings “yEs”, “yes”, “Yes”, and “YES” will be recognized as positive responses.

Example

Input
2
4
2 1 3 4
4
4 2 3 1
Output
YES
NO

Note

In the first test case, we can sort permutation p = [ 2 , 1 , 3 , 4 ] p = [2, 1, 3, 4] p=[2,1,3,4] by performing an operation on index 1 1 1 ( ∣ p 2 − p 1 ∣ = ∣ 1 − 2 ∣ = 1 |p_2 - p_1| = |1 - 2| = 1 p2p1=∣12∣=1), resulting in p = [ 1 , 2 , 3 , 4 ] p = [1, 2, 3, 4] p=[1,2,3,4].

In the second test case, it can be proven that it is impossible to sort permutation p = [ 4 , 2 , 3 , 1 ] p = [4, 2, 3, 1] p=[4,2,3,1] by performing the operation. Here is an example of a sequence of operations that can be performed on the permutation:

  • Select i = 2 i = 2 i=2 ( ∣ p 3 − p 2 ∣ = ∣ 3 − 2 ∣ = 1 |p_3 - p_2| = |3 - 2| = 1 p3p2=∣32∣=1). This results in p = [ 4 , 3 , 2 , 1 ] p = [4, 3, 2, 1] p=[4,3,2,1].
  • Select i = 1 i = 1 i=1 ( ∣ p 2 − p 1 ∣ = ∣ 3 − 4 ∣ = 1 |p_2 - p_1| = |3 - 4| = 1 p2p1=∣34∣=1). This results in p = [ 3 , 4 , 2 , 1 ] p = [3, 4, 2, 1] p=[3,4,2,1].
  • Select i = 3 i = 3 i=3 ( ∣ p 4 − p 3 ∣ = ∣ 1 − 2 ∣ = 1 |p_4 - p_3| = |1 - 2| = 1 p4p3=∣12∣=1). This results in p = [ 3 , 4 , 1 , 2 ] p = [3, 4, 1, 2] p=[3,4,1,2].

Unfortunately, permutation p p p remains unsorted after performing the operations.

Code

// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <stack>//栈
// #include <deque>//堆/优先队列
// #include <queue>//队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
typedef long long ll;
const ll N=2e5+10;
ll a[N];
  
void solve()
{
    ll n;
    cin>>n;

    for(ll i=1;i<=n;i++) cin>>a[i];

    ll m=2;
    while(m--)
    {
        for(ll i=1;i<=n;i++)
        {
            if(a[i-1]-a[i]==1)
                swap(a[i],a[i-1]);
        }
    }

    ll op=0;
    for(ll i=1;i<=n;i++)
    {
        if(a[i]!=i) op=1;
    }

    if(op) cout<<"NO"<<endl;
    else cout<<"YES"<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);

    ll t;
    cin>>t;
    while(t--) solve();
    
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pretty Boy Fox

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值