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?
Kiddo: "I have an array A
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: A
1
,A
2
…A
n![]()
.
1≤T≤20![]()
1≤n≤10
5![]()
![]()
0≤k≤n![]()
1≤A
i
≤10
5![]()
![]()
1≤T≤20
1≤n≤10
0≤k≤n
1≤A
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.题意:给出一个数列,如果去掉k个数字,剩下的数列是非递增或者是非递减的(刚开始理解错误了递减和等差,可怕),则是魔法数列,否则不是 思路:求出最长上升子序列,最长下降子序列,与n-k比较#include <cstdio> #include <cmath> #include <cstring> #include <iostream> #include <vector> #include <queue> using namespace std; const int MA=100009; int dp[MA],n; int bin(int len,int k) { int l = 1, r = len; while(l <= r) { int mid = (l+r)/2; if(k > dp[mid]) l = mid+1; else r = mid-1; } return l; } int LIS(int a[]) { int i,j,ans=1; dp[1] = a[1]; for(i = 2; i <= n; i++) { if(a[i] < dp[1])//如果比最小的还小 j = 1; else if(a[i] >= dp[ans])//如果比最大的还大 j = ++ans; else j = bin(ans,a[i]); dp[j] = a[i]; } return ans; } int main() { int t,k; int a[MA],b[MA]; scanf("%d",&t); while(t--) { scanf("%d %d",&n,&k); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); b[i]=-a[i]+100005; } int m1,m2; memset(dp,0,sizeof(dp)); m1=LIS(a); memset(dp,0,sizeof(dp)); m2=LIS(b); if(m1>=n-k||m2>=n-k) printf("A is a magic array.\n"); else printf("A is not a magic array.\n"); } return 0; }
本文介绍了一个有趣的编程问题:判断一个数列是否为“魔法数列”。通过移除k个元素后,若剩余数列呈非递增或非递减排列,则该数列为魔法数列。文章提供了使用最长上升子序列和最长下降子序列来解决此问题的方法。
&spm=1001.2101.3001.5002&articleId=77930723&d=1&t=3&u=7b2f4e93208547d7b8af660e313c04df)
404

被折叠的 条评论
为什么被折叠?



