HDU 5532 Almost Sorted Array (2015ACM/ICPC长春&&LIS)
来源:互联网 发布:sql 查询数据库大小 编辑:程序博客网 时间:2024/06/05 15:43
【题目链接】:click here~~
【题目描述】:
Almost Sorted Array
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 272 Accepted Submission(s): 132
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 arraya1,a2,…,an , is it almost sorted?
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
Input
The first line contains an integer T indicating the total number of test cases. Each test case starts with an integern 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 withn>1000 .
There are at most 20 test cases with
Output
For each test case, please output "`YES`" if it is almost sorted. Otherwise, output "`NO`" (both without quotes).
Sample Input
332 1 733 2 153 1 4 1 5
Sample Output
YESYESNO
【思路】我们考虑删除一个元素后,使得单调递增最长的序列长度满足>=n-1就可以了,单调递减的就翻转数列在操作一遍即可,复杂度(O(nlogN))
代码:
/** Problem:HDU 5532* Running time: 1100MS* Complier: G++* Author: javaherongwei* Create Time: 21:20 2015/11/02 星期一*/#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>const int inf=1e9+7;const int maxn=1e5+10;using namespace std;inline int max(int a,int b){ return a>b?a:b;}int a[maxn],b[maxn],c[maxn];int n;int binary_search(int x){ //二分搜索 int l=1,r=n; while(l<r){ int mid=(l+r)>>1; if(b[mid]>x) r=mid; else l=mid+1; } return l;}bool LIS(const int *a){ //LIS memset(b,inf,sizeof(b)); b[0]=0; int maxx=0; for(int i=1; i<=n; ++i){ int idx=binary_search(a[i]);//i位置的LIS为idx maxx=max(idx,maxx); b[idx]=a[i]; } if(maxx>=n-1) return true; //满足条件即可 return false;}int main(){ //freopen("1.txt","r",stdin); int t;scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i=1; i<=n; ++i){ scanf("%d",&a[i]); c[n-i+1]=a[i]; } if(LIS(a)||LIS(c)) puts("YES"); else puts("NO"); } return 0;}
0 0
- HDU 5532 Almost Sorted Array (2015ACM/ICPC长春&&LIS)
- F--Almost Sorted Array(2015-ACM-ICPC长春站)
- hdu 5532 Almost Sorted Array(LIS)
- 【HDU】5532 - Almost Sorted Array(LIS)
- HDU-5532 Almost Sorted Array(LIS)
- HDU 5532 Almost Sorted Array(LIS)
- hdu 5532 Almost Sorted Array 2015 长春区域赛 模拟
- HDU-5532-Almost Sorted Array【2015长春赛区】
- HDU 5532 Almost Sorted Array (LIS)
- hdu 5532 Almost Sorted Array(LIS)
- HDU-5532Almost Sorted Array LIS问题
- hdu 5532 Almost Sorted Array 【2015ACM/ICPC亚洲区长春站-重现赛】
- hdu 5532 Almost Sorted Array 2015ACM/ICPC亚洲区长春站-重现赛
- 2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大)HDU 5532 Almost Sorted Array
- HDU 5532 Almost Sorted Array(LIS讲解+二分)
- HDU 5532 Almost Sorted Array(LIS模板题)
- [hdu 5532] [2015ACM/ICPC亚洲区长春站 ] Almost Sorted Array 最长不下降子序列
- HDU 5532 Almost Sorted Array——LIS
- 【UML】宏观结构与概述
- 强制声明泛型的实际类型
- Codevs P1066 引水入城 2010年NOIP全国联赛提高组
- POJ 1743 Musical Theme (后缀数组)
- poj2289多重二分匹配
- HDU 5532 Almost Sorted Array (2015ACM/ICPC长春&&LIS)
- 软件质量模型
- 逻辑斯蒂回归 Logstic Regression in Python
- 开源框架spring AOP的深刻理解
- form表单被自动提交
- 【设计模式】Filter-Chain 的应用
- 关联选择器和组合选择器,伪元素选择器
- Modern OpenGL ES: ndk编程——画一个三角形之Shader工程
- Linux 的文件权限与目录配置