A. Nastya and Strange Generator

目录

1.Problem

2.Input

3.Output

4.Examples

4.1input

4.2output

5.Code

6.Conclusion


1.Problem

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 nn steps. At the ii-th step, a place is chosen for the number ii (1≤i≤n)(1≤i≤n). The position for the number ii is defined as follows:

  • For all jj from 11 to nn, we calculate rjrj  — the minimum index such that j≤rj≤nj≤rj≤n, and the position rjrj is not yet occupied in the permutation. If there are no such positions, then we assume that the value of rjrj is not defined.
  • For all tt from 11 to nn, we calculate counttcountt  — the number of positions 1≤j≤n1≤j≤n such that rjrj is defined and rj=trj=t.
  • Consider the positions that are still not occupied by permutation and among those we consider the positions for which the value in the countcount array is maximum.
  • The generator selects one of these positions for the number ii. The generator can choose any position.

Let's have a look at the operation of the algorithm in the following example:

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

  • The values of rr will be r=[3,3,3,4,×]r=[3,3,3,4,×], where ×× means an indefinite value.
  • Then the countcount values will be count=[0,0,3,1,0]count=[0,0,3,1,0].
  • There are only two unoccupied positions in the permutation: 33 and 44. The value in the countcount array for position 33 is 33, for position 44 it is 11.
  • The maximum value is reached only for position 33, so the algorithm will uniquely select this position for number 44.

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 p1,p2,…,pnp1,p2,…,pn 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.

2.Input

The first line contains a single integer tt (1≤t≤105)(1≤t≤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 nn (1≤n≤105)(1≤n≤105)  — the size of the permutation.

The second line of the test case contains nn different integers p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n)  — the permutation written by Denis.

It is guaranteed that the sum of nn over all test cases doesn't exceed 105105.

3.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.

4.Examples

4.1input

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

4.2output

Yes
Yes
No
Yes
No

5.Code

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

#define pb push_back
#define mp make_pair
#define fi first
#define se second

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template <typename T> bool chkmax(T &x, T y) { return (x < y) ? (x = y, true) : false; }
template <typename T> bool chkmin(T &x, T y) { return (x > y) ? (x = y, true) : false; }

int readint() {
    int x = 0, f = 1; 
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

int main() {
    int T = readint();
    while (T--) {
        int n = readint();
        vector<int> a(n + 1), pl(n + 1);

        for (int i = 1; i <= n; i++) {
            a[i] = readint();
            pl[a[i]] = i;
        }

        bool fl = false, can = true;
        int maxa = n, st = 0;

        for (int i = 1; i <= n; i++) {
            if (fl && pl[i] != pl[i - 1] + 1) {
                can = false;
            }

            if (!fl) {
                fl = true;
                st = pl[i];
            }

            if (pl[i] == maxa) {
                fl = false;
                maxa = st - 1;
            }
        }

        printf(can ? "Yes\n" : "No\n");
    }

    return 0;
}

6.Conclusion

这段代码是一个C++程序,其主要功能是处理多个测试用例。每个测试用例的格式如下:

  1. 读取一个整数 T,表示测试用例的数量。
  2. 对于每个测试用例,读取一个整数 n,表示数组的大小。
  3. 读取一个包含 n 个整数的数组 a。
  4. 程序会根据数组 a 中元素的排列情况,判断是否存在一种操作可以使得数组变得有序,具体而言是检查是否存在一种顺序排列(升序)的子序列。

具体实现细节如下:

  • 通过 readint 函数读取输入的整数。
  • 使用 vector<int> 数据结构存储数组 a 和另一个数组 pl,其中 pl 记录了数组元素在原数组中的位置。
  • 通过迭代数组 a,检查是否存在一种操作顺序,使得数组变得有序。
  • 输出 "Yes" 如果存在这样的操作顺序,否则输出 "No"。

总体而言,这段代码是为了解决一个数组排序的问题,它通过检查数组元素在原数组中的位置来判断是否存在一种操作可以使得整个数组变得有序。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

向阳而生__

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

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

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

打赏作者

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

抵扣说明:

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

余额充值