CF 1375C Element Extermination

You are given an array a of length n, which initially is a permutation of numbers from 1 to n. In one operation, you can choose an index i (1≤i<n) such that ai<ai+1, and remove either ai or ai+1 from the array (after the removal, the remaining parts are concatenated).

For example, if you have the array [1,3,2], you can choose i=1 (since a1=1<a2=3), then either remove a1 which gives the new array [3,2], or remove a2 which gives the new array [1,2].

Is it possible to make the length of this array equal to 1 with these operations?

Input
The first line contains a single integer t (1≤t≤2⋅104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n (2≤n≤3⋅105) — the length of the array.

The second line of each test case contains n integers a1, a2, …, an (1≤ai≤n, ai are pairwise distinct) — elements of the array.

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

Output
For each test case, output on a single line the word “YES” if it is possible to reduce the array to a single element using the aforementioned operation, or “NO” if it is impossible to do so.

Example
inputCopy
4
3
1 2 3
4
3 1 2 4
3
2 3 1
6
2 4 6 1 3 5
outputCopy
YES
YES
NO
YES


题意

有一个长度为 n n n 的序列,序列由 1 1 1 ~ n n n 组成,如果 a i < a i + 1 a_i < a_{i + 1} ai<ai+1 那么我可以选择删除 a i a_i ai 或者 a i + 1 a_{i + 1} ai+1,直到序列只剩一个元素。

思路

我们从头开始维护:

  1. 如果 a [ 1 ] < a [ 2 ] a[1] < a[2] a[1]<a[2] 那么我可以删除 a [ 2 ] a[2] a[2],留小的。
  2. 如果 a [ 1 ] > a [ 2 ] a[1] > a[2] a[1]>a[2] 那么我们没办法删除 a [ 2 ] a[2] a[2],那我们就继续往下维护,直到遇见 a [ i d x ] a[idx] a[idx] i d x idx idx 为第一个大于 a [ 1 ] a[1] a[1] 的下标, 1 1 1 ~ i d x − 1 idx-1 idx1 都可用 i d x idx idx 删除,因为 a [ i d x ] a[idx] a[idx] 是第一个大于 a [ 1 ] a[1] a[1] 的数。之后我们把 a i d x a_{idx} aidx a 1 a_1 a1 删除,因为此时能留下最小的只有 a [ 1 ] a[1] a[1]
  3. 2 2 2 为一组操作,操作后只剩下元素 a 1 a_1 a1,所以我们可以把一个序列进行多组 2 2 2 操作,最后 i d x idx idx 应该为 n n n

结论: a 1 < a n a_1 < a_n a1<an 时进行 2 2 2 操作后只剩下元素 a 1 a_1 a1

#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;

int main()
{
	int t; cin >> t;

	while (t--)
	{
		int n; cin >> n;
		vector<int> a(n);
		for (int i = 0; i < n; i++) cin >> a[i];
		puts(a[0] < a[n - 1] ? "YES" : "NO");
	} 

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值