hdu 6197 2017 ACM/ICPC Asia Regional Shenyang Online array array array【最长不上升子序列和最长不下降子序列】

hdu 6197

题意:给定一个数组,问删掉k个字符后数组是否能不减或者不增,满足要求则是magic array,否则不是。

题解:队友想的思路,感觉非常棒!既然删掉k个后不增或者不减,那么就先求数组的最长不下降子序列的长度l1和最长不上升子序列的长度l2,若l1>=n-k||l2>=n-k,则满足要求。

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e5+10;
int s[maxn],a[maxn],b[maxn];
int n,k;

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>k;
        for(int i=0;i<n;i++){
            a[i]=b[i]=INF;
        }
        for(int i=0;i<n;i++){
            cin>>s[i];
            *upper_bound(a,a+n,s[i])=s[i];
        }
        for(int i=n-1;i>=0;i--){
            *upper_bound(b,b+n,s[i])=s[i];
        }
        int sz1=lower_bound(a,a+n,INF)-a;//最长不下降
        int sz2=lower_bound(b,b+n,INF)-b;//最长不上升
        if(sz1>=(n-k)||sz2>=(n-k))
            cout<<"A is a magic array."<<endl;
        else
            cout<<"A is not a magic array."<<endl;
    }
    return 0;
}

 

posted @ 2017-09-13 17:09  ╰追憶似水年華ぃ╮  阅读(169)  评论(0编辑  收藏  举报