Amidst skyscrapers in the bustling metropolis of Metro Manila, the newest Noiph mall in the Philippines has just been completed! The construction manager, Penchick, ordered a state-of-the-art monument to be built with n n n pillars.
The heights of the monument’s pillars can be represented as an array h h h of n n n positive integers, where h i h_i hi represents the height of the i i i-th pillar for all i i i between 1 1 1 and n n n.
Penchick wants the heights of the pillars to be in non-decreasing order, i.e. h i ≤ h i + 1 h_i \le h_{i + 1} hi≤hi+1 for all i i i between 1 1 1 and n − 1 n - 1 n−1. However, due to confusion, the monument was built such that the heights of the pillars are in non-increasing order instead, i.e. h i ≥ h i + 1 h_i \ge h_{i + 1} hi≥hi+1 for all i i i between 1 1 1 and n − 1 n - 1 n−1.
Luckily, Penchick can modify the monument and do the following operation on the pillars as many times as necessary:
Help Penchick determine the minimum number of operations needed to make the heights of the monument’s pillars non-decreasing.
Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1≤t≤1000). The description of the test cases follows.
The first line of each test case contains a single integer n n n ( 1 ≤ n ≤ 50 1 \leq n \leq 50 1≤n≤50) — the number of pillars.
The second line of each test case contains n n n integers h 1 , h 2 , … , h n h_1, h_2, \ldots, h_n h1,h2,…,hn ( 1 ≤ h i ≤ n 1 \le h_i \le n 1≤hi≤n and h i ≥ h i + 1 h_i\ge h_{i+1} hi≥hi+1) — the height of the pillars.
Please take note that the given array h h h is non-increasing.
Note that there are no constraints on the sum of n n n over all test cases.
For each test case, output a single integer representing the minimum number of operations needed to make the heights of the pillars non-decreasing.
3
5
5 4 3 2 1
3
2 2 1
1
1
4
1
0
In the first test case, the initial heights of pillars are h = [ 5 , 4 , 3 , 2 , 1 ] h = [5, 4, 3, 2, 1] h=[5,4,3,2,1].
After the operation, the heights of the pillars are h = [ 2 , 2 , 3 , 4 , 4 ] h = [2, 2, 3, 4, 4] h=[2,2,3,4,4], which is non-decreasing. It can be proven that it is not possible for Penchick to make the heights of the pillars non-decreasing in fewer than 4 4 4 operations.
In the second test case, Penchick can make the heights of the pillars non-decreasing by modifying the height of pillar 3 3 3 to h 3 : = 2 h_3 := 2 h3:=2.
In the third test case, the heights of pillars are already non-decreasing, so no operations are required.
// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <stack>//栈
// #include <deque>//堆/优先队列
// #include <queue>//队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const ll N=1e2+10;
ll a[N],f[N];
void solve()
{
ll n;
cin>>n;
for(ll i=1;i<=n;i++) cin>>a[i];
for(ll i=1;i<=n;i++)
{
f[i]=1;
for(ll j=1;j<i;j++)
{
if(a[j]<=a[i]) f[i]=max(f[i],f[j]+1);
}
}
ll res=0;
for(ll i=1;i<=n;i++) res=max(res,f[i]);
cout<<n-res<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
ll t;
cin>>t;
while(t--) solve();
return 0;
}
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- baijiahaobaidu.com 版权所有 湘ICP备2023023988号-9
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务