HDU 6197 array array array nlogn求最长子序列 思维

  题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6197

  题目描述: 给一个长度为N的串, 问你能不能去掉K个数成为单调的串

  解题思路: 很明显的求最长上升或者下降子序列的题, 但是N <= 1e6 , 而且我们只要长度, 所以我们就用nlogn 来求......

  代码: 

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iterator>
#include <cmath>
#include <algorithm>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <queue>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define sca(x) scanf("%d",&x)
#define de printf("=======
")
typedef long long ll;
using namespace std;

const int maxn = 1e5+100;
int a[maxn];
int b[maxn];
int ans[maxn];

int main() {
    int t;
    sca(t);
    while( t-- ) {
        int n, k;
        scanf( "%d%d", &n, &k );
        for( int i = 0; i < n; i++ ) {
            scanf( "%d", a+i );
            b[n-i-1] = a[i];
        }
        ans[0] = a[0];
        int cnt = 0;
        for( int i = 1; i < n; i++ ) {
            if( a[i] >= ans[cnt] ) {
                ans[++cnt] = a[i];
            }
            else {
                *(upper_bound(ans, ans+cnt, a[i] )) = a[i];
            }
        }
        int ans1 = cnt+1;
        cnt = 0;
        for( int i = 1; i < n; i++ ) {
            if( b[i] >= ans[cnt] ) {
                ans[++cnt] = b[i];
            }
            else {
                *(upper_bound(ans, ans+cnt, b[i] )) = b[i];
            }
        }
        int ans2 = cnt+1;
        if( max(ans1, ans2) + k >= n ) {
            printf( "A is a magic array.
" );
        }
        else {
            printf( "A is not a magic array.
" );
        }
    }
    return 0;
}
View Code

  思考: 这个题以前必然做过啊.....印象非常深, 所以感觉还好........

原文地址:https://www.cnblogs.com/FriskyPuppy/p/7503396.html