Almost Sorted Array---hdu5532(简单dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5532
题意:问一个含有n个数的序列,删除一个数后是否有序(递增或递减都可以)
我们只要求一下最长上升子序列或者最长下降子序列是否大于等于n-1即可;
时间复杂度n*log(n);
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<set>
using namespace std;
#define met(a, b) memset(a, b, sizeof(a))
#define N 100005
#define INF 0x3f3f3f3f
typedef long long LL; int n, a[N], dp[N]; int solve1()
{
for(int i=; i<=n; i++)
dp[i] = INF;
int ans = ;
for(int i=; i<=n; i++)
{
int pos = upper_bound(dp, dp+n, a[i]) - dp;
dp[pos] = min(dp[pos], a[i]);
ans = max(ans, pos);
}
return ans+;
}
int solve2()
{
for(int i=; i<=n; i++)
dp[i] = INF;
int ans = ;
for(int i=n; i>=; i--)
{
int pos = upper_bound(dp, dp+n, a[i]) - dp;
dp[pos] = min(dp[pos], a[i]);
ans = max(ans, pos);
}
return ans+;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i=; i<=n; i++)
scanf("%d", &a[i]);
int ans1 = solve1();
int ans2 = solve2();
if(ans1 >= n- || ans2 >= n-)
puts("YES");
else
puts("NO");
}
return ;
}
Almost Sorted Array---hdu5532(简单dp)的更多相关文章
- 简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现。
简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现. 题目: Suppose a sorted array is rotated at som ...
- HDU-5532//2015ACM/ICPC亚洲区长春站-重现赛-F - Almost Sorted Array/,哈哈,水一把区域赛的题~~
F - Almost Sorted Array Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- [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 ...
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- [Leetcode][JAVA] Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- 【leetcode】Remove Duplicates from Sorted Array
题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- Java for LeetCode 026 Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
随机推荐
- Codeforces Round #189 (Div. 2) A. Magic Numbers
#include <iostream> #include <vector> #include <algorithm> #include <string> ...
- [Algorithms(Princeton)] Week1 - Percolation
public class Percolation { private boolean[] openSites; private int gridN; private WeightedQuickUnio ...
- java向图片上写字,两个图片合并的方法
package writeimg; import javax.imageio.ImageIO; import java.awt.Color; import java.awt.Font; import ...
- 外部调用JS文件时出现中文乱码的解决办法
若测试网页的编码格式为:gb2312,而调用外部JS文件时出现了乱码(前提是JS文件无错误),则将调用的外部JS文件用记事本打开,然后再保存成编码格式为UTF-8的JS文件即可. 若测试网页的编码格式 ...
- 给NSString增加Java风格的方法
给NSString增加Java风格的方法 文章目录 我实在受不了 NSString 冗长的方法调用了,每次写之前都要查文档.特别是那个去掉前后多余的空格的方法,长得离谱.与之对应的别的语言,拿 jav ...
- 短语密码(blowfish_secret)的设置
简单的说,phpmyadmin就是一种mysql的管理工具,安装该工具后,即可以通过web形式直接管理mysql数据,而不需要通过执行系统命令来管理,非常适合对数据库操作命令不熟悉的数据库管理者,下面 ...
- MySQL数据库管理常用命令
参考: http://blog.linuxeye.com/419.html 安装 利用RPM包安装MySQL 设置TCP 3306端口的iptables root密码管理 设置root用户 ...
- SecureCRT通过console口连接思科设备
- maven--私服的搭建(Nexus的使用)
Nexus常用功能就是:指定私服的中央地址.将自己的Maven项目指定到私服地址.从私服下载中央库的项目索引.从私服仓库下载依赖组件.将第三方项目jar上传到私服供其他项目组使用. 开启Nexus服务 ...
- Angualar:指令大全
指令: 内置渲染指令 内置事件指令 内置节点指令 自定义指令 restrict template replace属性 templateUrl属性 trnsclude priorty terminal属 ...