HDU 6197 array array array(LIS)

17 篇文章 0 订阅

Description

给出一个长度为 n n 的序列a1,...,an,如果删去其中的 k k 个数使得剩下的序列是不增或不减的,则称该序列是magic array,否则不是,判断 a a 序列是否为magic array

Input

第一行一整数 T T 表示用例组数,每组用例首先输入两个整数n,k,之后输入 n n 个整数a1,...,an

(1T20,1n105,0kn,1ai105) ( 1 ≤ T ≤ 20 , 1 ≤ n ≤ 10 5 , 0 ≤ k ≤ n , 1 ≤ a i ≤ 10 5 )

Output

判断 a a 序列是否为magic array

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 序列的最长不增子序列长度和最长不减子序列长度是否大于等于nk即可,求最长不减子序列可以把 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值