HDU-5532(LIS-nlogn)
来源:互联网 发布:三毛淘宝小号批发 编辑:程序博客网 时间:2024/06/05 00:59
思路:
解法一:
新的认识get+1,对于一个数组,可以通过记录他'<'和'>'来判断数组的升降序状态,这种方法只需要n的复杂度就可以解决问题,需要注意的一点是,每次删除一个结点在消失两个关系的同时也会出现一个新的关系
解法二:找到非递减和非递增LIS中数量较大的一个,只要它大于等于n-1,答案就是YES,不然就是NO,由于卡时间,故要用nlogn算法
方法一:
#include <iostream>#include <cstdio>#include <algorithm>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]) tsmall -= 2; if(num[x-1]>num[x]&&num[x]<num[x+1]||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 <iostream>#include <cstdio>#include <cstring>#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?
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.
1≤T≤2000
2≤n≤105
1≤ai≤105
There are at most 20 test cases with n>1000.
1≤T≤2000
2≤n≤105
1≤ai≤105
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
332 1 733 2 153 1 4 1 5
Sample Output
YESYESNO
0 0
- HDU-5532(LIS-nlogn)
- hdu 1025 LIS nlogn复杂度
- hdu 1950 Bridging signals【LIS nlogn】
- HDU 1950 Bridging signals(LIS nlogn)
- HDU 5748 Bellovin(LIS nlogn)
- HDU-1950 Bridging signals (nlogn求LIS)
- lis nlogn
- HDU 1950 Bridging signals(O(nlogn)算法LIS)
- hdu 5773 The All-purpose Zero 思维转化+nlogn LIS
- HDU 1025 Constructing Roads In JGShining's Kingdom(LIS nlogn)
- 【HDU】5141 LIS again 在普通nlogn求LIS的基础上稍作修改
- LIS nlogn写法
- LIS (nlogn解法)
- Lis的nlogn算法
- UVA 10354 nlogn LIS
- uva 10635 nlogn LIS
- LIS O(nlogn)
- poj 1631 LIS nlogn
- lintcode ----二分查找
- mysql 常见命令
- HDU-1176(基础方程DP)
- Akka/play(activator) 2.5.3 创建工程 2
- HDU-5504(逻辑if-else大水题)
- HDU-5532(LIS-nlogn)
- support包下的AlertDialog
- HDU-1078
- Ubuntu命令小抄
- 沉浸式状态栏的设定-com.readystatesoftware.systembartint:systembartint 的使用
- New Year Permutation(Floyd+并查集)
- POJ-1488(字符串应用)
- CF-599B - Spongebob and Joke
- iOS录音功能