Title link: https://acm.zcmu.edu.cn/JudgeOnline/problem.php?id=1517
Topic
For an array of length n, you can select a number ai each time, and then ai will switch positions from the current position. If the number on the right is smaller than him, then switch positions, otherwise stop moving.
Ask at least a few points to move to make the final sequence ascending.
Ideas
The first criterion is whether there is a number smaller than him in the back, and then each number can only move backward or not, so the number smaller than him will not run to his left and will not change.
Then the selected numbers have one thing in common, that is, if there is a smaller number later, you can preprocess the minimum value of x to n, and then compare the current value with it.
ac code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 1e6 + 5;
int a[maxn], b[maxn];
int main(){
int t; cin >> t;
while(t --){
int n; cin >> n;
for(int i = 1; i <= n; i ++) cin >> a[i];
b[n + 1] = 0x3f3f3f3f;
for(int i = n; i >= 1; i --) b[i] = min(b[i + 1], a[i]);
int ans = 0;
for(int i = 1; i <= n; i ++){
if(a[i] > b[i]) ans ++;
}
cout << ans << endl;
}
return 0;
}