array array array (2017 ACM/ICPC Asia Regional Shenyang Online 1004)简单DP,最长公共子序列(Ologn)

题意:

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?
 

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,A2An .
1T20
1n105
0kn
1Ai105
 

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;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值