HDU-5532(LIS-nlogn)

本文提供两种方法来解决一个特定的问题:确定一个整数数组是否通过移除一个元素即可变为升序或降序排列。方法一利用数组元素间的比较关系进行高效判断;方法二则寻找最长非递减或非递增子序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


 

思路:

解法一:

新的认识get+1,对于一个数组,可以通过记录他'<'和'>'来判断数组的升降序状态,这种方法只需要n的复杂度就可以解决问题,需要注意的一点是,每次删除一个结点在消失两个关系的同时也会出现一个新的关系

解法二:找到非递减和非递增LIS中数量较大的一个,只要它大于等于n-1,答案就是YES,不然就是NO,由于卡时间,故要用nlogn算法


 

方法一:
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; int T; int n,num[100007]; int big,small; int flag; int judge(int x) { int tsmall = small; int tbig = big; if(x == 1) { if(num[x] < num[x+1]) tsmall--; if(num[x] > num[x+1]) tbig--; if(!tbig||!tsmall) return 1; else return 0; } else if(x == n) { if(num[x-1] < num[x]) tsmall--; if(num[x-1] > num[x]) tbig--; if(!tbig||!tsmall) return 1; else return 0; } else { if(num[x-1]>num[x]&&num[x]>num[x+1]) tbig -= 2; if(num[x-1]<num[x]&&num[x]<num[x+1]) tsmall -= 2; if(num[x-1]>num[x]&&num[x]<num[x+1]||num[x-1]<num[x]&&num[x]>num[x+1]) { tbig --; tsmall --; } if(num[x-1] < num[x+1]) tsmall++; if(num[x-1] > num[x+1]) tbig++; if(!tbig||!tsmall) returl 1; else return 0; } } int main() { scanf("%d",&T); while(T--) { big = small = 0; flag = 0; scanf("%d",&n); for(int i = 1;i <= n;i++) { scanf("%d",&num[i]); if(i >= 2) { if(num[i] > num[i-1]) small++; if(num[i] < num[i-1]) big++; } } if(n==2||!big||!small) { printf("YES\n"); continue; } else { for(int i = 1;i <= n;i++) if(judge(i)){ flag = 1; break; } if(flag) printf("YES\n"); else printf("NO\n"); } } return 0; }

方法二:

#include <iostream>
#include <cstdio>
#include <cstring>
#define INF 0x7fffffff
using namespace std;

int T;
int n,num[100007];
int dp1[100007],dp2[100007];
int B1[100007],B2[100007];
int flag;

int max(int a,int b) {
    return a>b?a:b;
}

int find(int* B,int goal,int r)
{
    int l = 1;
    while(l <= r)
    {
        int mid = (l+r)/2;
        if(B[mid] < goal)
            l = mid+1;
        else if(B[mid] > goal) 
            r = mid-1;
        else return mid;
    }
    return l;
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        int len1,len2;
        int ans1 = 0,ans = 0,ans2 = 0;
        scanf("%d",&n);
        for(int i = 1;i <= n;i++) {
            scanf("%d",&num[i]);
            dp1[i] = dp2[i] = 1;
        }
        B1[1] = B2[1] = num[1];
        len1 = len2 = 1;
        for(int i = 2;i <= n;i++) {
            if(num[i] >= B1[len1]) B1[++len1] = num[i];
            else {
                int pos = find(B1,num[i],len1);
                B1[pos] = num[i];
            }
            if(num[i] <= B2[len2]) B2[++len2] = num[i];
            else {
                int pos = find(B2,num[i],len2);
                B2[pos] = num[i];
            }
        }
        ans = max(len1,len2);
        if(ans >= n-1) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

 

 

Problem Description
We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, selection sort, bubble sort, etc. But sometimes it is an overkill to use these algorithms for an almost sorted array.

We say an array is sorted if its elements are in non-decreasing order or non-increasing order. We say an array is almost sorted if we can remove exactly one element from it, and the remaining array is sorted. Now you are given an array a1,a2,,an, is it almost sorted?
 

 

Input
The first line contains an integer T indicating the total number of test cases. Each test case starts with an integer n in one line, then one line with n integers a1,a2,,an.

1T2000
2n105
1ai105
There are at most 20 test cases with n>1000.
 

 

Output
For each test case, please output "`YES`" if it is almost sorted. Otherwise, output "`NO`" (both without quotes).
 

 

Sample Input
3 3 2 1 7 3 3 2 1 5 3 1 4 1 5
 

 

Sample Output
YES YES NO

转载于:https://www.cnblogs.com/immortal-worm/p/4960153.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值