F - Almost Sorted Array

 
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,…,ana1,a2,…,an, is it almost sorted?

InputThe first line contains an integer TT indicating the total number of test cases. Each test case starts with an integer nn in one line, then one line with nn integers a1,a2,…,ana1,a2,…,an.

1≤T≤20001≤T≤2000 
2≤n≤1052≤n≤105 
1≤ai≤1051≤ai≤105 
There are at most 20 test cases with n>1000n>1000.OutputFor 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
//需要用到的算法:最长递增子序列

#include <iostream>
#include <cstdio>
#include <memory.h>
#include <algorithm> #define INF 0x3f3f3f using namespace std; const int MAXN = 1e5 + ; int dp[MAXN];
int a[MAXN];
int b[MAXN]; int main() {
int T;
cin >> T;
while(T--) {
int n;
scanf("%d", &n);
memset(dp, INF, sizeof dp);
for(int i = ; i < n; i++) {
scanf("%d", &a[i]);
b[n - i - ] = a[i];
}
//递增
//依次遍历,将当前元素插入dp数字中适当的位置
for(int i = ; i < n; i++) {
*upper_bound(dp, dp + n, a[i]) = a[i];
}
//求出最长递增子序列的长度
int cnt1 = lower_bound(dp, dp + n, INF) - dp;
//递减
memset(dp, INF, sizeof dp);
for(int i = ; i < n; i++) {
*upper_bound(dp, dp + n, b[i]) = b[i];
}
int cnt2 = lower_bound(dp, dp + n, INF) - dp;
//如果最后递增和递减的长度其中有一个大于或等于(n-1)的话,就输出YES,否则输出NO
if(cnt1 >= (n - ) || cnt2 >= (n - )) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return ;
}
												

F - Almost Sorted Array的更多相关文章

  1. HDU-5532//2015ACM/ICPC亚洲区长春站-重现赛-F - Almost Sorted Array/,哈哈,水一把区域赛的题~~

    F - Almost Sorted Array Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  2. HDU 5532 / 2015ACM/ICPC亚洲区长春站 F.Almost Sorted Array

    Almost Sorted Array Problem Description We are all familiar with sorting algorithms: quick sort, mer ...

  3. 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 ...

  4. 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现

    一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...

  5. 【原创】leetCodeOj --- Find Minimum in Rotated Sorted Array II 解题报告

    题目地址: https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目内容: Suppose a sort ...

  6. LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  7. Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

  8. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  9. [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. java解析json字符串详解(两种方法)

    一.使用JSONObject来解析JSON数据官方提供的,所以不需要导入第三方jar包:直接上代码,如下 private void parseJSONWithJSONObject(String Jso ...

  2. Python 入门 之 反射

    Python 入门 之 反射 1.反射 : (自省) ​ 反射主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省). Python面向对象中的反射:通过字符串的形式操作对象的相关属性.P ...

  3. Python 入门 之 类的三大关系(依赖 / 组合/ 继承关系)

    Python 入门 之 类的三大关系(依赖 / 组合/ 继承关系) 在面向对象的中,类与类之间存在三种关系:依赖关系.组合关系.继承关系. 1.依赖关系:将一个类的类名或对象当做参数传递给另一个函数被 ...

  4. js实现复制内容到剪贴板

    一. 原生js实现,电脑可以用,手机不可以用 1. 必须是 input元素 才可以使用 <input id="code" type="text" valu ...

  5. 实现远程线程DLL注入

    ### 32位:远程线程注入 远程线程注入是最常用的一种注入技术,该技术利用的核心API是 `CreateRemoteThread()` 这个API可以运行远程线程,其次通过创建的线程调用 `Load ...

  6. python网络爬虫(12)去哪网酒店信息爬取

    目的意义 爬取某地的酒店价格信息,示例使用selenium在Firefox中的使用. 来源 少部分来源于书.python爬虫开发与项目实战 构造 本次使用简易的方案,模拟浏览器访问,然后输入字段,查找 ...

  7. 面试之什么是java虚拟机

    java虚拟机体系结构 方法区 堆 java虚拟机栈 本地方法栈 方法区 java虚拟机编译的class文件中二进制数据类型解析数据存在方法区中 是所有线程共享 和存在数据的线程安全问题 当二个线程使 ...

  8. 无障碍开发(三)之ARIA aria-***属性值

    aria-***属性值

  9. 数据绑定-@RequestParam

    @PathVariable,前面已经讲过了 @RequestParam 作用:获取请求中的参数,GET请求,问号后面的请求参数,POST:请求体中的KV数据. 测试:

  10. springboot(二十二)-sharding-jdbc-读写分离

    前面我们使用sharding-jdbc配置了分库分表.sharding-jdbc还有个用法,就是实现读写分离. 什么时候需要或者可以使用读写分离? 当我们的项目所使用的数据库查询的访问量,访问频率,及 ...