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 ...
随机推荐
- nginx+keepalived实现nginx双主高可用的负载均衡
http://kling.blog.51cto.com/3320545/1253474 一.前言: 在互联网上面,网站为用户提供原始的内容访问,同时为用户提供交互操作.提供稳定可靠的服务,可以给用户带 ...
- [CSS3] CSS Media Queries
Using CSS media queries allows you to design responsive layout in your web apps and website. We will ...
- [RxJS] Creation operators: empty, never, throw
This lesson introduces operators empty(), never(), and throw(), which despite being plain and void o ...
- C# 网络编程之网页简单下载实现
这是根据<C#网络编程实例教程>中学到的知识实现的一个C#网页简单下载器,其中涉及到的知识主要是HTTP协议编程中相关类:HttpWebRequest类.HttpWebResponse类. ...
- 【转】Android开源项目 分类 便于查看
之前转载了一个开源项目的文章,发现那些都是没有系统的总结,这里又转载一篇有系统总结的文章. Android开源项目系列汇总已完成,包括: Android开源项目第一篇——个性化控件(View)篇 An ...
- 分析器错误消息: 未能加载类型“WebApplication._Default”
1.新建一个空白解决方案2.新闻一个Web Application项目 默认就有Default.aspx 直接调试的时候出现-------------------------------------- ...
- Linux文件和目录操作管理命令
1.pwd:显示工作目录路径 -p:显示实际物理路径 -l:显示链接路径 2.cd:更改工作目录路径 cd:进入用户主目录 cd~:进入用户主目录 cd-:返回进入此目录之前所在的目录 cd..:返回 ...
- Eclipse自动提示功能
一般默认情况下,Eclipse的代码提示功能是比MicrosoftVisualStudio的差很多的,主要是Eclipse本身有很多选项是默认关闭的,要开发者自己去手动配置.如果开发者不清楚的话,就不 ...
- MyEclipse内存不足的问题
今早打开MyEclipse莫名其妙弹出提示框,然后我各种搜索,用了网上能查到的各种办法去试图解决问题,方法包括但不限于 修改eclipse.ini .设置Default VM Arguments . ...
- zabbix 通过smtp 邮件报警
注:sendemail 不是sendmail....sendemail是用perl语言写的一个smtp发邮件的小程序....详情可自行查阅..... 1. media 用户配置下的media. Adm ...