HDU - array array array(LIS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6197
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Problem Description

One day, Kaitou Kiddo had stolen a priceless diamond ring. But detective Conan blocked Kiddo's path to escape from the museum. But Kiddo didn't want to give it back. So, Kiddo asked Conan a question. If Conan could give a right answer, Kiddo would return the ring to the museum. 
Kiddo: "I have an array A and a number k, if you can choose exactly k elements from A and erase them, then the remaining array is in non-increasing order or non-decreasing order, we say A is a magic array. Now I want you to tell me whether A is a magic array. " Conan: "emmmmm..." Now, Conan seems to be in trouble, can you help him?

Input

The first line contains an integer T indicating the total number of test cases. Each test case starts with two integers n and k in one line, then one line with n integers: HDU - array array array(LIS)

HDU - array array array(LIS)

Output

For each test case, please output "A is a magic array." if it is a magic array. Otherwise, output "A is not a magic array." (without quotes).

Sample Input

3
4 1
1 4 3 7
5 2
4 1 3 1 2
6 1
1 4 3 5 4 6

Sample Output

A is a magic array.
A is a magic array.
A is not a magic array.

Problem solving report:

Description: 给了一个序列,长度为n,问是否可以删去k个字符,使之成为非递增或者非递减子序列。
Problem solving: 因为是非递增或者非递减,所以我们需要正反都要求一次,选其中最大的,然后判断长度是否大于等于n-k就行了。

Accepted Code:

/* 
 * @Author: lzyws739307453 
 * @Language: C++ 
 */
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
int spt[MAXN], dp[MAXN];
int slove(int p, int l, int r) {
    int mid;
    while (l <= r) {
        mid = l + r >> 1;
        if (p >= dp[mid])
            l = mid + 1;
        else r = mid - 1;
    }
    return l;
}
int LIS(int sp[], int n) {
    int len = 0;
    dp[len++] = sp[0];
    for (int i = 1; i < n; i++) {
        if (sp[i] >= dp[len - 1])
            dp[len++] = sp[i];
        else {
            int p = slove(sp[i], 0, len - 1);
            dp[p] = sp[i];
        }
    }
    return len;
}
int main() {
    int t, n, k;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n, &k);
        for (int i = 0; i < n; i++)
            scanf("%d", &spt[i]);
        int len = LIS(spt, n);
        //printf("%d\n", len);
        reverse(spt, spt + n);
        len = max(len, LIS(spt, n));
        //printf("%d\n", len);
        if (len >= n - k)
            printf("A is a magic array.\n");
        else printf("A is not a magic array.\n");
    }
    return 0;
}
上一篇:hdu 1078(记忆化搜索)


下一篇:Codeforces Round #635 (Div. 2) E. Kaavi and Magic Spell