array array array题解

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;
 }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值