• 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;
    }
    

      

  • 相关阅读:
    Android中的内部类引起的内存泄露
    Android的消息机制: Message/MessageQueue/Handler/Looper
    ArrayList/Vector的原理、线程安全和迭代Fail-Fast
    JVM中的Stack和Frame
    JVM中的垃圾收集算法和Heap分区简记
    无锁编程以及CAS
    简述Java内存模型的由来、概念及语义
    MQTT协议简记
    RabbitMQ的工作队列和路由
    RabbitMQ 入门
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/7517566.html
Copyright © 2020-2023  润新知