You will be fine

<Algorithm> 82. 3745번 오름세

by BFine
반응형

1. 3745번 오름세

  • 배열이 주어지면 그 배열의 증가하는 부분 수열의 길이를 구하는 문제

  • O(N^2)에 경우는 쉽게 이해했지만 N log(N) 푸는것은 쉽지 않았다.

  • 이 알고리즘은 LIS(Longest increasing subsequence) 이다.

  • 주어진 input 값이 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
 
public class Main {
    static int N, Answer;
    static int [] arr;
    static int [] order;
    
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        
        Scanner sc = new Scanner(System.in);
        
        while(sc.hasNext()){
            N = sc.nextInt();
            arr = new int [N];
            order = new int[N];
            Arrays.fill(order, 1);
            
            for(int i=0; i<N; i++){
                arr[i] = sc.nextInt();
            }
            /*
            for(int i = 1 ; i < N; i++) {
                for(int j = 0; j < i; j++) {
                    if(arr[i] > arr[j] && order[j]+1 > order[i]) {
                        order[i] = order[j] + 1;
                    }
                }
            }
*/            /*Arrays.sort(order);
            System.out.println(order[N-1]);*/
            
            
            int T[] = new int[N];
            int T_val[] = new int[N];
            int R[] = new int[N];
            
            Arrays.fill(R, -1);
            
            T_val[0= arr[0];
            int len = 0;
            
            for(int i = 1; i < N ; i++) {
                /*System.out.println("들어온다 : " + arr[i]);
                System.out.println("현재 T_val "+Arrays.toString(T_val));
                System.out.println("len : "+ len);
                System.out.println();
                */if(T_val[len] < arr[i]) {
                    //System.out.println("붙인다 : tval = " +T_val[len]+" arr = "+arr[i]);
                    T[len] = i;
                    len++;
                    T_val[len] = arr[i]; 
                    /*System.out.println("T_val 추가 ~ "+Arrays.toString(T_val));
                    System.out.println("len : "+ len);
                    System.out.println();*/
                }else {
                    int index =lowerBound(T_val, arr[i], 0, len);
                    T_val[index] = arr[i];
                    T[index] = i;
                    /*System.out.println("len : "+ len);
                    System.out.println("추출 index : "+index);
                    System.out.println("T_val 변화 ~ "+Arrays.toString(T_val));
                    System.out.println();*/
                }
            }
            
            /*System.out.println(Arrays.toString(T_val));
        */    System.out.println(len+1);
        }
        
    }
    public static int lowerBound(int[] array, int key, int start, int end) {
        if(start >= end) {
            if(start - != -1)
                return start;
            else
                return 0;
        }
        
        // 4 7 10
        // end = 2
        // start = 0
        // mid = 1;
        // key 10;
        //  10 < 7  false
        // start = 2 end = 2
        int mid = (start+end)/2;
        
        if(key <= array[mid]) {
            return lowerBound(array, key, start, mid);
        }else {
            return lowerBound(array, key, mid+1, end);
        }
        
        
    }
}
    
    /*public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        
        while(true) {
            String str = br.readLine();
            if(str.equals("")) {break;}
            int N = Integer.parseInt(str);
            int arr[] = new int[N];
            int order[] = new int[N];
            Arrays.fill(order, 1);
            StringTokenizer st = new StringTokenizer(br.readLine());
            
            for(int i = 0; i < N; i++) {
                arr[i] = Integer.parseInt(st.nextToken());
            }
            
            for(int i = 1 ; i < N; i++) {
                for(int j = 0; j < i; j++) {
                    if(arr[i] > arr[j] && order[j]+1 > order[i]) {
                        order[i] = order[j] + 1;
                    }
                }
            }
            Arrays.sort(order);
            System.out.println(order[N-1]);
        }
}
    }*/
 
cs

참고 & 출처  http://stack07142.tistory.com/56

반응형

블로그의 정보

57개월 BackEnd

BFine

활동하기