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 ...
随机推荐
- 链表-简单练习题1-数据结构实验之链表一:顺序建立链表 SDUT2117
Problem Description 输入N个整数,按照输入的顺序建立单链表存储,并遍历所建立的单链表,输出这些数据. Input 第一行输入整数的个数N:第二行依次输入每个整数. Output 输 ...
- vscode中eslint插件的配置-prettier
用vue-cli构建vue项目,会有个eslint代码检测的安装 可vscode自带代码格式化是prettier格式(右键有格式化文件或alt+shift+f) 这时候要在vscode上装一个esli ...
- CentOS7部署Tomcat服务器
1. 软件 存放路径:/usr/local/src apache-tomcat-9.0.22.tar.gz openjdk-12_linux-x64_bin.tar.gz 2.事先配置 启动后关闭防火 ...
- 第二章 单表查询 T-SQL语言基础(1)
单表查询(1) 本章:逻辑查询处理,特定的SELECT查询生成正确的结果集而要经历的一系列逻辑阶段;单表查询的其他方面,包括:运算符,空值(NULL),字符的处理和临时数据,分级(ranking),C ...
- c# ListView 简单操作
1. 添加数据 listView1.Items.Clear(); for (int i = 0; i < 50; i++) { ListViewItem lv = new ListViewIte ...
- IAP技术原理
目录 IAP技术原理 更新记录 IAP与ISP的概念及原理 ISP简介 ISP原理 IAP简介 IAP原理 IAP优势 IAP的设计 1.程序启动流程 2.中断向量表的重定位 3.IAP跳转APP函数 ...
- 封装H5ToApp方法
方法一: 新建个 Android studio 项目,用 webview 指定访问你的页面 方法二: 使用工具 cordova 附上地址:http://cordova.axuer.com/docs/ ...
- JS语法学习笔记-菜鸟教程
现在只需要<script></script>标签 javascript通常做法是放在<head>部分中,或者放在页面底部,这样可以把他们安置在同一位置,不会干扰页面 ...
- mysql的导入导出操作
mysqldump工具基本用法 此方法不适用于大数据备份 备份所有数据库 mysqldump -u root -p --all-databases > all_database_sql 备份my ...
- shell脚本if判断语句报错[: too many arguments的两种原因
shell脚本,if判断语句报错[: too many arguments 我遇到过两种情况: 1.第一中情况就是网上大家说的,字符串变量中可能存在空格,shell解析时将其认为是多个参数,再进行判断 ...