题意:
告诉你n 个数, 问你是否去掉k 个数后, 原序列变成非严格递增序列或者非严格递减序列。
思路:
显然求一遍LIS , 倒过来在求一边LIS, 比较ans 和 k 的关系即可。
#include
#include
#include
using namespace std;
int T;
const int maxn = 100000 + 10;
int a[maxn], c[maxn];
int main(){
scanf("%d",&T);
while(T--){
int n, k;
scanf("%d %d",&n, &k);
for (int i = 0; i < n; ++i){
scanf("%d", a+i);
}
int cnt = 0;
int ans = 0;
for (int i = 0; i < n; ++i){
int p = upper_bound(c, c + cnt, a[i]) - c;
if (p == cnt){
c[cnt++] = a[i];
ans = max(ans, cnt);
}
else {
c[p] = a[i];
}
}
cnt = 0;
for (int i = n-1; i >= 0; --i){
int p = upper_bound(c, c + cnt, a[i]) - c;
if (p == cnt){
c[cnt++] = a[i];
ans = max(ans, cnt);
}
else {
c[p] = a[i];
}
}
// printf("ans = %d\n", ans);
if (n - k <= ans){
puts("A is a magic array.");
}
else {
puts("A is not a magic array.");
}
}
return 0;
}