本文主要是介绍POJ1631最长单调递增子序列,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最长单调递增子序列
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.StringTokenizer;public class Main {public static void main(String[] args) {new Task().sovle();}
}class Task {InputReader in = new InputReader(System.in);PrintWriter out = new PrintWriter(System.out);void sovle() {int t = in.nextInt() ;while(t-- > 0){int n = in.nextInt() ;int[] a = new int[n] ;for(int i = 0 ; i < n ; i++) a[i] = in.nextInt() ;int[] stack = new int[n] ;int top = -1 ;stack[++top] = a[0] ;for(int i = 1 ; i < n ; i++){if(a[i] > stack[top]) stack[++top] = a[i] ;else{int l = 0 , r = top , idx = -1 ;while(l <= r){int mid = (l + r) >> 1 ;if(stack[mid] >= a[i]){r = mid - 1 ;idx = mid ;}else l = mid + 1 ;}stack[idx] = a[i] ;}}out.println(top+1) ;}out.flush() ;}
}class InputReader {public BufferedReader reader;public StringTokenizer tokenizer;public InputReader(InputStream stream) {reader = new BufferedReader(new InputStreamReader(stream), 32768);tokenizer = new StringTokenizer("");}private void eat(String s) {tokenizer = new StringTokenizer(s);}public String nextLine() {try {return reader.readLine();} catch (Exception e) {return null;}}public boolean hasNext() {while (!tokenizer.hasMoreTokens()) {String s = nextLine();if (s == null)return false;eat(s);}return true;}public String next() {hasNext();return tokenizer.nextToken();}public int nextInt() {return Integer.parseInt(next());}public long nextLong() {return Long.parseLong(next());}public double nextDouble() {return Double.parseDouble(next());}public BigInteger nextBigInteger() {return new BigInteger(next());}}
这篇关于POJ1631最长单调递增子序列的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!