1. 程式人生 > >6197 array array array (2017 ACM-ICPC 亞洲區 (瀋陽賽區) 網路賽 1004)

6197 array array array (2017 ACM-ICPC 亞洲區 (瀋陽賽區) 網路賽 1004)

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,A2An.
1T20
1n105
0kn
1Ai105

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.
Source

題意:給你一串數字,問你能否去掉k個數後,剩下的數列是遞增的或遞減的。

解題思路:nlogn求最長上升子序列和最長下降子序列即可。

#include<iostream>
#include<deque>
#include<memory.h>
#include<stdio.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
#define INF 1<<29
#define MAXN 100005
using namespace std;

int A[100005];
int B[100005];
int dp[100005];

int main() {

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

    int n,k;


    while(t--){
        scanf("%d%d",&n,&k);

        for(int i=0;i<n;i++){
            scanf("%d",&A[i]);
            B[i]=-A[i];//反過來
        }

        fill(dp,dp+MAXN,INF);
        for(int i = 0;i < n;i++){
            *upper_bound(dp,dp+n,A[i]) = A[i];
        }

        int iii=(lower_bound(dp,dp+n,INF)-dp);//最長上升子序列長度

        int inum=n-iii;//可以去掉多少個數

        fill(dp,dp+MAXN,INF);
        for(int i = 0;i < n;i++){
            *upper_bound(dp,dp+n,B[i]) = B[i];
        }

        int ddd=(lower_bound(dp,dp+n,INF)-dp);//最長下降子序列長度


        int dnum=n-ddd;//可以去掉多少個數

        if(inum<=k||dnum<=k||n==k)//空串也算遞增或下降
            printf("A is a magic array.\n");
        else
            printf("A is not a magic array.\n");

    }

    return 0;
}