hdu6197 array array array

題目:

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: A1,A2…An.
1≤T≤20
1≤n≤10^5
0≤k≤n
1≤Ai≤10^5
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.

題目是說給你n個(gè)數(shù),問能否刪除其中的k個(gè)數(shù),使得剩余的數(shù)組成的序列是非遞減或者非遞增的序列(也就是遞增或者遞減)。
其實(shí)遞增情況就是求最長上升子序列,因?yàn)橛每傞L度減去最長上升子序列的長度后,剩余長度如果小于等于k,那么總有一種可行的情況:把除了最長遞增子序列之外的數(shù)刪除就行。而遞減情況就是求最長下降子序列(反向求最長上升子序列)。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 1e5+10;
const int INF = 1e9+7;

int n;
int k;
int a[N];
int dp[N];

void init() {
    memset(a, 0, sizeof(a));
}

int LIS() {
    fill(dp, dp + n, INF);
    for (int i = 0;i < n;++i) {
        *lower_bound(dp, dp + n, a[i]) = a[i];
    }

    return lower_bound(dp, dp + n, INF) - dp;
}

int LDS() {
    fill(dp, dp + n, INF);
    for (int i = n-1;i >= 0;--i) {
        *lower_bound(dp, dp + n, a[i]) = a[i];
    }

    return lower_bound(dp, dp + n, INF) - dp;
}

int main() {
    int t;
    scanf("%d", &t);

    while (t--) {
        init();
        scanf("%d%d", &n, &k);
    
        for (int i = 0;i < n;++i) {
            scanf("%d", &a[i]);
        }

        int res1 = LIS();
        if (n - res1 <= k) {
            printf("A is a magic array.\n");
            continue;
        }
        
        int res2 = LDS();
        if (n - res2 <= k) {
            printf("A is a magic array.\n");
            continue;
        }

        printf("A is not a magic array.\n");
    }
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容