标签:
Problem Description:
题意:给出一个序列,如果去掉一个元素,问剩下的序列是否是有序序列。
#include<stdio.h> #include<string.h> #include<queue> #include<math.h> #include<stdlib.h> #include<algorithm> using namespace std; const int N=1e5+10; const int INF=0x3f3f3f3f; const int MOD=1e9+7; typedef long long LL; int a[N], flag; void Increase(int n) { int i, num, idex; num = flag = 0; for (i = 2; i <= n; i++) { if (a[i] < a[i-1]) { num++; idex = i; ///标记不满足元素的下标 } } if (num == 0) flag = 1; if (num == 1) { if (idex == n || idex == 2 || a[idex-2] <= a[idex] || a[idex-1] <= a[idex+1]) flag = 1; ///如果这个不满足元素的左边和右边还是非递减的,那么可以去掉该元素,或者该元素前的第二个元素和该元素是非递减的,可以去掉该元素之前的第一个元素 } } void Decrease(int n) { int idex, i, num; num = flag = 0; for (i = 2; i <= n; i++) { if (a[i] > a[i-1]) { num++; idex = i; } } if (num == 0) flag = 1; if (num == 1) { if (idex == 2 || idex == n || a[idex-1] >= a[idex+1] || a[idex-2] >= a[idex]) flag = 1; } } int main () { int T, i, n; scanf("%d", &T); while (T--) { scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &a[i]); Increase(n); ///先判断是否是非递减序列:找到不满足条件的元素下标,并统计个数,个数<=1时才满足条件 if (flag == 0) Decrease(n); ///如果不是非递减序列再判断是否是非递增序列 if (flag == 1) printf("YES\n"); else printf("NO\n"); } return 0; }
2015ACM/ICPC亚洲区长春站-重现赛 1006 Almost Sorted Array
标签:
原文地址:http://www.cnblogs.com/syhandll/p/4928129.html