Array Eversion

B. Array Eversion

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

You are given an array a of length n.
Let’s define the eversion operation. Let x=an. Then array a is partitioned into two parts: left and right. The left part contains the elements of a that are not greater than x (≤x). The right part contains the elements of a that are strictly greater than x (>x). The order of elements in each part is kept the same as before the operation, i. e. the partition is stable. Then the array is replaced with the concatenation of the left and the right parts.

For example, if the array a is [2,4,1,5,3], the eversion goes like this: [2,4,1,5,3]→[2,1,3],[4,5]→[2,1,3,4,5].

We start with the array a and perform eversions on this array. We can prove that after several eversions the array a stops changing. Output the minimum number k such that the array stops changing after k eversions.

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤100). Description of the test cases follows.

The first line contains a single integer n (1≤n≤2⋅105).

The second line contains n integers a1,a2,…,an (1≤ai≤109).

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output
For each test case print a single integer k — the number of eversions after which the array stops changing.

Example
input

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

output
1
2
0

Note
Consider the fist example.

The first eversion: a=[1,4,2,5,3], x=3. [2,4,1,5,3]→[2,1,3],[4,5]→[2,1,3,4,5].
The second and following eversions: a=[2,1,3,4,5], x=5. [2,1,3,4,5]→[2,1,3,4,5],[]→[2,1,3,4,5]. This eversion does not change the array, so the answer is 1.
Consider the second example.

The first eversion: a=[5,3,2,4,1], x=1. [5,3,2,4,1]→[1],[5,3,2,4]→[1,5,3,2,4].
The second eversion: a=[1,5,3,2,4], x=4. [1,5,3,2,4]→[1,3,2,4],[5]→[1,3,2,4,5].
The third and following eversions: a=[1,3,2,4,5], x=5. [1,3,2,4,5]→[1,3,2,4,5],[]→[1,3,2,4,5]. This eversion does not change the array, so the answer is 2.

题意: 给你一个数组(长度为n),每次令x=an,把小于等于x的放左边,大于x的放右边(两边各自的相对位置不变),数组顺序变为左右合并顺序,问几次操作后数组顺序不再变化

思路: 什么时候不再变化呢,当然是an=数组最大值(以下用maxx代替)时,那么题就转变为经过几次变化,an=maxx,即从后往前遍历,当an=maxx时,跳出结束,那经过几次变化应该如何计算呢,像5 4 3 2 1,x=1,变为1 5 4 3 2,sum++,x=2,变为1 2 5 4 3,sum++,以此类推,但像5 4 3 1 2,x=2,变为1 2 5 4 3,sum++,下次的x=3,而不是1,说明直接比较a[i]和maxx的值并sum++是不对的,sum的值和两者大小有关,当前值如果 大于(不能等于) 上次的x,那这个sum++是有效的,如果当前值小于或等于上次的x,上次移动时,当前值已经被带到左边,此时的sum++无效,此时思路就很清晰了

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[210000];
int main()
{
	int t,n,i,j,k;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		int maxx=-1,mx=-1;
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			maxx=max(maxx,a[i]);
		}
		int sum=0,p=a[n-1];
		for(i=n-1;i>=0;i--)
		{
			if(a[i]>p)
			{
				sum++;
				p=a[i];
			}
			if(p==maxx)
			break;
		}
		printf("%d\n",sum);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值