题目:
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≤10^5
0≤k≤n
1≤Ai≤10^5
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个数,问能否删除其中的k个数,使得剩余的数组成的序列是非递减或者非递增的序列(也就是递增或者递减)。
其实递增情况就是求最长上升子序列,因为用总长度减去最长上升子序列的长度后,剩余长度如果小于等于k,那么总有一种可行的情况:把除了最长递增子序列之外的数删除就行。而递减情况就是求最长下降子序列(反向求最长上升子序列)。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 1e5+10;
const int INF = 1e9+7;
int n;
int k;
int a[N];
int dp[N];
void init() {
memset(a, 0, sizeof(a));
}
int LIS() {
fill(dp, dp + n, INF);
for (int i = 0;i < n;++i) {
*lower_bound(dp, dp + n, a[i]) = a[i];
}
return lower_bound(dp, dp + n, INF) - dp;
}
int LDS() {
fill(dp, dp + n, INF);
for (int i = n-1;i >= 0;--i) {
*lower_bound(dp, dp + n, a[i]) = a[i];
}
return lower_bound(dp, dp + n, INF) - dp;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
init();
scanf("%d%d", &n, &k);
for (int i = 0;i < n;++i) {
scanf("%d", &a[i]);
}
int res1 = LIS();
if (n - res1 <= k) {
printf("A is a magic array.\n");
continue;
}
int res2 = LDS();
if (n - res2 <= k) {
printf("A is a magic array.\n");
continue;
}
printf("A is not a magic array.\n");
}
return 0;
}