Being upset after this behavior of Nastya, Denis was very sad. Nothing could make the rejected guy happier. To at least somehow have fun, he decided to walk through the gateways. And, luck smiled at him! When he entered the first courtyard, he met a strange a man who was selling something.
Looking around, Denis went to the stranger and bought a mysterious product. It was... Random permutation generator! That is what the boy has been looking for so long!
When he arrived home, he began to study how his generator works and learned the algorithm. The process of generating a permutation consists of n steps. At the i-th step, a place is chosen for the number i (1≤i≤n). The position for the number i is defined as follows
1.For all j from 1 to n, we calculate rj — the minimum index such that j≤rj≤n, and the position rj is not yet occupied in the permutation. If there are no such positions, then we assume that the value of rj is not defined.
2.For all t from 1 to n, we calculate count t — the number of positions 1≤j≤n such that rj is defined and rj=t.
3.Consider the positions that are still not taken up by permutation and among those we consider the positions for which the value in the count array is maximum.
4.The generator selects one of these positions for the number i. The generator can choose any position.
英语水品太差了,导致看了半天才看懂题,所以打完试着渣翻了一下QAQ:
Denis 被 Nastya 这种行为深深的伤害了(指B题的peaks),这使得他十分沮丧。没有东西可以让这个被拒绝的大兄弟再开心起来了。为了至少找点乐子,他决定穿过gateways。在此时,幸运之神此时眷顾了他,当进入一号庭院时,他遇见了个奇怪的商人。
Denis环顾四周,去这个奇怪的商人那里买了一件神秘的商品。这是。。。随机排列生成器!这可是他长久以来所渴望得到的东西!
当他到家后,开始研究生成器如何去工作,并学习了算法。生成一个排列的过程包括了 n 步。在第i步时,一个位置会被数字i所填充占用,这个位置也随之获得定义。
1.对于每个j(1<=j<=n),我们通过在已生成的排列的区间[j,n]中寻找到一个没有被占用位置的下标作为rj的值。如果找不到这样一个位置,那么我们就假设rj的值没有定义。
2.对于每个t(1<=t<=n),我们通过计算rj中有多少个值被定义为t的位置数量作为count t的值。
3.只考虑那些没有没占用排列所占用的位置,在这些位置中我们选取其中count t值最大的那些位置。
4.生成器选择count t最大的位置的一个位置填充上数字i.
之后给你一个长度为n的排列,问能否由上述规则生成这个排列。
通过上述规律,我们发现每次填入一个数,就要从这个位置向右递增1地一直填数,直到这个位置的rj没有定义。
这样只需要检验每个连续递增子序列是否相邻差值为1即可
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<int>a(n + 1);
a[0] = 1e9;
int l = 0;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
if (a[i] > a[i - 1])
{
if (a[i] - a[i - 1] > 1) { cout << "No" << endl; l = n - i; break; }
}
if (i == n)
cout << "Yes" << endl;
}
int x;
while (l--)cin >> x;
}
return 0;
}