Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
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
Tags
more
Archives
Today
Total
관리 메뉴

코드와이

[BAEKJOON] 14002. 가장 큰 증가하는 부분수열 4 본문

acmicpc

[BAEKJOON] 14002. 가장 큰 증가하는 부분수열 4

코드와이 2021. 8. 26. 18:17

 

문제링크

https://www.acmicpc.net/problem/14002

 

14002번: 가장 긴 증가하는 부분 수열 4

수열 A가 주어졌을 때, 가장 긴 증가하는 부분 수열을 구하는 프로그램을 작성하시오. 예를 들어, 수열 A = {10, 20, 10, 30, 20, 50} 인 경우에 가장 긴 증가하는 부분 수열은 A = {10, 20, 10, 30, 20, 50} 이

www.acmicpc.net

 

가장 아래에 반례 3개를 넣어놨습니다.

제가 겪었던 오류들을 해결했던 반례입니다.

package acmicpc.Gold4;

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

public class 가장_긴_증가하는_부분_수열4 {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		StringBuilder sb = new StringBuilder();
		
		int n = Integer.parseInt(br.readLine());
		st = new StringTokenizer(br.readLine());
		int[] arr = new int[n + 1];

		for(int i = 0 ; i < n ; i++) {
			arr[i + 1] = Integer.parseInt(st.nextToken());
		}
		
		if(n == 1) {
			System.out.println(1 + "\n" + arr[1]);
		} else {
			
			int[] cnt = new int[n + 1];			// 현재 index 에서 가장 긴 증가하는 부분 수열 길이를 저장하는 배열
			int[] idx = new int[n + 1];			// 가장 긴 증가하는 부분수열을 만들기 위한 바로 전 index 값을 저장하는 배열
			int ans = 1;					// 가장 긴 증가하는 부분수열 길이
			int index = 1;					// 가장 긴 경우의 마지막 index 값
			for(int i = 1 ; i <= n ; i++) {
				for(int j = 0 ; j < i ; j++) {
					if(arr[j] < arr[i] && cnt[j] + 1 > cnt[i]) {
						cnt[i] = cnt[j] + 1;
						idx[i] = j;
					}
				}
				
				if(ans < cnt[i]) {
					ans = cnt[i];
					index = i;
				}
			}
			
			sb.append(ans).append("\n");
	
			// 가장 긴 증가하는 부분수열을 넣어줄 array
			int[] newArr = new int[ans];
			int newIdx = ans - 1;

			while(index != 0) {
				
				// 가장 긴 증가하는 부분수열을 구성하는 바로 전 값의 index를 추적해서 newArr에 넣어준다.
				newArr[newIdx--] = arr[index];
				
				// index 값을 최신화
				index = idx[index];
			}
			
			for(int x : newArr) sb.append(x).append(" ");
			
			sb.setLength(sb.length() - 1);
			System.out.println(sb);
		}
	}
}
//반례찾기
//7
//1 6 2 4 5 3 7
//6
//1 3 5 7 2 3
//3
//3 3 1

'acmicpc' 카테고리의 다른 글

[BAEKJOON] 13913. 숨바꼭질4  (0) 2021.08.28
[BAEKJOON] 1963. 소수경로  (0) 2021.08.28
[BAEKJOON] 5052. 전화번호 목록  (0) 2021.08.24
[BAEKJOON] 1715. 카드 정렬하기  (0) 2021.08.22
[BAEKJOON] 15684. 사다리 조작  (0) 2021.08.19