File tree Expand file tree Collapse file tree 5 files changed +185
-0
lines changed
Expand file tree Collapse file tree 5 files changed +185
-0
lines changed Original file line number Diff line number Diff line change 1+ // [실버2] 2805번. 나무 자르기
2+ // 메모리 : 167844 KB, 시간 : 448 ms
3+
4+ import java .io .*;
5+ import java .util .*;
6+
7+ public class P2805 {
8+ public static void main (String [] args ) throws Exception {
9+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10+ StringTokenizer st = new StringTokenizer (br .readLine ());
11+ int N = Integer .parseInt (st .nextToken ()); // 나무의 수
12+ int M = Integer .parseInt (st .nextToken ()); // 필요한 나무의 길이
13+
14+ int start = 0 , mid = 0 , end = 0 , result = 0 ;
15+
16+ int [] heights = new int [N ]; // 나무의 높이
17+ st = new StringTokenizer (br .readLine ());
18+ for (int i = 0 ; i < N ; i ++) {
19+ heights [i ] = Integer .parseInt (st .nextToken ());
20+ end = Math .max (end , heights [i ]);
21+ }
22+
23+ while (start <= end ) {
24+ mid = (start + end ) / 2 ;
25+ long sum = 0 ;
26+
27+ for (int height : heights ) {
28+ if (height > mid ) sum += (height - mid );
29+ }
30+
31+ if (sum < M ) end = mid - 1 ;
32+ else start = mid + 1 ;
33+ }
34+ System .out .println (end );
35+ }
36+ }
Original file line number Diff line number Diff line change 1+ // [골드4] 2110번. 공유기 설치
2+ // 메모리 : 28396 KB, 시간 : 244 ms
3+
4+ import java .io .*;
5+ import java .util .*;
6+
7+ public class P2110 {
8+ static int N , C ;
9+ static int [] houses ;
10+ public static void main (String [] args ) throws Exception {
11+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
12+ StringTokenizer st = new StringTokenizer (br .readLine ());
13+ N = Integer .parseInt (st .nextToken ()); // 집의 개수
14+ C = Integer .parseInt (st .nextToken ()); // 공유기의 개수
15+
16+ houses = new int [N ];
17+ for (int i = 0 ; i < N ; i ++) {
18+ houses [i ] = Integer .parseInt (br .readLine ());
19+ }
20+ Arrays .sort (houses );
21+
22+ int start = 1 , end = houses [N -1 ] - houses [0 ];
23+ while (start <= end ) {
24+ int mid = (start + end ) / 2 ; // 공유기 설치 간격
25+
26+ // 설치 불가능 -> 간격 더 줄여보기
27+ if (!install (mid , new ArrayList <>())) end = mid - 1 ;
28+
29+ // 설치 가능 -> 간격 더 늘려보기
30+ else start = mid + 1 ;
31+ }
32+ System .out .println (end );
33+ }
34+
35+ static boolean install (int mid , List <Integer > current ) {
36+ int cnt = 1 ; // 현재까지 설치된 공유기 개수
37+ int last = houses [0 ]; // 가장 최근 설치한 집
38+
39+ for (int i = 1 ; i < N ; i ++) {
40+ if (cnt >= C ) return true ;
41+
42+ if (houses [i ] - last < mid ) continue ;
43+ last = houses [i ];
44+ cnt ++;
45+ }
46+
47+ if (cnt < C ) return false ;
48+ return true ;
49+ }
50+ }
Original file line number Diff line number Diff line change 1+ // [골드1] 1300번. K번째 수
2+ // 메모리 : 11600 KB, 시간 : 104 ms
3+
4+ import java .io .*;
5+
6+ public class P1300 {
7+ public static void main (String [] args ) throws Exception {
8+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
9+ long N = Integer .parseInt (br .readLine ());
10+ long k = Integer .parseInt (br .readLine ());
11+
12+ long answer = 0 , start = 1 , end = k ;
13+
14+ while (start <= end ) {
15+ long mid = (start + end ) / 2 ;
16+
17+ long cnt = 0 ;
18+ for (int i = 1 ; i <= N ; i ++) {
19+ cnt += Math .min (mid / i , N );
20+ }
21+
22+ if (cnt >= k ) {
23+ answer = mid ;
24+ end = mid - 1 ;
25+ }
26+
27+ else start = mid + 1 ;
28+ }
29+ System .out .println (answer );
30+ }
31+ }
Original file line number Diff line number Diff line change 1+ // [골드2] 12015번. 가장 긴 증가하는 부분 수열 2
2+ // 메모리 : 125320 KB, 시간 : 444 ms
3+
4+ import java .io .*;
5+ import java .util .*;
6+
7+ public class P12015 {
8+ public static void main (String [] args ) throws Exception {
9+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10+ int N = Integer .parseInt (br .readLine ());
11+
12+ int [] A = new int [N ];
13+ StringTokenizer st = new StringTokenizer (br .readLine ());
14+ for (int i = 0 ; i < N ; i ++) A [i ] = Integer .parseInt (st .nextToken ());
15+
16+ int size = 0 ;
17+ int [] LIS = new int [N ];
18+ for (int i = 0 ; i < N ; i ++) {
19+ int num = A [i ];
20+
21+ int start = 0 , end = size ;
22+ while (start < end ) {
23+ int mid = (start + end ) / 2 ;
24+ if (LIS [mid ] < num ) start = mid + 1 ;
25+ else end = mid ;
26+ }
27+
28+ LIS [start ] = num ;
29+ if (start == size ) size ++;
30+ }
31+
32+ System .out .println (size );
33+ }
34+ }
Original file line number Diff line number Diff line change 1+ // [골드2] 12738번. 가장 긴 증가하는 부분 수열 3
2+ // 메모리 : 170056 KB, 시간 : 496 ms
3+
4+ import java .io .*;
5+ import java .util .*;
6+
7+ public class P12738 {
8+ public static void main (String [] args ) throws Exception {
9+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
10+ int N = Integer .parseInt (br .readLine ());
11+
12+ int [] A = new int [N ];
13+ StringTokenizer st = new StringTokenizer (br .readLine ());
14+ for (int i = 0 ; i < N ; i ++) A [i ] = Integer .parseInt (st .nextToken ());
15+
16+ int size = 0 ;
17+ int [] LIS = new int [N ];
18+ for (int i = 0 ; i < N ; i ++) {
19+ int num = A [i ];
20+
21+ int start = 0 , end = size ;
22+ while (start < end ) {
23+ int mid = (start + end ) / 2 ;
24+ if (LIS [mid ] < num ) start = mid + 1 ;
25+ else end = mid ;
26+ }
27+
28+ LIS [start ] = num ;
29+ if (start == size ) size ++;
30+ }
31+
32+ System .out .println (size );
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments