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 ...
随机推荐
- Flexbox——快速布局神器
Flexbox通常能让我们更好的操作他的子元素布局,例如: 如果元素容器没有足够的空间,我们无需计算每个元素的宽度,就可以设置他们在同一行: 可以快速让他们布局在一列: 可以方便让他们对齐容器的左.右 ...
- css3中的几何图形shape研究
前言 估计大家在日常工作中都会用到css形状,但是目前天朝中使用到最多的估计就是圆(circle).椭圆(ellipse).各种三角形形状,但是你肯定很少看见过用几何图形或者多边图形.假如你不懂什么叫 ...
- 【BZOJ】1069: [SCOI2007]最大土地面积(凸包+旋转卡壳)
http://www.lydsy.com/JudgeOnline/problem.php?id=1069 显然这四个点在凸包上,然后枚举两个点找上下最大的三角形即可. 找三角形表示只想到三分QAQ.. ...
- TC SRM 584 DIV 2
第一次在DIV2 AK了. 250水题. 500,FLoyd搞出所有边的最短路,然后找最短路,中最长的,如果有不连通的边返回-1 1000,组合DP,各种慌乱,在最后1分钟时,交上了,感觉很棒,最后还 ...
- BZOJ3444: 最后的晚餐
Description [问题背景] 高三的学长们就要离开学校,各奔东西了.某班n人在举行最后的离别晚餐时,饭店老板觉得十分纠结.因为有m名学生偷偷找他,要求和自己暗恋的同学坐在一起. [问题描述] ...
- winform学习2-datagridview数据绑定
1.datagridview.clearSelection()清除默认的选中项 2.列数据显示,首先列必须是显示状态, 3.布局-单元格内文字内容居中显示,示例:外观-defaultCellStyle ...
- iOS真机调试的验证项
基本的匹配项 App 描述文件 BundleID (匹配) ApplicationID Cert (匹配) Cert DeviceID (被包含) DeviceIDs 描述文件就是包含了各种需要验证信 ...
- [百科] - iLBC
iLBC是一种专为包交换网络通信设计的编解码,优于目前流行的G.729.G.723.1,对丢包进行了特有处理,即使在丢包率相当高的网络环境下,仍可获得非常清晰的语音效果. 30ms ptime的iLB ...
- IMAP命令
IMAP命令学习1.CREATE <folder>CREATE可以创建指定名字的新邮箱.邮箱名称通常是带路径的文件夹全名.(有些IMAP客户机使用邮件夹称呼新邮箱)C: A003 CREA ...
- JavaWEB中读取配置信息
第一种方法是使用java.io和java.util包,缺点是路径的概念要清晰, 例子: Properties prop = new Properties(); InputStream in = get ...