HDU 5532 Almost Sorted Array(最長非遞減子序列 模板題)——2015ACM/ICPC亞洲區長春站
Almost Sorted Array
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)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 a
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.
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 /*********************************************************************/
題意:定義Almost Sorted Array是一個[去掉序列中的一個數,剩下的數滿足單調非遞減或者單調非遞增的]序列,給出一個序列問它是否是Almost Sorted Array。
解題思路:純粹的模板題,只要你有O(nlogn)複雜度的LIS(最長上升子序列)模板在手,那就不用愁了,沒有的還是儘早弄一個吧
至於非遞增嘛,把輸入的序列反過來儲存不就又是求LIS了^_^,機智如我
菜鳥成長記#pragma comment(linker, "/STACK:1024000000,1024000000") #include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> #include<stack> #include<math.h> #include<vector> #include<map> #include<set> #include<stdlib.h> #include<cmath> #include<string> #include<algorithm> #include<iostream> #define exp 1e-10 using namespace std; const int N = 100005; const int M = 2010; const int inf = 2147483647; const int mod = 2009; int a[N],b[N], f[N], d[N]; // f[i]用於記錄 a[0...i]的最大長度 int bsearch(const int *f, int size, const int &a) { int l=0, r=size-1; while( l <= r ) { int mid = (l+r)>>1; if( a >= d[mid-1] && a < d[mid] ) return mid; // >&&<= 換為: >= && < else if( a <d[mid] ) r = mid-1; else l = mid+1; } } int LIS(const int *a, const int &n) { int i, j, size = 1; d[0] = a[0]; f[0] = 1; for( i=1; i < n; ++i ) { if( a[i] < d[0] ) // <= 換為: < j = 0; else if( a[i] >= d[size-1] ) // > 換為: >= j = size++; else j = bsearch(d, size, a[i]); d[j] = a[i]; f[i] = j+1; } return size; } int main() { int t,i, n; scanf("%d",&t); while(t--) { memset(f,0,sizeof(f)); memset(d,0,sizeof(d)); scanf("%d",&n); for( i=0; i < n; ++i ) { scanf("%d", &a[i]); b[n-i-1]=a[i]; } if(LIS(a, n)>=n-1||LIS(b,n)>=n-1) puts("YES"); else puts("NO"); //printf("%d\n",);// 求最大遞增/上升子序列(如果為最大非降子序列,只需把上面的註釋部分給與替換) } return 0; }
相關推薦
HDU 5532 Almost Sorted Array(最長非遞減子序列 模板題)——2015ACM/ICPC亞洲區長春站
Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Problem Description We are
hdu 1025(最長非遞減子序列的n*log(n)求法)
經典題。。。最長非遞減序列的n*log(n)求法。。。orz... View Code 1 #include<iostream> 2 const int N=500007; 3
【Effect CodeForces - 270D】Greenhouse (思維,最長非遞減子序列,對偶問題,考慮反面)
題幹: Emuskald is an avid horticulturist and owns the world's longest greenhouse — it is effectively infinite in length. Over the years Emuskald h
最長非遞減子序列的應用
Description 我們有一個數列A1,A2...An,你現在要求修改數量最少的元素,使得這個數列嚴格遞增。其中無論是修改前還是修改後,每個元素都必須是整數。 請輸出最少需要修改多少個
HDU 5532 Almost Sorted Array(最長不下降子序列nlogn模板)
We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, selection sort, bubble sort, etc. But s
hdu 5532【最長非遞增子序列 時間復雜度 nlogn】
int urn 思路 while 最小值 復雜 include hdu nlogn http://acm.split.hdu.edu.cn/showproblem.php?pid=5532 題意:由n個數組成的序列,如果去掉一個數後仍保持非遞增或者非遞減,則輸出YES,否
HDU5532 Almost Sorted Array(最長上升子序列 or 瞎搞個做差的數組)
algo problem 刪除 給定 子序列 中間 OS blank mes 題目鏈接:點我 題意:給定一個序列,詢問是否能刪除一個數讓它成為非遞減或者非遞增的序列。 比如說 刪除後的序列是1 3 3 5 或者5 3 3 1 或者1 3 5 或者5 3 1 都可以。只
hdu 5532 Almost Sorted Array (水題)
opened familiar mat src cep sed lin n) rst Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (J
hdu5532 Almost Sorted Array--最長上升子序列
一:分析 求兩次最長上升/下降子序列即可。 二:AC程式碼 #define _CRT_SECURE_NO_DEPRECATE #include<iostream> #inc
最長非下降子序列
最長非下降?就是要麼上升要麼不變唄。實現?這裡只講O(nlogn)的最長非下降實現: 先看最長上升,二分優化的基礎在於我們分解問題的方式是用dp[i]表示長度為i的序列的最小的第i號元素,只要讓dp[i]最小,那麼後序可選擇加進來的數的數量就會變多。 非下降就是加了個新的要求:與結尾元素相等
CodeForces - 340D - Bubble Sort Graph(最長非降子序列)
Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, …, an in ascending order. He is bo
[LeetCode] Longest Uncommon Subsequence I 最長非共同子序列之一
Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined
[LeetCode] Longest Uncommon Subsequence II 最長非共同子序列之二
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequ
動態規劃03—最長非降子序列的長度(LIS)
最長非降子序列問題:longest increasing subsequence 是一個稍微複雜一點地動態規劃問題,給定一個數組array[N],求最大的非降子序列的長度。同樣,該問題可以先求解array[i],其中i小於N。 同樣,該動態規劃問題中的狀態和
hdu 5256 最長不遞減子序列(二分查詢)
分析:因為要改變最少的數,使得陣列a,成為嚴格遞增,也就是使得a[i+1]>=a[i]+1,即a[i+1]-(i+1)>=a[i]-i。所以令a[i]=a[i]-1,原題就變成了改變最少的
最長非降子序列模型
1)首先最長單調非增子序列(一維) 描述: 給定一整型數列{a1,a2...,an}(0<n<=100000),找出單調遞增最長子序列,並求出其長度。 如:1 9 10 5 11 2 13的最長單調遞增子序列是1 9 10 11 13,長度為5。 題目連結
PAT 1045 Favorite Color Stripe(最長不遞減子序列)
Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting
淺析最長不下降子序列一題的方法
對於最長不下降子序列一題,問題描述如下: 有長度為N的序列: A1 A2 …..An 求最長不下降子序列:Ai1,Ai2,,,,,Aik, 其中ai1<=ai2<=.....<=ai
hdoj 5532 Almost Sorted Array 【LIS】
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 o
HDU 3068 最長回文(manacher模板題)
style hdu log pre using ret algo names print 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 題目大意:求字符串s中最長的回文子串 解題思路:manacher模板 代