HDU 5532——Almost Sorted Array——————【技巧】
Almost Sorted Array
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 236 Accepted Submission(s): 113
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?
1≤T≤2000
2≤n≤105
1≤ai≤105
There are at most 20 test cases with n>1000.
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 100010;
const int INF = 0x3f3f3f3f;
int a[maxn];
bool jud1(int n){
int tmp = a[2];
for(int i = 2; i <=n; i++){
if(tmp > a[i]){
return false;
}else{
tmp = a[i];
}
}
return true;
}
bool jud2(int n){
int tmp = a[2];
for(int i = 2; i <= n; i++){
if(tmp < a[i]){
return false;
}else{
tmp = a[i];
}
}
return true;
}
bool jud3(int n){
a[0] = 0; a[n+1] = INF;
int tim = 0;
int fir = 0;
for(int i = 2; i <= n; i++){
if(a[i]<a[i-1]){
if(fir == 1)
return false;
fir++;
if(a[i+1]>=a[i-1]||a[i]>=a[i-2]){
continue;
}else{
return false;
}
}
}
return true;
}
bool jud4(int n){
a[0] = INF; a[n+1] = 0;
int tim = 0;
int fir = 0;
for(int i = 2; i <= n; i++){
if(a[i]>a[i-1]){
if(fir == 1)
return false;
fir++;
if(a[i+1]<=a[i-1]||a[i]<=a[i-2]){
continue;
}else{
return false;
}
}
}
return true;
}
int main(){
int T,n,m;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i = 1; i <= n; i++){
scanf("%d",&a[i]);
}
if(jud1(n)){
puts("YES");
continue;
}
if(jud2(n)){
puts("YES");
continue;
}
if(jud3(n)){
puts("YES");
continue;
}
if(jud4(n)){
puts("YES");
continue;
}
puts("NO");
}
return 0;
}
/*
555
4
8 6 7 8
4
8 6 7 7
5
1 2 1000 7 8
5
1 2 100 2 3
4
1 1 1 1
6
7 8 1 1 1 10 */
HDU 5532——Almost Sorted Array——————【技巧】的更多相关文章
- hdu 5532 Almost Sorted Array(模拟)
Problem Description We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, ...
- HDU 5532 Almost Sorted Array (最长非递减子序列)
题目链接 Problem Description We are all familiar with sorting algorithms: quick sort, merge sort, heap s ...
- hdu 5532 Almost Sorted Array (水题)
Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- hdu 5532 Almost Sorted Array nlogn 的最长非严格单调子序列
Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- HDU - 5532 Almost Sorted Array (最长非严格单调子序列)
We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, sele ...
- hdu 5532 Almost Sorted Array
http://acm.hdu.edu.cn/showproblem.php?pid=5532 题目大意: 给你一个不规则的序列,问是否能够通过删除一个元素使其成为一个有序的序列(递增或递减(其中相邻 ...
- 【HDU 5532 Almost Sorted Array】水题,模拟
给出一个序列(长度>=2),问去掉一个元素后是否能成为单调不降序列或单调不增序列. 对任一序列,先假设其可改造为单调不降序列,若成立则输出YES,不成立再假设其可改造为单调不增序列,若成立则输出 ...
- 2015ACM/ICPC亚洲区长春站 F hdu 5533 Almost Sorted Array
Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- HDU 5532 / 2015ACM/ICPC亚洲区长春站 F.Almost Sorted Array
Almost Sorted Array Problem Description We are all familiar with sorting algorithms: quick sort, mer ...
随机推荐
- sqlserver的索引创建
随着系统数据的增多,一些查询逐渐变慢,这时候我们可以根据sqlserver的执行计划,查看sql的开销,然后根据开销创建索引. 索引有聚集索引与非聚集索引. 聚集索引:聚集索引在存储上是按照顺序存储的 ...
- Data Base oracle简单使用及管理工具使用
oracle简单使用及管理工具使用 一.常用工具: 1.sqldeveloper 2.navicat for oracle 3.PLSQL Developer 4.toad
- 【Python发展】pandas和koalas
1.pandas介绍 Python 数据科学在过去几年中爆炸式增长, pandas 已成为生态系统的关键.当数据科学家得到一个数据集时,他们会使用 pandas 进行探索.它是数据处理和分析的终极工具 ...
- 【图灵学院10】高并发之java线程池源码分析
1. 提纲 1)线程池的模块结构 2)示例&原理解析 2. 问题 1)线程池包含哪些东西 2)线程池的运作原理 3)调度线程池的运作原理 4)线程池怎么实现FixRate,FixDelay,他 ...
- 转载Json和Xml的区别,以及它们的底层是如何处理的
XML:可扩展标记语言 JSON:轻量级的数据交换格式 区别: 1.可读性方面:基本相同,Xml的可读性较好些: 2.可扩展性方面:都有较好的扩展性: 3.编码难度方面:json的编码较容 ...
- P2700 逐个击破
题意:现在有N个城市,其中K个被敌方军团占领了,N个城市间有N-1条公路相连,破坏其中某条公路的代价是已知的, 现在,告诉你K个敌方军团所在的城市,以及所有公路破坏的代价,请你算出花费最少的代价将这K ...
- CF1076C Meme Problem 数学
Try guessing the statement from this picture: You are given a non-negative integer d . You have to f ...
- Blocks poj 区间dp
Some of you may have played a game called 'Blocks'. There are n blocks in a row, each box has a colo ...
- kuangbin专题十六 KMP&&扩展KMP HDU3068 最长回文
给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input输入有多组case,不超过120组,每组输入为 ...
- .net mvc 框架实现后台管理系统 2
layui 数据表格 返回格式: var json = new { code = 0, count = pagers.totalRows, msg = "", data =null ...