HDU

题意:对于一个序列,要求去掉正好K个数字,若能使其成为不上升子序列或不下降子序列,则“A is a magic array.”,否则"A is not a magic array. "。

分析:

1、求一遍LCS,然后在将序列逆转,求一遍LCS,分别可得最长上升子序列和最长下降子序列的长度tmp1、tmp2。

2、n - tmp1 <= k或n - tmp2 <= k即可,需要去掉的去完之后,在已经是最长上升或最长下降的序列中随便去够k个就好了。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN], dp[MAXN];
int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        int n, k;
        scanf("%d%d", &n, &k);
        for(int i = 0; i < n; ++i){
            scanf("%d", &a[i]);
        }
        memset(dp, INT_INF, sizeof dp);
        for(int i = 0; i < n; ++i){
            *lower_bound(dp, dp + n, a[i]) = a[i];
        }
        int tmp1 = lower_bound(dp, dp + n, INT_INF) - dp;
        if(n - tmp1 <= k){
            printf("A is a magic array.
");
        }
        else{
            reverse(a, a + n);
            memset(dp, INT_INF, sizeof dp);
            for(int i = 0; i < n; ++i){
                *lower_bound(dp, dp + n, a[i]) = a[i];
            }
            int tmp2 = lower_bound(dp, dp + n, INT_INF) - dp;
            if(n - tmp2 <= k){
                printf("A is a magic array.
");
            }
            else{
                printf("A is not a magic array.
");
            }
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/7517566.html