题面
AksLolCoding is playing a game on an array of positive integers. During each turn:
- If is non-decreasing, the game ends.
- Otherwise, AksLolCoding can choose any single element and remove it from the array.
Determine the minimum possible number of elements that can be remaining in the array after the game ends.
^{\text{∗}}$$a is non-decreasing if for all , where is the length of .
Input
The first line contains an integer (), the number of test cases.
The first line of each test case contains an integer ().
The second line of each test case contains integers, the elements of ().
Output
For each test case, output an integer: the minimum possible number of elements left once the array is sorted.
思路
这题的关键是注意游戏会在数组变成非递减时立刻结束,所以我们的目标不是尽快变有序,而是尽量拖到最后再有序,从而多删元素
- 如果一开始就满足 ,那么游戏直接结束,无法删除任何元素,所以答案是
- 否则数组一定存在相邻逆序对,即存在某个 使得 ,记 ,,此时
接下来我们可以一直删除除 之外的所有元素,因为删掉其它位置不会改变 与 的相对顺序,而且它们始终相邻并满足 ,因此数组始终不是非递减,游戏不会提前结束
此时:
- 当数组只剩下 时仍然不是非递减,还可以再删一次,删掉其中一个后数组长度变成
- 长度为 的数组必然是非递减,所以游戏结束,此时剩余元素最少为
综上答案只有两种情况,要么是 ,要么是 ,实现时只需检查原数组是否非递减即可
代码
#include <bits/stdc++.h>
using namespace std;
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];
bool flag = false; int prev = a[0]; for (int i = 1; i < n; i ++) { if (prev > a[i]) { flag = true; break; } prev = a[i]; }
if (flag) { cout << 1 << endl; } else cout << n << endl; }}