题意:
array array array
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2917 Accepted Submission(s): 1170
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 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
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.
题意是给你一个T组测试数据,然后输入一个n,k。(1<=n<=10^5, 0<=k<=n)。然后输入n个数,你可以任意选择n中的k个数删除,使得剩下的序列不是一个上升序列或者不是一个下降序列,如果满足则输出"A is a magic array.
”。注意这里是或者,也就是说即使删除k个元素后剩下的能组成一个上升序列,但不是一个下降序列,它也是满足条件的。只有都不是的才输出not。求出最长上升和最长下降的长度max1,max2,判断n-max<=k,则返回true
还有这里指的上升下降序列不是严格意义上的,如果两个数相等也是可以的,判断条件要加=号。
还有就是求最长下降子序列的长度的时候可以把原来序列反过来存储,求反序列的最长上升的,就是原序列最长下降的。
开始用的O(n²),结果超时了。后来用的O(nlogn),没超时。
O(n²)做法:
#include <stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
int n,k;
int arr[10010];
bool lis(int arr[], int len)
{
int longest[len];
for (int i=0; i<len; i++)
longest[i] = 1;
for (int j=1; j<len; j++) {
for (int i=0; i<j; i++) {
if (arr[j]>arr[i] && longest[j]<longest[i]+1){ //注意longest[j]<longest[i]+1这个条件,不能省略。
longest[j] = longest[i] + 1; //计算以arr[j]结尾的序列的最长递增子序列长度
}
}
}
int max = 0;
for (int j=0; j<len; j++)
if (longest[j] > max) max = longest[j]; //从longest[j]中找出最大值
//printf("max:%d\n",max);
if(k>=n-max) return true;
else return false;
}
bool lds(int arr[], int len)
{
int longest[len];
for (int i=0; i<len; i++)
longest[i] = 1;
for (int j=1; j<len; j++) {
for (int i=0; i<j; i++) {
if (arr[j]<arr[i] && longest[j]<longest[i]+1){ //注意longest[j]<longest[i]+1这个条件,不能省略。
longest[j] = longest[i] + 1; //计算以arr[j]结尾的序列的最长递增子序列长度
}
}
}
int max = 0;
for (int j=0; j<len; j++){
if (longest[j] > max) max = longest[j]; //从longest[j]中找出最大值
}
//printf("min:%d\n",max);
if(k>=n-max) return true;
else return false;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&k);
for(int i=0;i<n;i++)
scanf("%d",&arr[i]);
bool flag1=lis(arr,n);
bool flag2=lds(arr,n);
if(flag1==true||flag2==true) printf("A is a magic array.\n");
else printf("A is not a magic array.\n");
}
return 0;
}
O(nlogn)做法:arr[i]代表以a[i]这个数结尾的最长上升子序列的长度
#include <stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
int B[100010],len;
int n,k;
int arr[100010];
int brr[100010];
int BiSearch(int *b, int len, int w)
{
int left = 0, right = len - 1;
int mid;
while (left <= right)
{
mid = left + (right-left)/2;
if (b[mid] > w)
right = mid - 1;
else if (b[mid] < w)
left = mid + 1;
else //找到了该元素,则直接返回
return mid;
}
return left;//数组b中不存在该元素,则返回该元素应该插入的位置
}
bool LIS(int *array, int n)
{
len = 1;
B[0] = array[0];
int i, pos = 0;
for(i=1; i<n; ++i)
{
if(array[i] >= B[len-1]) //如果大于B中最大的元素,则直接插入到B数组末尾
{
B[len] = array[i];
++len;
}
else
{
pos = BiSearch(B, len, array[i]); //二分查找需要插入的位置
B[pos] = array[i];
}
}
if(k>=n-len) return true;
else return false;
}
int main()
{
int t;
int N;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&k);
N=n-1;
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
brr[N--]=arr[i];
}
bool flag1=LIS(arr,n);
bool flag2=LIS(brr,n);
if(flag1==true||flag2==true) printf("A is a magic array.\n");
else printf("A is not a magic array.\n");
}
return 0;
}