CF1341C. Nastya and Strange Generator

Denis was very sad after Nastya rejected him. So he decided to walk through the gateways to have some fun. And luck smiled at him! When he entered the first courtyard, he met a strange man who was selling something.

Denis bought a mysterious item and it was… Random permutation generator! Denis could not believed his luck.

When he arrived home, he began to study how his generator works and learned the algorithm. The process of generating a permutation consists of 𝑛 steps. At the 𝑖-th step, a place is chosen for the number 𝑖 (1≤𝑖≤𝑛). The position for the number 𝑖 is defined as follows:

For all 𝑗 from 1 to 𝑛, we calculate 𝑟𝑗 — the minimum index such that 𝑗≤𝑟𝑗≤𝑛, and the position 𝑟𝑗 is not yet occupied in the permutation. If there are no such positions, then we assume that the value of 𝑟𝑗 is not defined.
For all 𝑡 from 1 to 𝑛, we calculate 𝑐𝑜𝑢𝑛𝑡𝑡 — the number of positions 1≤𝑗≤𝑛 such that 𝑟𝑗 is defined and 𝑟𝑗=𝑡.
Consider the positions that are still not occupied by permutation and among those we consider the positions for which the value in the 𝑐𝑜𝑢𝑛𝑡 array is maximum.
The generator selects one of these positions for the number 𝑖. The generator can choose any position.
Let’s have a look at the operation of the algorithm in the following example:

Let 𝑛=5 and the algorithm has already arranged the numbers 1,2,3 in the permutation. Consider how the generator will choose a position for the number 4:

The values of 𝑟 will be 𝑟=[3,3,3,4,×], where × means an indefinite value.
Then the 𝑐𝑜𝑢𝑛𝑡 values will be 𝑐𝑜𝑢𝑛𝑡=[0,0,3,1,0].
There are only two unoccupied positions in the permutation: 3 and 4. The value in the 𝑐𝑜𝑢𝑛𝑡 array for position 3 is 3, for position 4 it is 1.
The maximum value is reached only for position 3, so the algorithm will uniquely select this position for number 4.
Satisfied with his purchase, Denis went home. For several days without a break, he generated permutations. He believes that he can come up with random permutations no worse than a generator. After that, he wrote out the first permutation that came to mind 𝑝1,𝑝2,…,𝑝𝑛 and decided to find out if it could be obtained as a result of the generator.

Unfortunately, this task was too difficult for him, and he asked you for help. It is necessary to define whether the written permutation could be obtained using the described algorithm if the generator always selects the position Denis needs.

Input
The first line contains a single integer 𝑡 (1≤𝑡≤105) — the number of test cases. Then the descriptions of the test cases follow.

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

The second line of the test case contains 𝑛 different integers 𝑝1,𝑝2,…,𝑝𝑛 (1≤𝑝𝑖≤𝑛) — the permutation written by Denis.

It is guaranteed that the sum of 𝑛 over all test cases doesn’t exceed 105.

Output
Print “Yes” if this permutation could be obtained as a result of the generator. Otherwise, print “No”.

All letters can be displayed in any case.

Example
inputCopy
5
5
2 3 4 5 1
1
1
3
1 3 2
4
4 2 3 1
5
1 5 2 4 3
outputCopy
Yes
Yes
No
Yes
No
Note
Let’s simulate the operation of the generator in the first test.

At the 1 step, 𝑟=[1,2,3,4,5],𝑐𝑜𝑢𝑛𝑡=[1,1,1,1,1]. The maximum value is reached in any free position, so the generator can choose a random position from 1 to 5. In our example, it chose 5.

At the 2 step, 𝑟=[1,2,3,4,×],𝑐𝑜𝑢𝑛𝑡=[1,1,1,1,0]. The maximum value is reached in positions from 1 to 4, so the generator can choose a random position among them. In our example, it chose 1.

At the 3 step, 𝑟=[2,2,3,4,×],𝑐𝑜𝑢𝑛𝑡=[0,2,1,1,0]. The maximum value is 2 and is reached only at the 2 position, so the generator will choose this position.

At the 4 step, 𝑟=[3,3,3,4,×],𝑐𝑜𝑢𝑛𝑡=[0,0,3,1,0]. The maximum value is 3 and is reached only at the 3 position, so the generator will choose this position.

At the 5 step, 𝑟=[4,4,4,4,×],𝑐𝑜𝑢𝑛𝑡=[0,0,0,4,0]. The maximum value is 4 and is reached only at the 4 position, so the generator will choose this position.

In total, we got a permutation of 2,3,4,5,1, that is, a generator could generate it.

题意:
按照题意规则有一个 c n t cnt cnt数组,每次取其中最大值对应位置填数,按顺序填。问能否填出题目所给序列

思路:
一开始写的很暴力,直接模拟,set和线段树都用上了。
但是本题有个规律,假设你这次选定了第 i i i个位置,如果 i ! = n i!=n i!=n,那么下次就会是第 i + 1 i+1 i+1个位置。
如果 a [ i ] − a [ i − 1 ] ≥ 2 a[i]-a[i-1]≥2 a[i]a[i1]2,那么填完了第 i − 1 i-1 i1个数,再填第 i i i个就不对了。所以只需要排除这种情况就好了

#include <cstdio>
#include <algorithm>
#include <map>
#include <iostream>
#include <vector>
#include <deque>
 
using namespace std;
 
const int maxn = 1e5 + 7;
 
int a[maxn];
 
int main() {
    int T;scanf("%d",&T);
    while(T--) {
        int n;scanf("%d",&n);
        for(int i = 1;i <= n;i++) {
            scanf("%d",&a[i]);
        }
        int flag = 1;
        for(int i = 2;i <= n;i++) {
            if(a[i] - a[i - 1] >= 2) {
                flag = 0;break;
            }
        }
        if(flag) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值