题目传送
题意:
给你一个数组,让你删除最少的前缀,使得删除后的数组,在只能把第一个元素或者最后一个元素添加到另外一个空数组情况下使得空数组添加后得到的数组递增排序。
思路:
我们先想如何能在删除前缀后,使得剩下的元素能满足在只添加第一个元素,或者最后一个元素的情况下使得另外那个添加的数组递增。
那么只有一种情况是可能的,就是这个数组是直接先递增再递减的的这种,但是我们现在的删除前缀是不确定的,所以我们直接从后面开始枚举,先遍历到从后往前递减序列,一直遍历到不成立的那个点,然后再往前遍历递增的序列,再遍历到不成立,然后这个序列就是最长的,也就是删除最少的
AC代码
#include <bits/stdc++.h>
inline long long read(){char c = getchar();long long x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 2e5 + 10;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const int mod = 1e9+7;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int t,n;
cin >> t;
while(t--)
{
cin >> n;
int arr[n+5] = {0},r = n;
for(int i = 1;i <= n;i++) cin >> arr[i];
while(1)
{
if(r-1 > 0)
{
if(arr[r] < arr[r-1] || arr[r] == arr[r-1])
r--;
else
break;
}
else
break;
}
while(1)
{
if(r-1 > 0)
{
if(arr[r] > arr[r-1] || arr[r] == arr[r-1])
r--;
else
break;
}
else
break;
}
cout << r-1 << endl;
}
}