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 ...
随机推荐
- A Complete Guide to the <Picture> Element
If you’ve ever struggled building responsive websites, this post is for you. It’s part of a series o ...
- 生成CSV文件后再将CSV文件导入到mysql
1.生成CSV jar包:http://pan.baidu.com/s/1xIL26 String csvFilePath = "d:\\test.csv"; CsvWriter ...
- 【BZOJ】1901: Zju2112 Dynamic Rankings(区间第k小+树状数组套主席树)
http://www.lydsy.com/JudgeOnline/problem.php?id=1901 首先还是吐槽时间,我在zoj交无限tle啊!!!!!!!!我一直以为是程序错了啊啊啊啊啊啊. ...
- COJ978 WZJ的数据结构(负二十二)
试题描述 输入两个正整数N.K,以及N个整数Ai,求第K小数. 输入 第一行为两个正整数N.K.第二行为N个正整数Ai. 输出 输出第K小数. 输入示例 5 41 2 3 3 5 输出示例 3 其他说 ...
- JS function的参数问题
1.当传入的参数个数小于声明的参数个数时,缺少的参数值就是:undefined 类似方法重载 var f1 = function(p1,p2,p3){ switch(arguments.len ...
- github配置
注册github账号: 准备秘钥文件: 认证: https://github.com 测试秘钥: 创建仓库: 执行下面命令创建git远程仓库: 添加一个two.txt文件:
- Css3 - 全面学习
css3实验.生成.学习网站 http://www.css3maker.com/ http://www.css3.me/ 查询前缀和兼容性 http://caniuse.com/ 1.文本阴影 < ...
- ORACLE SEQUENCE用法
引用自: http://www.cnblogs.com/hyzhou/archive/2012/04/12/2444158.html 在oracle中sequence就是序号,每次取的时候它会自动增加 ...
- UVALive 2635 匈牙利算法
题意 给出k块地 规模n*m 需要在每块地中找至多一块h*w的地 这些地中如果包含字母 只能包含一种字母 如果一块地中选地使用了A 其余的地就不能使用A 但是全0可以重复 问 最后能最多选出来多少块地 ...
- DS实验题 Inversion
题目: 解题过程: 第一次做这题的时候,很自然的想到了冒泡和选择,我交的代码是用选择写的.基本全WA(摊手). 贴上第一次的代码: // // main.cpp // sequenceschange ...