HDU-5532(LIS-nlogn)

时间:2022-01-11 04:58:12

HDU-5532(LIS-nlogn)

HDU-5532(LIS-nlogn)


思路:

解法一:

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

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


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

方法二:

#include <iostream>
#include <cstdio>
#include <cstring>
#define INF 0x7fffffff
using namespace std; int T;
int n,num[];
int dp1[],dp2[];
int B1[],B2[];
int flag; int max(int a,int b) {
return a>b?a:b;
} int find(int* B,int goal,int r)
{
int l = ;
while(l <= r)
{
int mid = (l+r)/;
if(B[mid] < goal)
l = mid+;
else if(B[mid] > goal)
r = mid-;
else return mid;
}
return l;
} int main()
{
scanf("%d",&T);
while(T--)
{
int len1,len2;
int ans1 = ,ans = ,ans2 = ;
scanf("%d",&n);
for(int i = ;i <= n;i++) {
scanf("%d",&num[i]);
dp1[i] = dp2[i] = ;
}
B1[] = B2[] = num[];
len1 = len2 = ;
for(int i = ;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-) printf("YES\n");
else printf("NO\n");
}
return ;
}
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.

1≤T≤2000
2≤n≤105
1≤ai≤105
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