Description
给出一个长度为 n n 的序列,如果删去其中的 k k 个数使得剩下的序列是不增或不减的,则称该序列是,否则不是,判断 a a 序列是否为
Input
第一行一整数 T T 表示用例组数,每组用例首先输入两个整数,之后输入 n n 个整数
(1≤T≤20,1≤n≤105,0≤k≤n,1≤ai≤105) ( 1 ≤ T ≤ 20 , 1 ≤ n ≤ 10 5 , 0 ≤ k ≤ n , 1 ≤ a i ≤ 10 5 )
Output
判断 a a 序列是否为
Sample Input
3
4 1
1 4 3 7
5 2
4 1 3 1 2
6 1
1 4 3 5 4 6
Sample Output
A is a magic array.
A is a magic array.
A is not a magic array.
Solution
判断 a a 序列的最长不增子序列长度和最长不减子序列长度是否大于等于即可,求最长不减子序列可以把 a a 序列全部变号即变成求序列最长不增子序列长度了
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100005;
int dp[maxn],T,n,k,a[maxn];
int LIS()
{
for(int i=1;i<n;i++)dp[i]=INF;
dp[0]=a[0];
int len=1;
for(int i=1;i<n;i++)
{
if(a[i]>=dp[len-1])dp[len++]=a[i];
else dp[upper_bound(dp,dp+n,a[i])-dp]=a[i];
}
return len;
}
int main()
{
scanf("%d",&T);
while(T--)
{
int flag=0;
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++)scanf("%d",&a[i]);
if(n-LIS()<=k)flag=1;
else
{
for(int i=0;i<n;i++)a[i]*=-1;
if(n-LIS()<=k)flag=1;
}
if(flag)printf("A is a magic array.\n");
else printf("A is not a magic array.\n");
}
return 0;
}