522 字
3 分钟
Deletion-Sort

题面#

AksLolCoding is playing a game on an array aa of nn positive integers. During each turn:

  • If aa is non-decreasing^{\text{∗}}, 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 aiai+1a_i\leq a_{i+1} for all 1im11\leq i\leq m-1, where mm is the length of aa.

Input

The first line contains an integer tt (1t10001 \leq t \leq 1000), the number of test cases.

The first line of each test case contains an integer nn (1n101 \leq n \leq 10).

The second line of each test case contains nn integers, the elements of aa (1ai1001 \leq a_i \leq 100).

Output

For each test case, output an integer: the minimum possible number of elements left once the array is sorted.

思路#

这题的关键是注意游戏会在数组变成非递减时立刻结束,所以我们的目标不是尽快变有序,而是尽量拖到最后再有序,从而多删元素

  • 如果一开始就满足 a1a2ana_1 \le a_2 \le \cdots \le a_n,那么游戏直接结束,无法删除任何元素,所以答案是 nn
  • 否则数组一定存在相邻逆序对,即存在某个 ii 使得 ai>ai+1a_i > a_{i+1},记 x=aix=a_iy=ai+1y=a_{i+1},此时 x>yx>y

接下来我们可以一直删除除 x,yx,y 之外的所有元素,因为删掉其它位置不会改变 xxyy 的相对顺序,而且它们始终相邻并满足 x>yx>y,因此数组始终不是非递减,游戏不会提前结束

此时:

  • 当数组只剩下 [x,y][x,y] 时仍然不是非递减,还可以再删一次,删掉其中一个后数组长度变成 11
  • 长度为 11 的数组必然是非递减,所以游戏结束,此时剩余元素最少为 11

综上答案只有两种情况,要么是 nn,要么是 11,实现时只需检查原数组是否非递减即可

代码#

#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;
}
}
Deletion-Sort
https://github.com/posts/deletion-sort/
作者
FZSGBall
发布于
2026-05-04
许可协议
CC BY-NC-SA 4.0