F - Almost Sorted Array
F - Almost Sorted Array
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的更多相关文章
- HDU-5532//2015ACM/ICPC亚洲区长春站-重现赛-F - Almost Sorted Array/,哈哈,水一把区域赛的题~~
F - Almost Sorted Array Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- HDU 5532 / 2015ACM/ICPC亚洲区长春站 F.Almost Sorted Array
Almost Sorted Array Problem Description We are all familiar with sorting algorithms: quick sort, mer ...
- 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 ...
- 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现
一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...
- 【原创】leetCodeOj --- Find Minimum in Rotated Sorted Array II 解题报告
题目地址: https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目内容: Suppose a sort ...
- LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- [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 ...
随机推荐
- windows下生成zlib1.dll
一.原料: VC zlib-1.2.3-src.zip 二.解压zlib-1.2.3-src.zip,用VC打开工作空间 src/zlib/1.2.3/zlib-1.2.3/projects/visu ...
- Java中“==”与equals的区别以及equals方法的重写
一.“==”与equals的区别: (1)==代表比较双方是否相同: 基本数据类型表示值相等. 引用数据类型表示地址相等,即同一个对象. (2)Object中的equals()方法:是否为同一个对象的 ...
- openlayers之地图截图
方法1 //this.map._this为初始化地图对象 this.map._this.once('postcompose', function (event) { var canvas = even ...
- openCV3 Python编译指南
这里主要对openCV官网的<Installation in Linux>文档进行了翻译和解释 原文见:https://docs.opencv.org/3.4.1/doc/tutorial ...
- docker使用国内镜像加速
在daemon.json文件里以下国内镜像 { "registry-mirrors": [ "https://registry.docker-cn.com", ...
- hadoop安装zookeeper-3.4.12
在安装hbase的时候,需要安装zookeeper,当然也可以用hbase自己管理的zookeeper,在这里我们独立安装zookeeper-3.4.12. 下载地址:https://mirrors. ...
- 防范DDoS攻击的15个方法
0x01 背景 为了对抗 DDoS(分布式拒绝服务)攻击,你需要对攻击时发生了什么有一个清楚的理解..简单来讲,DDoS 攻击可以通过利用服务器上的漏洞,或者消耗服务器上的资源(例如 内存.硬盘等等) ...
- python根据已有数据库生成model.py
有时我们需要根据已存在的数据库进行django开发时,手写model.py是不现实的 先执行下面的语句,在命令行终端会输出所有表的类 python .\manage.py inspectdb 检查无误 ...
- UltraEdit中的特殊字符
以下特殊字符,可以在替换中用到, ^n--换行 ^t--Tab
- 第十篇.2、python并发编程之多进程
一 multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程.P ...