hdu 5532 Almost Sorted Array 【2015ACM/ICPC亚洲区长春站-重现赛】
2015-11-04 17:07
597 查看
Almost Sorted Array
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 404 Accepted Submission(s): 188
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 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
Source
2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大)
题目大意:
有T组数据,每组数据有 m 个数,然后让你判断当去掉其中一个数的时候还是不是一个有序的数列(可以是非递增的 ,也可以是非递减的)
如果是的话,输出YES,否则输出NO。
解题思路:
刚开始我是打算用最长上升子序列做的,但是发现 m 的范围有点大,所以就没有用,我也说一下,以当参考,就是判断这个数列的
最长上升子序列的最大长度(在把序列倒过来计算一遍),如果 LIS 的值 >= m-1的话就是一个有序的,否则不是,但是会TLE,
下面是我的另一种做法。。。
就是分别判断一下非递增的数列(和非递减的数列),同时判断的时候还要考虑前后两项是不是也满足非递增(非递减)的关系。。。
然后就ok了。
可以参考一下我的代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <algorithm> #include <set> using namespace std; #define MM(a) memset(a,0,sizeof(a)) typedef long long LL; typedef unsigned long long ULL; const int maxn = 1e5+5; const int mod = 1000000007; const int INF = 0x3f3f3f3f; const double eps = 1e-7; int arr[maxn]; int m; bool judge1()///从小到大 { int sum = 0; arr[0] = -INF, arr[m+1] = INF; for(int i=2; i<=m; i++) { if(arr[i] < arr[i-1]) { if(sum == 1) return false; sum++; if(arr[i+1]>=arr[i-1] || arr[i]>=arr[i-2]) continue; else return false; } } return true; } bool judge2()///从大到小 { int sum = 0; arr[0] = INF, arr[m+1] = -INF; for(int i=2; i<=m; i++) { if(arr[i] > arr[i-1]) { if(sum == 1) return false; sum++; if(arr[i+1]<=arr[i-1] || arr[i]<=arr[i-2]) continue; else return false; } } return true; } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d",&m); for(int i=1; i<=m; i++) scanf("%d",&arr[i]); if(judge1() || judge2()) puts("YES"); else puts("NO"); } return 0; } /** 2 5 1 2 5 3 4 5 1 2 1 4 5 **/
相关文章推荐
- 使用ViewTreeObserver动态调整控件位置(Android)
- GridControl控件的数据显示的样式控制(转)
- jquery源码分析
- 黑马程序员-----Java概述
- java.lang.NoClassDefFoundError: org.apache.axis.cl
- Q-Q图的理解
- Linux系统安全加固策略(一)
- PHP书写规范 PHP
- CSS属性选择器参考表
- PHP通过接口形式传输xml数据
- 底边滑动变色的列表
- js原生设计模式——2面向对象编程之js原生的链式调用
- 移动应用体检报告
- Linux IDR机制
- Mysql 5.7多源复制及并行复制功能
- linux操作系统测试工具(不算原创,是我们一起试验后完全可行的一套流程)
- onCreate方法中调用PopupWindow的错误:android.view.WindowManager$BadTo
- Mysql 5.7多源复制及并行复制功能
- CSS十六种基本颜色
- 在 Java 中高效使用锁的技巧--转载