HDU6197 array array array 最长上升子序列(模板题)

题目链接:点击打开链接

题目思路:求出LIS和LDS(最长下降子序列,自己编的名字:),如果 LIS+k>=n或者LDS+k>=n 则输出 A is a magic array. 否则输出A is not a magic array.

题目的意思有点绕,其实就是如果去掉k个元素使得剩余的不是递增序列或者不是递减序列  等价于  去掉k个元素后剩余的元素是递减序列或者递增序列。这样就懂了吧。

AC代码:

/*
2017年9月10日20:44:38
HDU6197
最长上升子序列 签到 
AC 
*/ 
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector> 
using namespace std;

const int maxn = 100010;
int n;
int a[maxn],b[maxn];
int dp[maxn];

int lis(int *a)
{
    memset(dp, 0, sizeof(int)*n);
    int len = 1;
    dp[0] = a[0];
    for (int i = 1; i < n; ++i)
    {
        int pos = lower_bound(dp, dp + len, a[i]) - dp;
        dp[pos] = a[i];
        len = max(len, pos + 1);
    }
    return len;
}

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int k;
		scanf("%d%d",&n,&k);
		//正着存 
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		//倒着存,求一遍即为最长递减序列 
		for(int i=0;i<n;i++){
			b[i]=a[n-1-i];
		}
		if(lis(a)+k>=n||lis(b)+k>=n) printf("A is a magic array.\n");
		else printf("A is not a magic array.\n");
	} 
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值