hdu5532 长春站水题
来源:互联网 发布:如何摆脱网络依赖症 编辑:程序博客网 时间:2024/04/28 12:53
<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;}
0 0
- hdu5532 长春站水题
- hdu5532
- 【HDU5532 2015长春赛区F】【LIS+剪枝】Almost Sorted Array 最多移除一元素后单调 O(n)
- 长春
- HDU5532-Almost Sorted Array
- HDU5532 Almost Sorted Array
- hdu5532 Almost Sorted Array(LIS)
- 长春印象
- 长春*吉他
- 长春旅游景点
- ZF_20160702_Region2013长春
- hdu5532 Almost Sorted Array--最长上升子序列
- HDU5532 Almost Sorted Array(最长不上升子序列)
- HDU5532-LIS||暴力(注意剪枝)-Almost Sorted Array
- HDU5532 Almost Sorted Array(最长子序列)
- 长春电台频率表
- 长春之行
- 明天去长春出差。。。
- Volley解析Xml数据
- LeetCode—315 Count of Smaller Numbers After Self
- Linux中TCP连接过程状态简介
- Ubuntu磁盘空间不足,加挂新硬盘
- Java中的数组工具类Arrays
- hdu5532 长春站水题
- 利用HTML5开发Android
- 将 mysql 查询结果输出到文件
- Volley解析Json数据
- android动画
- 稀疏矩阵存储之二
- django分页实现
- 串联型或并联型电压基准的选择
- eclipse中启动tomcat的项目路径