您的位置:首页 > 其它

hdu5532 长春站水题

2015-12-07 15:22 274 查看
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">Problem Description</span>
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

15长春区域赛的水题,奈何我比他更水,当时卡了两个多点!现在想想真是愚蠢。

这题的大意是说给一个长度为n的序列,求去掉其中一个元素后不会不会成为不增或者不降序列,就是模拟。

标记一下该去掉点的数目,到2就false,还有几个连续的点在一起,直接导致false的情况,比如(2 4 1 3),(2 4 1 5),(4 5 1 2),正向反向都判断一次,如果满足就可以了

#include<iostream>
#include<cstdio>
#include<cstdio>
using namespace std;
int a[100005];
int main()
{
int t,n,i,j;
scanf("%d",&t);
while(t--)
{
int falg = 0;
cin>>n;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
int s = 0;
a[0]=-100001;a[n+1] = 0x3f3f3f3f;
for(i=1;i<=n;i++)/**不降**/
{
if(a[i]<a[i-1])
{
if(s==1) {s=2;break;}
else if(i>2&&a[i]<a[i-2]&&a[i+1]<a[i-1]) {s=2;break;}
else {s=1;}
}
}
if(s<2) falg=1;
s = 0;
a[0]=0x3f3f3f3f;a[n+1] = -100001;
for(i=1;i<=n;i++)/**不增**/
{
if(a[i]>a[i-1])
{
if(s==1) {s=2;break;}
else if(i>2&&a[i]>a[i-2]&&a[i+1]>a[i-1]) {s=2;break;}
else {s=1;}
}
}
if(s<2) falg=1;
if(falg) puts("YES");
else puts("NO");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: