首页 > 其他 > 详细

hdu 5532 Almost Sorted Array

时间:2015-12-10 19:10:33      阅读:105      评论:0      收藏:0      [点我收藏+]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5532

Almost Sorted Array

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


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 a技术分享1技术分享,a技术分享2技术分享,,a技术分享n技术分享技术分享 , 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 a技术分享1技术分享,a技术分享2技术分享,,a技术分享n技术分享技术分享 .

1T2000技术分享
2n10技术分享5技术分享技术分享
1a技术分享i技术分享10技术分享5技术分享技术分享
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
 题意:给你一个长度为n的数列,让你从中删除一个数,使这个数列变成非递减或者非递增(数列中可能有重复的数)
题解:对数列正反各求一次LIS(最长上升子序列(这里要将相等的数也考虑进去))再拿n减去最长上升子序列的长度,小于等于1输出YES
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
#define MAX 100100
#define INF 0x3f3f3f
#define DD double
using namespace std;
int stack[MAX];
int top;
int f(int x)//二分 查找栈中第一个大于x的数 
{
	int l=0,r=top;
	int mid;
	while(r>=l)
	{
		mid=(l+r)/2;
		if(x>=stack[mid])
		    l=mid+1;
		else
		    r=mid-1;
	}
	return l;
} 
int main()
{
	int t,n,m,j,i,k;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		int s[MAX];
		for(i=1;i<=n;i++)
		    scanf("%d",&s[i]);
		
		top=0;
		stack[0]=-1;
		for(i=1;i<=n;i++)
		{
			if(s[i]>=stack[top])
			    stack[++top]=s[i];
			else
				stack[f(s[i])]=s[i];
		}
		int ans=top;
		
		memset(stack,0,sizeof(0)); 
		top=0;
		stack[0]=-1;
		for(i=n;i>=1;i--)
		{
			if(s[i]>=stack[top])
			    stack[++top]=s[i];
			else
				stack[f(s[i])]=s[i];
		}
		int ant=top;
		if(n-ant<=1||n-ans<=1)
		printf("YES\n");
		else
		printf("NO\n");
	} 
	return 0;
} 

  

hdu 5532 Almost Sorted Array

原文:http://www.cnblogs.com/tonghao/p/5036447.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!