博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU-5532(LIS-nlogn)
阅读量:6941 次
发布时间:2019-06-27

本文共 3684 字,大约阅读时间需要 12 分钟。


 

思路:

解法一:

新的认识get+1,对于一个数组,可以通过记录他'<'和'>'来判断数组的升降序状态,这种方法只需要n的复杂度就可以解决问题,需要注意的一点是,每次删除一个结点在消失两个关系的同时也会出现一个新的关系

解法二:找到非递减和非递增LIS中数量较大的一个,只要它大于等于n-1,答案就是YES,不然就是NO,由于卡时间,故要用nlogn算法


 

方法一: #include 
#include
#include
using namespace std;int T;int n,num[100007];int big,small;int flag;int judge(int x){ int tsmall = small; int tbig = big; if(x == 1) { if(num[x] < num[x+1]) tsmall--; if(num[x] > num[x+1]) tbig--; if(!tbig||!tsmall) return 1; else return 0; } else if(x == n) { if(num[x-1] < num[x]) tsmall--; if(num[x-1] > num[x]) tbig--; if(!tbig||!tsmall) return 1; else return 0; } else { if(num[x-1]>num[x]&&num[x]>num[x+1]) tbig -= 2; if(num[x-1]
num[x]&&num[x]
num[x+1]) { tbig --; tsmall --; } if(num[x-1] < num[x+1]) tsmall++; if(num[x-1] > num[x+1]) tbig++; if(!tbig||!tsmall) returl 1; else return 0; }}int main(){ scanf("%d",&T); while(T--) { big = small = 0; flag = 0; scanf("%d",&n); for(int i = 1;i <= n;i++) { scanf("%d",&num[i]); if(i >= 2) { if(num[i] > num[i-1]) small++; if(num[i] < num[i-1]) big++; } } if(n==2||!big||!small) { printf("YES\n"); continue; } else { for(int i = 1;i <= n;i++) if(judge(i)){ flag = 1; break; } if(flag) printf("YES\n"); else printf("NO\n"); } } return 0;}

方法二:

#include 
#include
#include
#define INF 0x7fffffffusing namespace std;int T;int n,num[100007];int dp1[100007],dp2[100007];int B1[100007],B2[100007];int flag;int max(int a,int b) { return a>b?a:b;}int find(int* B,int goal,int r){ int l = 1; while(l <= r) { int mid = (l+r)/2; if(B[mid] < goal) l = mid+1; else if(B[mid] > goal) r = mid-1; else return mid; } return l;}int main(){ scanf("%d",&T); while(T--) { int len1,len2; int ans1 = 0,ans = 0,ans2 = 0; scanf("%d",&n); for(int i = 1;i <= n;i++) { scanf("%d",&num[i]); dp1[i] = dp2[i] = 1; } B1[1] = B2[1] = num[1]; len1 = len2 = 1; for(int i = 2;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-1) printf("YES\n"); else printf("NO\n"); } return 0;}

 

 

Problem Description
We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, selection sort, bubble sort, etc. But sometimes it is an overkill to use these algorithms for an almost sorted array.
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?
 

 

Input
The first line contains an integer
T indicating the total number of test cases. Each test case starts with an integer
n in one line, then one line with
n integers
a1,a2,,an.
1T2000
2n105
1ai105
There are at most 20 test cases with
n>1000.
 

 

Output
For each test case, please output "`YES`" if it is almost sorted. Otherwise, output "`NO`" (both without quotes).
 

 

Sample Input
3 3 2 1 7 3 3 2 1 5 3 1 4 1 5
 

 

Sample Output
YES YES NO

转载于:https://www.cnblogs.com/immortal-worm/p/4960153.html

你可能感兴趣的文章
为什么Linux不需要碎片整理?
查看>>
EasyARM i.mx28学习笔记——开箱试用总结
查看>>
ASP怎么解除文件上传200kb限制
查看>>
Xshell选中的同时把内容复制到剪贴板(还可以设置设置文本分隔符)
查看>>
laravel的中间件demo
查看>>
Linux守护进程的编程实现
查看>>
ISCSI工作流程target和initiator
查看>>
Oracle密码过期the password has expired
查看>>
linux grep常用参数
查看>>
button 按钮,结合onclick事件,验证和提交表单
查看>>
<转>python字典排序 关于sort()、reversed()、sorted()
查看>>
java中Token验证
查看>>
医保业务的相关概念
查看>>
【Mac使用系列】【转载】十几个Mac实用工具
查看>>
网易七鱼 Android 高性能日志写入方案
查看>>
微软Visual Studio 2010架构设计功能应用(转)
查看>>
干净的代码是改出来的
查看>>
微软面试题附答案(转)
查看>>
你必须要知道的架构知识~第三章 接口用来制定操作的统一性
查看>>
关于下拉菜单和iframe的问题
查看>>