HDU-5532(LIS-nlogn)


思路:
解法一:
新的认识get+1,对于一个数组,可以通过记录他'<'和'>'来判断数组的升降序状态,这种方法只需要n的复杂度就可以解决问题,需要注意的一点是,每次删除一个结点在消失两个关系的同时也会出现一个新的关系
解法二:找到非递减和非递增LIS中数量较大的一个,只要它大于等于n-1,答案就是YES,不然就是NO,由于卡时间,故要用nlogn算法
方法一:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; int T;
int n,num[];
int big,small;
int flag; int judge(int x)
{
int tsmall = small;
int tbig = big;
if(x == ) {
if(num[x] < num[x+]) tsmall--;
if(num[x] > num[x+]) tbig--;
if(!tbig||!tsmall) return ;
else return ;
}
else if(x == n) {
if(num[x-] < num[x]) tsmall--;
if(num[x-] > num[x]) tbig--;
if(!tbig||!tsmall) return ;
else return ;
}
else {
if(num[x-]>num[x]&&num[x]>num[x+]) tbig -= ;
if(num[x-]<num[x]&&num[x]<num[x+]) tsmall -= ;
if(num[x-]>num[x]&&num[x]<num[x+]||num[x-]<num[x]&&num[x]>num[x+]) {
tbig --;
tsmall --;
}
if(num[x-] < num[x+]) tsmall++;
if(num[x-] > num[x+]) tbig++;
if(!tbig||!tsmall) returl ;
else return ;
}
} int main()
{
scanf("%d",&T);
while(T--)
{
big = small = ;
flag = ;
scanf("%d",&n);
for(int i = ;i <= n;i++) {
scanf("%d",&num[i]);
if(i >= ) {
if(num[i] > num[i-]) small++;
if(num[i] < num[i-]) big++;
}
}
if(n==||!big||!small) {
printf("YES\n");
continue;
}
else {
for(int i = ;i <= n;i++)
if(judge(i)){
flag = ;
break;
}
if(flag) printf("YES\n");
else printf("NO\n");
}
}
return ;
}
方法二:
#include <iostream>
#include <cstdio>
#include <cstring>
#define INF 0x7fffffff
using namespace std; int T;
int n,num[];
int dp1[],dp2[];
int B1[],B2[];
int flag; int max(int a,int b) {
return a>b?a:b;
} int find(int* B,int goal,int r)
{
int l = ;
while(l <= r)
{
int mid = (l+r)/;
if(B[mid] < goal)
l = mid+;
else if(B[mid] > goal)
r = mid-;
else return mid;
}
return l;
} int main()
{
scanf("%d",&T);
while(T--)
{
int len1,len2;
int ans1 = ,ans = ,ans2 = ;
scanf("%d",&n);
for(int i = ;i <= n;i++) {
scanf("%d",&num[i]);
dp1[i] = dp2[i] = ;
}
B1[] = B2[] = num[];
len1 = len2 = ;
for(int i = ;i <= n;i++) {
if(num[i] >= B1[len1]) B1[++len1] = num[i];
else {
int pos = find(B1,num[i],len1);
B1[pos] = num[i];
}
if(num[i] <= B2[len2]) B2[++len2] = num[i];
else {
int pos = find(B2,num[i],len2);
B2[pos] = num[i];
}
}
ans = max(len1,len2);
if(ans >= n-) printf("YES\n");
else printf("NO\n");
}
return ;
}
We
say an array is sorted if its elements are in non-decreasing order or
non-increasing order. We say an array is almost sorted if we can remove
exactly one element from it, and the remaining array is sorted. Now you
are given an array a1,a2,…,an, is it almost sorted?
1≤T≤2000
2≤n≤105
1≤ai≤105
There are at most 20 test cases with n>1000.
3
2 1 7
3
3 2 1
5
3 1 4 1 5
YES
NO
HDU-5532(LIS-nlogn)的更多相关文章
- hdu 5532 (LIS) Almost Sorted Array
http://acm.hdu.edu.cn/showproblem.php?pid=5532 题意大致是一组数中去掉一个数后问剩下的数是否构成非严格单调序列 正反各跑一遍最长非严格连续子序列,存在长度 ...
- HDU 1950 LIS(nlogn)
Bridging signals Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Constructing Roads In JGShining's Kingdom(HDU 1025 LIS nlogn方法)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- hdu 5532 Almost Sorted Array nlogn 的最长非严格单调子序列
Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- HDU 1025 LIS二分优化
题目链接: acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Time Limit: ...
- LIS(nlogn) POJ 3903 Stock Exchange
题目传送门 题意:LIS最长递增子序列 O(nlogn) 分析:设当前最长递增子序列为len,考虑元素a[i]; 若d[len]<a[i],则len++,并使d[len]=a[i]; 否则,在d ...
- HDU 1025 (LIS+二分) Constructing Roads In JGShining's Kingdom
这是最大上升子序列的变形,可并没有LIS那么简单. 需要用到二分查找来优化. 看了别人的代码,给人一种虽不明但觉厉的赶脚 直接复制粘贴了,嘿嘿 原文链接: http://blog.csdn.net/i ...
- hdu 5532 Almost Sorted Array
http://acm.hdu.edu.cn/showproblem.php?pid=5532 题目大意: 给你一个不规则的序列,问是否能够通过删除一个元素使其成为一个有序的序列(递增或递减(其中相邻 ...
- Super Jumping! Jumping! Jumping!(hdu 1087 LIS变形)
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- HDU 1950(LIS)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1950 Bridging signals Time Limit: 5000/1000 MS (Java ...
随机推荐
- STL之Iterator(迭代器)
概述 根据迭代器功能的不同,将迭代器分为以下几类: Iterator Category Ability Providers Input iterator Reads forward istream O ...
- yii console.php 报错 Property "CConsoleApplication.theme" is not defined.
默认配置的话,是不会出现这个错误的,应该是有人为修改了 yiic.php 这个文件,本来是 $config 载入的应该是 console.php ,人为修改后载入了 main.php 这个配置文件了 ...
- Adding Swap Files
Adding Swap Files If you do not have free disk space to create a swap partition and you do need to a ...
- C# ashx生成的验证码
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "image/jpeg&qu ...
- svg学习笔记
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- JS类百度的动态提示框思路及完成
参考的代码来自这里: http://www.jb51.net/article/28075.htm 不过说实话,这个网站太烂了,不适合看代码,另外写代码的人是个大牛,但是却没有模块化思想,所以朕不高兴直 ...
- 关于导出oracle多个表的建表语句DLL,生成.sql语句。
--('TABLE','LINE','ODS_XX')这里面的表和用户都需要大写.如果表名用户名不大写会报这个错误:对象 "emp" 属于类型 TABLE, 在方案 "s ...
- Java实现断点下载Demo
//1.声明URL String path="http://localhost:8080/day22_DownLoad/file/a.rmvb"; URL url=new URL( ...
- oracle中char,vchar,vchar2的区别与联系
1.char是固定长度的,例如你定义某一列类型为char(20).那么即便你插入"abcde"5个字节,数据库也会在后面自动加上15个字节(可能为空格),以补足二十个字节. 2.v ...
- 兄弟连面试宝典php
学历这个事情是企业招聘经常设置的一道门槛,我们不能说学历高就能力高,也不能说学历低能力就差,那如何辩证回答这个问题呢?回答提示:学历不一定完全代表能力,虽然我的学历不够硬但是我会在技术上更努力更认真, ...