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 ...
随机推荐
- QQ登錄實現功能
<html> <head> <title></title> </head> <body> <form> <p& ...
- Java 条件语句 if else
一个 if 语句包含一个布尔表达式和一条或多条语句. 语法 if 语句的语法如下: if(布尔表达式) { //如果布尔表达式为true将执行的语句 } 如果布尔表达式的值为 true,则执行 if ...
- thinkPHP模型before_insert新增前 before_update更新前 before_write写入前 区别
thinkPHP模型中有个save方法,可用于新增数据和修改数据,这里容易出现混淆. 经过调试: before_write,不管是插入新数据还是修改数据都会执行: before_insert,只有插入 ...
- thinkPHP验证码报错: Call to undefined function captcha_src()
问题出现的原因可能有: 1. captcha扩展缺失: 2. captcha扩展与当前thinkPHP版本不兼容. thinkPHP6.0以下版本只能使用 captcha2.0以下版本,不支持2.0版 ...
- Docker学习1
命名空间(Namesaoces):Linux内核提供的一种对进程资源隔离的机制,例如网络.进程.挂载点等资源. 控制组(CGroups):Linux内核提供的一种限制进程资源的机制:例如CPU.内存等 ...
- Buy a Ticket CodeForces - 938D (dijkstra)
大意: n节点无向图, 点$i$到点$j$的花费为$2dis(i,j)+a[j]$, 对于每个点, 求最少花费. 每条边权翻倍, 源点S向所有点$i$连边, 权为$a[i]$, 答案就为$S$到每个点 ...
- 使用TableSnapshotInputFormat读取Hbase快照数据
根据快照名称读取hbase快照中的数据,在网上查了好多资料,很少有资料能够给出清晰的方案,根据自己的摸索终于实现,现将代码贴出,希望能给大家有所帮助: public void read(org.apa ...
- DIj
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace DefineG ...
- git 常用命令语句(个人笔记)
切换账户 git config user.name xxxxx 查看用户名 ex: git config user.name tongjiaojiao git config user.e ...
- 109、Secret的使用场景 (Swarm16)
参考https://www.cnblogs.com/CloudMan6/p/8082429.html 我们可以用secret管理任何敏感数据.这些敏感数据是容器在运行时需要的.同时我们又不想把这些 ...