hdu5532 长春站水题
2015-12-07 15:22
274 查看
<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; }
相关文章推荐
- Java中的数组工具类Arrays
- Volley解析Xml数据
- 关于xcode7出现的一些问题
- Linux中TCP连接过程状态简介
- 一步步学习javascript基础篇(5):面向对象设计之对象继承(原型链继承)
- ADC0809 ISIS仿真
- Volley加载图片
- android使用html+javascript来制作页面
- 【android L数据连接】如何在Sim Cards的cellular data添加OFF
- JSON库之性能比较:JSON.simple VS GSON VS Jackson VS JSONP
- 稀疏矩阵的存储方法之二
- android(35)(service服务、生命周期、进程优先级)
- iOS实现MD5加密的代码
- leetcode Count of Smaller Numbers After Self
- git 理解和使用
- 关于类(继承)
- string iterators incompatible
- iOS常见创建页面的方式
- 是时候写点东西
- uart dma实现方式分析