HDOJ 5532 Almost Sorted Array(qwb铜牌水题 最大上升子序列)

Almost Sorted Array

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3262    Accepted Submission(s): 818


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
 

Source



思路:
题意就是问你一串数去掉一个数之后还是不是非递减或者非递增的。
转化为求数字串正反两面的最大上升子序列长度。只要LIS长度大于等于n-1,那么他一定可以去掉一个数之后还是单调的。
我这里用了kuangbi大神的板子求的LIS的长度。正反扫两次就好。

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=500010; 
int a[MAXN],b[MAXN],c[MAXN],d[MAXN]; 
 
int Search1(int num,int low,int high) 
{     
	int mid;     
	while(low<=high)     
	{ 
		mid=(low+high)/2;         
		if(num>=b[mid])  
		low=mid+1;         
		else   
		high=mid-1;     
	}     
	return low; 
} 
int DP1(int n) 
{     
	int i,len,pos;     
	b[1]=a[1];     
	len=1;     
	for(i=2;i<=n;i++)     
	{         
		if(a[i]>=b[len])       
		{             
			len=len+1;             
			b[len]=a[i];         
		}         
		else      
		{           
			pos=Search1(a[i],1,len);           
			b[pos]=a[i];        
		}    
	}    
	return len;
} 

int Search2(int num,int low,int high) 
{     
	int mid;     
	while(low<=high)     
	{ 
		mid=(low+high)/2;         
		if(num>=d[mid])  
		low=mid+1;         
		else   
		high=mid-1;     
	}     
	return low; 
} 
int DP2(int n) 
{     
	int i,len,pos;     
	d[1]=c[1];     
	len=1;     
	for(i=2;i<=n;i++)     
	{         
		if(c[i]>=d[len])      
		{             
			len=len+1;             
			d[len]=c[i];         
		}         
		else         
		{           
			pos=Search2(c[i],1,len);           
			d[pos]=c[i];        
		}    
	}    
	return len;
} 
		 
int main()
{
	int num;
    while(scanf("%d",&num)!=EOF)
    {
    	while(num--)
    	{
    		int n;
    		cin>>n;
    		for(int i=1;i<=n;i++)
	        {
	            scanf("%d",&a[i]);
	            c[n-i+1]=a[i];
	        }    
	        int res1=DP1(n);
	        int res2=DP2(n);
	        //cout<<res1<<" "<<res2<<endl; 
	        if(res1>=n-1||res2>=n-1)printf("YES\n");
			else printf("NO\n");    
    	}  
    }
    return 0;    	
}



全部评论

相关推荐

_mos_:要不是看评论区我都不知道你要找的是数分
点赞 评论 收藏
分享
牛客87317764...:最近没啥hc,做好心灰意冷的准备。另外,大概率只有字节给你面试,最好别作为处女面
实习简历求拷打
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务