Sequence in the Pocket(2019浙江省省赛)(找规律)

Sequence in the Pocket(2019浙江省省赛)(找规律)

Time Limit: 2000 ms
Memory Limit: 65536 KB
judge:
ZOJ
vjudge

Description

DreamGrid has just found an integer sequence a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an in his right pocket. As DreamGrid is bored, he decides to play with the sequence. He can perform the following operation any number of times (including zero time): select an element and move it to the beginning of the sequence.

What’s the minimum number of operations needed to make the sequence non-decreasing?

Input

There are multiple test cases. The first line of the input contains an integer T T T, indicating the number of test cases. For each test case:

The first line contains an integer n n n ( 1 ≤ n ≤ 1 0 5 1 \le n \le 10^5 1n105), indicating the length of the sequence.

The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109), indicating the given sequence.

It’s guaranteed that the sum of n n n of all test cases will not exceed 1 0 6 10^6 106.

Output

For each test case output one line containing one integer, indicating the answer.

Sample Input

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

Sample Output

2
0

Hint

For the first sample test case, move the 3rd element to the front (so the sequence become {2, 1, 3, 4}), then move the 2nd element to the front (so the sequence become {1, 2, 3, 4}). Now the sequence is non-decreasing.

For the second sample test case, as the sequence is already sorted, no operation is needed.

题意

给你一个序列,你可以选择一个数字然后他会被放到序列的最前面,你可以执行任意次此操作,问你把这个序列变为非递减的序列最少需要多少步操作。

题解

加入有一个序列:
在这里插入图片描述
那么它变为非递减的序列后应该是这样子:
在这里插入图片描述
想一下中间哪些数字没有被操作?

应该是这几个:
在这里插入图片描述
因为他们没必要被操作。

仔细观察发现这三个数从右往左看,是依次递减的,而且他们的相对位置在最终的序列中也是这个样子。那就不难想到答案就是序列的长度减去已经排好位置的最大的数字的个数。

代码

#include <bits/stdc++.h>
#define maxn 100005
#define _for(i, a) for(int i = 0; i < (a); ++i)
#define sc(x) scanf("%d", &x)
using namespace std;

int T, n, a[maxn], ans, b[maxn];

void sol() {
	ans = 0;
	_for(i, n) sc(a[i]), b[i] = a[i];
	sort(b, b + n);
	int p = n - 1;
	for (int i = n - 1; i >= 0; --i) if (a[i] == b[p]) --p;
	printf("%d\n", p + 1);
}

int main() {
	while (cin >> T) {
		_for(i, T) {
			sc(n);
			sol();
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值