Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

코드와이

[SW Expert Academy] 3307. 최장 증가 부분 수열 본문

SW_Expert

[SW Expert Academy] 3307. 최장 증가 부분 수열

코드와이 2021. 3. 25. 17:25

 

DP

문제링크

swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWBOKg-a6l0DFAWr

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

package D3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class 최장_증가_부분_수열 {

	static int[][] arr;
	static ArrayList<Integer> update;
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		StringBuilder sb = new StringBuilder();
		
		int T = Integer.parseInt(br.readLine());
		
		for(int tc = 1 ; tc <= T ; tc++) {
			sb.append("#").append(tc).append(" ");
			
			int n = Integer.parseInt(br.readLine());
			arr = new int[2][n+1];
			update = new ArrayList<Integer>();
			update.add(0);
			
			st = new StringTokenizer(br.readLine());
			for(int i = 1 ; i <= n ; i++) {
				arr[0][i] = Integer.parseInt(st.nextToken());
				
				if(arr[0][i] > update.get(update.size()-1)) {
					update.add(arr[0][i]); 
				} else {
					int idx = BinaryCheck(0, update.size(), arr[0][i]);
					update.set(idx, arr[0][i]);
				}
			}
			sb.append(update.size() - 1).append("\n");
		}
		sb.setLength(sb.length()-1);
		System.out.println(sb);
	}

	private static int BinaryCheck(int l, int r, int x) {
		
		int mid = (l + r) / 2;
		
		while(l < r) {
			
			if( update.get(mid) < x) {
				l = mid + 1;
			} else {
				r = mid;
			}
			mid = (l + r) / 2;
		}
		
		return mid;
	}
}