CF1658C Shinju and the Lost Permutation- Codeforces Round #779 (Div. 2)

Shinju and the Lost Permutation - 洛谷

C. Shinju and the Lost Permutation

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Shinju loves permutations very much! Today, she has borrowed a permutation pp from Juju to play with.

The ii-th cyclic shift of a permutation pp is a transformation on the permutation such that p=[p1,p2,…,pn]p=[p1,p2,…,pn] will now become p=[pn−i+1,…,pn,p1,p2,…,pn−i]p=[pn−i+1,…,pn,p1,p2,…,pn−i].

Let's define the power of permutation pp as the number of distinct elements in the prefix maximums array bb of the permutation. The prefix maximums array bb is the array of length nn such that bi=max(p1,p2,…,pi)bi=max(p1,p2,…,pi). For example, the power of [1,2,5,4,6,3][1,2,5,4,6,3] is 44 since b=[1,2,5,5,6,6]b=[1,2,5,5,6,6] and there are 44 distinct elements in bb.

Unfortunately, Shinju has lost the permutation pp! The only information she remembers is an array cc, where cici is the power of the (i−1)(i−1)-th cyclic shift of the permutation pp. She's also not confident that she remembers it correctly, so she wants to know if her memory is good enough.

Given the array cc, determine if there exists a permutation pp that is consistent with cc. You do not have to construct the permutation pp.

A permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

Input

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤5⋅1031≤t≤5⋅103) — the number of test cases.

The first line of each test case contains an integer nn (1≤n≤1051≤n≤105).

The second line of each test case contains nn integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤n1≤ci≤n).

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

Output

For each test case, print "YES" if there is a permutation pp exists that satisfies the array cc, and "NO" otherwise.

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).

Example

input

Copy

6
1
1
2
1 2
2
2 2
6
1 2 4 6 3 5
6
2 3 1 2 3 4
3
3 2 1

output

Copy

YES
YES
NO
NO
YES
NO

Note

In the first test case, the permutation [1][1] satisfies the array cc.

In the second test case, the permutation [2,1][2,1] satisfies the array cc.

In the fifth test case, the permutation [5,1,2,4,6,3][5,1,2,4,6,3] satisfies the array cc. Let's see why this is true.

  • The zeroth cyclic shift of pp is [5,1,2,4,6,3][5,1,2,4,6,3]. Its power is 22 since b=[5,5,5,5,6,6]b=[5,5,5,5,6,6] and there are 22 distinct elements — 55 and 66.
  • The first cyclic shift of pp is [3,5,1,2,4,6][3,5,1,2,4,6]. Its power is 33 since b=[3,5,5,5,5,6]b=[3,5,5,5,5,6].
  • The second cyclic shift of pp is [6,3,5,1,2,4][6,3,5,1,2,4]. Its power is 11 since b=[6,6,6,6,6,6]b=[6,6,6,6,6,6].
  • The third cyclic shift of pp is [4,6,3,5,1,2][4,6,3,5,1,2]. Its power is 22 since b=[4,6,6,6,6,6]b=[4,6,6,6,6,6].
  • The fourth cyclic shift of pp is [2,4,6,3,5,1][2,4,6,3,5,1]. Its power is 33 since b=[2,4,6,6,6,6]b=[2,4,6,6,6,6].
  • The fifth cyclic shift of pp is [1,2,4,6,3,5][1,2,4,6,3,5]. Its power is 44 since b=[1,2,4,6,6,6]b=[1,2,4,6,6,6].

Therefore, c=[2,3,1,2,3,4]c=[2,3,1,2,3,4].

In the third, fourth, and sixth testcases, we can show that there is no permutation that satisfies array cc.

本题一是题意难以理解,二是细节比较难找。在理题意基础上,本题突破点在于数组值为1的位置,因为是p排列的权值,个数字互不相同,故只有在n这个数字在第一个位置时,其权值为1,所以我们可以排除掉有多个1的数组。另外,我们找到唯一一个1的位置时,对其进行题目所说的循环操作 ,也就是n这个数字向右推进,推进之后,最左端会进来一个新的数字,而这个新的数字最多只会把权值增加1,故可以判断推进之后权值是否比推进之前超过1即可

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

int a[100000+10];

int main ()
{

    /*

      1 [ pn p1 p2 .. pn-1 ]

      2 [ pn-12 pn p1 p2 ..pn-2]

      c1是p第 0轮循环权值

      c2是p第 1轮循环权值

      c3是p第 2轮循环权值

    */

     int t;
     cin>>t;

     while(t--)
     {
         int n;

         cin>>n;

         int pos=0,flag=0;

         for(int i=1;i<=n;i++)
         {
             cin>>a[i];
             if(pos==0&&a[i]==1)
             {
                 pos=i;
             }
             else if(a[i]==1)
             {
                 flag=1;

             }

         }

         if(flag)
         {
             cout<<"NO"<<endl;

             continue;

         }
         else
         {
             int nex=pos+1;

             flag=0;

             for(int i=1;i<=n;i++)
             {
                 if(nex==n+1)
                 {
                     nex=1;


                 }
                 if(a[nex]-a[pos]>1)
                 {
                     flag=1;

                     break;
                 }

                 pos=nex;

                 nex++;

             }

             if(flag)
             {
                 cout<<"NO"<<endl;

             }
             else
             {
                 cout<<"YES"<<endl;

             }
         }


     }

    return 0;

}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

秦三码

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

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

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

打赏作者

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

抵扣说明:

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

余额充值