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

来源:互联网 发布:ubuntu 16.10安装lamp 编辑:程序博客网 时间:2024/06/07 04:42

题目链接:点击打开链接

题目思路:求出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:38HDU6197最长上升子序列 签到 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