hdu 5532 Almost Sorted Array(LIS)
来源:互联网 发布:midaspro3离线编辑软件 编辑:程序博客网 时间:2024/06/15 14:23
Almost Sorted Array
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 5587 Accepted Submission(s): 1315
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 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 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
题意:在原数列中删除一个数判断是否有序;
解:LIS的n*log(n) 啪啪啪打脸
#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<cmath>#include<string>#include <bits/stdc++.h>using namespace std;typedef long long LL;const int N =2e6+10;const int inf = 0x3f3f3f3f;int a[N], b[N], c[N];int cmp(int A,int B){return A>B;}int main(){ int t, n; scanf("%d", &t); while(t--) { scanf("%d", &n); for(int i=0;i<n;i++) scanf("%d", &a[i]), b[n-i-1]=a[i]; int k=1; c[0]=a[0]; for(int i=1;i<n;i++) { if(a[i]>=c[k-1]) c[k++]=a[i]; else *upper_bound(c,c+k,a[i])=a[i]; } if(k>=n-1) { puts("YES"); continue; } k=1; c[0]=b[0]; for(int i=1;i<n;i++) { if(b[i]>=c[k-1]) c[k++]=b[i]; else *upper_bound(c,c+k,b[i])=b[i]; } if(k>=n-1) { puts("YES"); continue; } puts("NO"); } return 0;}
阅读全文
0 0
- 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(LIS)
- hdu 5532 Almost Sorted Array(LIS)
- HDU-5532Almost Sorted Array LIS问题
- HDU 5532 Almost Sorted Array(LIS讲解+二分)
- HDU 5532 Almost Sorted Array——LIS
- HDU 5532 Almost Sorted Array(LIS模板题)
- hdoj 5532 Almost Sorted Array 【LIS】
- hdoj--5532--Almost Sorted Array(正反LIS)
- hdu 5532 Almost Sorted Array
- HDU 5532 Almost Sorted Array
- HDU 5532 Almost Sorted Array
- 【HDU】 5532 Almost Sorted Array
- HDU 5532 Almost Sorted Array
- HDU 5532:Almost Sorted Array
- A Bug's Life-----分类并查集
- 【Java进阶-Java动态代理与AOP】04 实现InvocationHandler的invoke方法
- P1939 【模板】矩阵加速(数列)
- MyBatis 入门开发
- 单片机中断详讲
- hdu 5532 Almost Sorted Array(LIS)
- HDU-1079 Calendar Game(找规律,博弈)
- Activity
- Struts2创建项目1
- uva 1608(分治 + 中途相遇法)
- E
- 机器学习之Grid World的SARSA算法解析
- 初次使用Base64转储文件小结
- 集训第十七天(2017/8/16):继续研究树状数组