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 AA and a number kk, if you can choose exactly kk elements from AA and erase them, then the remaining array is in non-increasing order or non-decreasing order, we say AA is a magic array. Now I want you to tell me whether AA 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 nn and kk in one line, then one line with nnintegers: A1,A2…AnA1,A2…An.
1≤T≤201≤T≤20
1≤n≤1051≤n≤105
0≤k≤n0≤k≤n
1≤Ai≤1051≤Ai≤105
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减去这个长度得到a,在判断一下k是否大于a。如果大于则是一个魔法数组,反之,不是。
附上我们队的代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
const int inf=1<<30;
int b[maxn];
int lndp(int n,int *a)
{
for(int i=0;i<=n+1;i++)
b[i]=inf;
for(int i=0;i<n;i++)
{
int pos=upper_bound(b,b+i+1,a[i])-b;
b[pos]=a[i];
}
int ans;
for(ans=0;b[ans]!=inf;ans++);
return ans;
}
int main()
{
int a[maxn];
int tmp[maxn];
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]);
tmp[n-i-1]=a[i];
}
int cnt=max(lndp(n,a),lndp(n,tmp));
if(n-cnt>k)
printf("A is not a magic array.\n");
else
printf("A is a magic array.\n");
}
return 0;
}