백준공부/java

[백준] 14002번 가장 긴 증가하는 부분 수열 4 (gold 4

gomduri43 2023. 7. 10. 00:51

import java.io.*;
import java.util.*;

class Point{
    int value;
    int length;
    String word;
    public Point(int value, int length, String word){
        this.value=value;
        this.length=length;
        this.word=word;
    }
}
public class Main{
    public static void main (String[] args) throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));

        int n=Integer.parseInt(br.readLine());
        Point[] num=new Point[n];
        StringTokenizer st=new StringTokenizer(br.readLine());
        for(int i=0; i<n; i++){
            num[i]=new Point(Integer.parseInt(st.nextToken()),0,"");
        }
        int max=0;
        String word="";
        for(int i=0; i<n; i++){
            Point temp=num[i];
            temp.length++;
            temp.word=temp.word.concat(String.valueOf(temp.value)+" ");

            if(max < temp.length){
                max=temp.length;
                word=temp.word;
            }

            for(int j=i+1; j<n; j++){
                if(temp.value<num[j].value && temp.length > num[j].length){
                    num[j].length=temp.length;
                    num[j].word=temp.word;

                }
            }
        }
        bw.write(max+"\n"+word);
        bw.flush();
    }
}