• hdu 6197 array array array


    https://vjudge.net/contest/184514#problem/D

    题意:

    给出一个数组和一个数字k,问去掉k个数字之后这个数列能否非递增或者非递减。

    思路:

    智障了,这种应该一眼就看出来嘛,还去求逆序数。。。求一遍最长不下降子序列和最长不上升子序列,只要数组的长度减去其中任何一个小于等于k的话,那么就说明去掉k个数就肯定满足题中的一个条件。

    nlogn的LIS。

    代码:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 int a[100005];
     7 int b[100005];
     8 int c[100005];
     9 
    10 int main()
    11 {
    12     int t;
    13 
    14     scanf("%d",&t);
    15 
    16     while (t--)
    17     {
    18         int n,k;
    19 
    20         scanf("%d%d",&n,&k);
    21 
    22         for (int i = 0;i < n;i++)
    23         {
    24             scanf("%d",&a[i]);
    25         }
    26 
    27         b[0] = a[0];
    28 
    29         int cnt1 = 1;
    30 
    31         for (int i = 1;i <= n;i++)
    32         {
    33             if (a[i] > b[cnt1-1]) b[cnt1++] = a[i];
    34             else
    35             {
    36                 int pos = lower_bound(b,b+cnt1,a[i]) - b;
    37                 b[pos] = a[i];
    38             }
    39         }
    40 
    41         int cnt2 = 1;
    42 
    43         for (int i = 0;i < n;i++)
    44         {
    45             b[n - i - 1] = a[i];
    46         }
    47 
    48         c[0] = b[0];
    49 
    50         for (int i = 1;i < n;i++)
    51         {
    52             if (b[i] > c[cnt2-1]) c[cnt2++] = b[i];
    53             else
    54             {
    55                 int pos = lower_bound(c,c+cnt2,b[i]) - c;
    56                 c[pos] = b[i];
    57             }
    58         }
    59 
    60         //printf("%d %d
    ",cnt1,cnt2);
    61 
    62         if (n - cnt1 <= k || n - cnt2 <= k) printf("A is a magic array.
    ");
    63         else printf("A is not a magic array.
    ");
    64     }
    65 
    66     return 0;
    67 }
  • 相关阅读:
    6.数字三角形
    5.分组背包问题
    1.商品模块表结构分析
    AUTH_USER_MODEL refers to model 'user.User' that has not been installed
    发布品论接口
    查询指定课程评论接口
    1.评论模块表结构
    上传视频课程到七牛云后存储到django后端接口
    5.上传视频到七牛云django端实现
    4.七牛云上传前台页面
  • 原文地址:https://www.cnblogs.com/kickit/p/7507594.html
Copyright © 2020-2023  润新知