您的位置:首页 > 理论基础 > 计算机网络

HDU - 6197 array array array (2017 ACM-ICPC 亚洲区 (沈阳赛区) 网络赛 1004)

2017-09-10 22:48 447 查看


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≤105
0≤k≤n
1≤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.

 

Source

2017 ACM/ICPC Asia Regional Shenyang Online

 

题意:给你一串数字,问你能否去掉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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐