View

๋ฐ˜์‘ํ˜•

[๋ฌธ์ œ]

์ฃผ์–ด์ง„ ๋‘ ์ˆ˜์—ด์˜ ์ตœ์žฅ ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด(Longest Increasing Subsequence)์˜ ๊ธธ์ด๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ˆ˜์—ด { A1, A2, ... , AN }์˜ ์ตœ์žฅ ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด B๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋œ๋‹ค.

{ B1, B2, ... , BK }์—์„œ 0≤K≤N, B1 ≤ B2 ≤ ... ≤ BK์ด๊ณ ,

AB1 ≤ AB2 ≤ ... ≤ ABK์ธ ์ตœ๋Œ€ K๋กœ ๊ตฌ์„ฑ๋œ ์ˆ˜์—ด์ด๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์ˆ˜์—ด์ด { 1, 3, 2, 5, 4, 7 } ์ด๋ผ๋ฉด, ์ตœ์žฅ ๋ถ€๋ถ„ ์ฆ๊ฐ€ ์ˆ˜์—ด์˜ ๊ธธ์ด๋Š” 4๊ฐ€ ๋œ๋‹ค.

[์ž…๋ ฅ]

์ฒซ ๋ฒˆ์งธ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ฒซ์งธ ์ค„์—๋Š” ์ˆ˜์—ด์˜ ๊ธธ์ด N(1≤N≤1,000)์ด ์ฃผ์–ด์ง„๋‹ค. 

๋‘˜์งธ ์ค„์—๋Š” ์ˆ˜์—ด์˜ ์›์†Œ N๊ฐœ๊ฐ€ ๊ณต๋ฐฑ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค.

๊ฐ ์ˆ˜์—ด์˜ ์›์†Œ๋Š” 1 ์ด์ƒ 231-1 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค.

[์ถœ๋ ฅ]

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค ‘#T’(T๋Š” ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ๋ฒˆํ˜ธ๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค)๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ์ตœ๋Œ€ ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


[์ž…๋ ฅ]
2
5
1 3 2 5 4 
6
4 2 3 1 5 6
 
 
[์ถœ๋ ฅ]
#1 3
#2 4

 

 


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

public class Solution_3370 {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int T = Integer.parseInt(br.readLine());

		for (int t = 1; t <= T; t++) { // ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค
			int N = Integer.parseInt(br.readLine());
			int[] list = new int[N];
			int[] dp = new int[N];

			int max = 0;
			StringTokenizer st = new StringTokenizer(br.readLine());
			for (int i = 0; i < N; i++) {
				list[i] = Integer.parseInt(st.nextToken());
			}

			dp[0] = 1;

			for (int i = 1; i < N; i++) {
				dp[i] = 1; // ๋ชจ๋‘ ํ•œ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ์žˆ์Œ (์ž๊ธฐ์ž์‹ )
//				System.out.println(Arrays.toString(list));
//				System.out.println(Arrays.toString(dp));
				for (int pre = 0; pre < i; pre++) { // 4 2 3
					if (list[i] > list[pre] && dp[pre] + 1 > dp[i])
						dp[i] = dp[pre] + 1;
				}
				max = Math.max(max, dp[i]); // ์ง€๊ธˆ๊นŒ์ง€์˜ max๋ž‘ ํ˜„์žฌ๊นŒ์ง€์˜ ๊ฐ’๊ณผ ๋น„๊ต
			}
			System.out.println("#" + t + " " + max);
		}
	}
}

https://dheldh77.tistory.com/entry/%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-%EC%B5%9C%EC%9E%A5-%EC%A6%9D%EA%B0%80-%EC%88%98%EC%97%B4LIS-Longest-Increasing-Subsequence

 

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ์ตœ์žฅ ์ฆ๊ฐ€ ์ˆ˜์—ด(LIS, Longest Increasing Subsequence)

์ตœ์žฅ ์ฆ๊ฐ€ ์ˆ˜์—ด(LIS, Longest Increasing Subsequence) ์ตœ์žฅ ์ฆ๊ฐ€ ์ˆ˜์—ด์ด๋ž€ ์–ด๋–ค ์ˆ˜์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ตœ์žฅ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด์„ ๋งํ•œ๋‹ค. ์ฆ‰, 1, 2, 3, ..., i, i+1, i+2, ..., n๊นŒ์ง€์˜ ์ˆ˜์—ด a[n]์ด ์ฃผ์–ด์กŒ์„..

dheldh77.tistory.com

 

๋ฐ˜์‘ํ˜•
Share Link
reply
๋ฐ˜์‘ํ˜•
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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 31