Almost Sorted ArrayTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 7942 Accepted Submission(s): 1848 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. 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. Output For each test case, please output "`YES`" if it is almost sorted. Otherwise, output "`NO`" (both without quotes). Sample Input 3
扫描二维码关注公众号,回复:
3152312 查看本文章
3 2 1 7 3 3 2 1 5 3 1 4 1 5 Sample Output YES YES NO Source 2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大) Recommend hujie | We have carefully selected several similar problems for you: 6447 6446 6445 6444 6443 |
题意:给定长度的数列,去掉一个数字后,能否让数列变成为递增或递减。
思路:先正着LIS,再把数列翻转,再求一次LIS。判断 lis >= n-1 是否成立即可。
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define line cout<<"-----------------"<<endl;
typedef long long ll;
const int maxn = 1e5+10;
const int MAXN = 1e6+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int N = 1010;
int n;
int a[maxn],b[maxn];
bool check(){
int top = 0;
for(int i=0;i<n;i++){
int pos = upper_bound(b, b+top, a[i]) - b;
b[pos] = a[i];
top = max(top, pos+1);
}
if(top >= n-1) return true;
return false;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
bool flag = check();
reverse(a, a+n);
flag |= check();
if(flag) puts("YES");
else puts("NO");
}
return 0;
}