码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5532 / 2015ACM/ICPC亚洲区长春站 F.Almost Sorted Array

时间:2015-11-19 14:58:49      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

Almost Sorted Array




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,可做
        还有一种是O(N) 对于每一个数,要么呈现V形或者^形,或者单调,记录上升下降次数,我们对删除某个数所造成影响记录下来,假如删除它致使最后上升次数或者下降次数满足n-2次就行了
技术分享
///1085422276
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define inf 1000000007
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<0||ch>9){
        if(ch==-)f=-1;ch=getchar();
    }
    while(ch>=0&&ch<=9){
        x=x*10+ch-0;ch=getchar();
    }return x*f;
}
//****************************************

#define maxn 100000+5
int a[maxn],n;
int main(){
    int T=read();
    while(T--){
        scanf("%d",&n);int flag=0;
        for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        }
        int res=0,fall=0;
        for(int i=1;i<=n;i++){
            if(i-1>=1&&a[i]>a[i-1])res++;
            else if(i-1>=1&&a[i]<a[i-1])fall++;
        }
       // cout<<fall<<" "<<res<<endl;
        for(int i=1;i<=n;i++){
                int t1=res,t2=fall;
            if(i-1>=1&&a[i]>a[i-1]){
                res--;
            }
            else if(i-1>=1&&a[i]<a[i-1]){
                fall--;
            }
            if(i+1<=n&&a[i]>a[i+1]){
                fall--;
            }
            else if(i+1<=n&&a[i]<a[i+1]){
                res--;
            }
            if(i-1>=1&&i+1<=n){
                if(a[i-1]<a[i+1]){
                    res++;
                }else if(a[i-1]>a[i+1])fall++;
            }
            if(fall==0||res==0){
                flag=1;break;
            }
        res=t1;fall=t2;
        }
        if(flag){
            cout<<"YES"<<endl;
        }
        else cout<<"NO"<<endl;

    }
  return 0;
}
代码

 

HDU 5532 / 2015ACM/ICPC亚洲区长春站 F.Almost Sorted Array

标签:

原文地址:http://www.cnblogs.com/zxhl/p/4977481.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!