-
Notifications
You must be signed in to change notification settings - Fork 3
조민제 / BOJ 3055, BOJ 3079, BOJ 1766, BOJ 1477 / 그래프, 이분탐색, 위상정렬 / 5주차 #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
minje0204
wants to merge
3
commits into
main
Choose a base branch
from
minje
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
src/algorithm/minje/week5/boj/Q1477/Main_1477_휴게소_세우기_조민제.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package algorithm.minje.week5.boj.Q1477; | ||
| /* | ||
| 메모리 : 11720KB | ||
| 시간 : 80ms | ||
| */ | ||
| import java.io.BufferedReader; | ||
| import java.io.BufferedWriter; | ||
| import java.io.InputStreamReader; | ||
| import java.io.OutputStreamWriter; | ||
| import java.util.Arrays; | ||
| import java.util.StringTokenizer; | ||
|
|
||
| public class Main_1477_휴게소_세우기_조민제 { | ||
| static int N, M, L; | ||
| static int[] diff; | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
| BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); | ||
| StringBuilder sb = new StringBuilder(); | ||
|
|
||
| StringTokenizer st = new StringTokenizer(br.readLine(), " "); | ||
|
|
||
| N = Integer.parseInt(st.nextToken()); | ||
| M = Integer.parseInt(st.nextToken()); | ||
| L = Integer.parseInt(st.nextToken()); | ||
|
|
||
| diff = new int[N + 1]; | ||
| int[] loc = new int[N + 2]; | ||
| loc[0] = 0; | ||
| StringTokenizer st1 = new StringTokenizer(br.readLine(), " "); | ||
| for (int i = 1; i <= N; i++) { | ||
| loc[i] = Integer.parseInt(st1.nextToken()); | ||
| } | ||
| loc[N + 1] = L; | ||
|
|
||
| Arrays.sort(loc); | ||
|
|
||
| //차이구하기 | ||
| for (int i = 1; i <= N + 1; i++) { | ||
| diff[i - 1] = loc[i] - loc[i - 1]; | ||
| } | ||
| Arrays.sort(diff); | ||
|
|
||
| int left = 1, right = diff[diff.length - 1]; | ||
| int ans = Integer.MAX_VALUE; | ||
| while (left <= right) { | ||
| int mid = (left + right) / 2; | ||
| if (isPassed(mid)) { | ||
| ans = Math.min(ans, mid); | ||
| right = mid - 1; | ||
| continue; | ||
| } | ||
| left = mid + 1; | ||
| } | ||
|
|
||
| sb.append(ans); | ||
| bw.write(sb.toString()); | ||
| bw.flush(); //왜와이? | ||
| } | ||
|
|
||
| public static boolean isPassed(int mid) { | ||
| int amount = 0; | ||
| for (int i = diff.length - 1; i >= 0; i--) { | ||
| if (diff[i] <= mid) break; | ||
| int dist = diff[i]; | ||
| int divisor = 1; | ||
| while (dist > mid) { | ||
| dist = diff[i]; | ||
| divisor++; | ||
| if (dist % divisor != 0) | ||
| dist = dist / divisor + 1; | ||
| else | ||
| dist = dist / divisor; | ||
|
|
||
| } | ||
| amount += divisor - 1; | ||
| } | ||
|
|
||
| if (amount <= M) return true; | ||
| return false; | ||
| } | ||
| } | ||
74 changes: 74 additions & 0 deletions
74
src/algorithm/minje/week5/boj/Q1766/Main_1766_문제집_조민제.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package algorithm.minje.week5.boj.Q1766; | ||
|
|
||
| /* | ||
| 메모리 : 53276KB | ||
| 시간 : 476ms | ||
| */ | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.BufferedWriter; | ||
| import java.io.InputStreamReader; | ||
| import java.io.OutputStreamWriter; | ||
| import java.util.LinkedList; | ||
| import java.util.PriorityQueue; | ||
| import java.util.Queue; | ||
| import java.util.StringTokenizer; | ||
|
|
||
| public class Main_1766_문제집_조민제 { | ||
| static boolean[] problemSolved; | ||
| static PriorityQueue<Integer> problemWillSolved = new PriorityQueue<>(); | ||
| static Queue<Integer>[] adjList; | ||
| static int[] remainProblem; | ||
| static int N, M; | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
| BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); | ||
| StringBuilder sb = new StringBuilder(); | ||
|
|
||
| StringTokenizer st = new StringTokenizer(br.readLine(), " "); | ||
|
|
||
| N = Integer.parseInt(st.nextToken()); | ||
| M = Integer.parseInt(st.nextToken()); | ||
|
|
||
| problemSolved = new boolean[N + 1]; | ||
| remainProblem = new int[N + 1]; | ||
| adjList = new Queue[N + 1]; | ||
|
|
||
| for (int i = 0; i <= N; i++) { | ||
| adjList[i] = new LinkedList<>(); | ||
| } | ||
|
|
||
| for (int i = 0; i < M; i++) { | ||
| StringTokenizer st1 = new StringTokenizer(br.readLine(), " "); | ||
| int solvePrev = Integer.parseInt(st1.nextToken()); | ||
| int solveNext = Integer.parseInt(st1.nextToken()); | ||
| adjList[solvePrev].add(solveNext); | ||
| remainProblem[solveNext]++; | ||
| } | ||
|
|
||
| for (int i = 1; i <= N; i++) { | ||
| if (remainProblem[i] == 0) { | ||
| problemWillSolved.offer(i); | ||
| problemSolved[i] = true; | ||
| } | ||
| } | ||
|
|
||
| while (!problemWillSolved.isEmpty()) { | ||
| int cur = problemWillSolved.poll(); | ||
| sb.append(cur + " "); | ||
| int size = adjList[cur].size(); | ||
| for (int i = 0; i < size; i++) { | ||
| int cand = adjList[cur].poll(); | ||
| remainProblem[cand] -= 1; | ||
| if (remainProblem[cand] == 0) { | ||
| problemSolved[cand] = true; | ||
| problemWillSolved.offer(cand); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| bw.write(sb.toString()); | ||
| bw.flush(); | ||
| } | ||
| } |
120 changes: 120 additions & 0 deletions
120
src/algorithm/minje/week5/boj/Q3055/Main_3055_탈출_조민제.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| package algorithm.minje.week5.boj.Q3055; | ||
| /* | ||
| 메모리 : 11984KB | ||
| 시간 : 92ms | ||
| */ | ||
| import java.io.BufferedReader; | ||
| import java.io.BufferedWriter; | ||
| import java.io.InputStreamReader; | ||
| import java.io.OutputStreamWriter; | ||
| import java.util.*; | ||
|
|
||
| public class Main_3055_탈출_조민제 { | ||
| static char[][] map; | ||
| static char WATER = '*'; | ||
| static char GO_SEUM_DO_CHI = 'S'; | ||
| static char BEBBER_CAVE = 'D'; | ||
| static char EMPTY_LAND = '.'; | ||
|
|
||
| static int[] dr = {-1, 1, 0, 0}; //상하좌우 | ||
| static int[] dc = {0, 0, -1, 1}; | ||
| // static int[] GS_LOC; //고슴도치 위치 | ||
| // static int[] BC_LOC; //비버의굴 위치 | ||
| static int time = 0; | ||
| static Queue<int[]> waterQ = new LinkedList<>(); | ||
| static Queue<int[]> pathQ = new LinkedList<>(); | ||
| static int R, C; | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
| BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); | ||
| StringBuilder sb = new StringBuilder(); | ||
|
|
||
| StringTokenizer st = new StringTokenizer(br.readLine(), " "); | ||
|
|
||
| R = Integer.parseInt(st.nextToken()); | ||
| C = Integer.parseInt(st.nextToken()); | ||
|
|
||
| map = new char[R][C]; | ||
|
|
||
| for (int i = 0; i < R; i++) { | ||
| char[] input = br.readLine().toCharArray(); | ||
| for (int j = 0; j < C; j++) { | ||
| inputHandler(input[j], new int[]{i, j}); | ||
| map[i][j] = input[j]; | ||
| } | ||
| } | ||
|
|
||
| do { | ||
| time++; | ||
| if (pathQ.size() == 0) { | ||
| System.out.println("KAKTUS"); | ||
| break; | ||
| } | ||
| waterSpread(); | ||
| } while (!moveGS()); | ||
|
|
||
| } | ||
|
|
||
| public static void waterSpread() { | ||
| int size = waterQ.size(); | ||
| while (size-- > 0) { | ||
| int[] cur = waterQ.poll(); | ||
| for (int d = 0; d < 4; d++) { | ||
| int nr = cur[0] + dr[d]; | ||
| int nc = cur[1] + dc[d]; | ||
| if (isValidRange(nr, nc) && isEmptyLand(nr, nc)) { | ||
| map[nr][nc] = WATER; | ||
| waterQ.offer(new int[]{nr, nc}); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static boolean moveGS() { | ||
| int size = pathQ.size(); | ||
| while (size-- > 0) { | ||
| int[] cur = pathQ.poll(); | ||
| for (int d = 0; d < 4; d++) { | ||
| int nr = cur[0] + dr[d]; | ||
| int nc = cur[1] + dc[d]; | ||
| if (isValidRange(nr, nc)) { | ||
| if (arrived(nr, nc)) { | ||
| System.out.println(time); | ||
| return true; | ||
| } | ||
| if (isEmptyLand(nr, nc)) { | ||
| map[nr][nc] = WATER; //물로 방문체크 | ||
| pathQ.offer(new int[]{nr, nc}); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public static boolean isValidRange(int nr, int nc) { | ||
| return nr >= 0 && nc >= 0 && nr < R && nc < C; | ||
| } | ||
|
|
||
| public static boolean isEmptyLand(int nr, int nc) { | ||
| return map[nr][nc] == EMPTY_LAND; | ||
| } | ||
|
|
||
| public static boolean arrived(int nr, int nc) { | ||
| return map[nr][nc] == BEBBER_CAVE; | ||
| } | ||
|
|
||
| public static void inputHandler(char input, int[] curCord) { | ||
| if (input == GO_SEUM_DO_CHI) { | ||
| pathQ.offer(curCord); | ||
| } | ||
| // if (input == BEBBER_CAVE) { | ||
| // BC_LOC = curCord; | ||
| // } | ||
| if (input == WATER) { | ||
| waterQ.offer(curCord); | ||
| } | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
src/algorithm/minje/week5/boj/Q3079/Main_3079_입국심사_조민제.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package algorithm.minje.week5.boj.Q3079; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.BufferedWriter; | ||
| import java.io.InputStreamReader; | ||
| import java.io.OutputStreamWriter; | ||
| import java.util.Arrays; | ||
| import java.util.StringTokenizer; | ||
|
|
||
| public class Main_3079_입국심사_조민제 { | ||
| static int N, M; | ||
| static int[] times; | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
| BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); | ||
| StringBuilder sb = new StringBuilder(); | ||
|
|
||
| StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
|
||
| N = Integer.parseInt(st.nextToken()); | ||
| M = Integer.parseInt(st.nextToken()); | ||
|
|
||
| times = new int[N]; | ||
| for (int i = 0; i < N; i++) { | ||
| int input = Integer.parseInt(br.readLine()); | ||
| times[i] = input; | ||
| } | ||
|
|
||
| Arrays.sort(times); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sort 는 굳이 안해도 될 것 같아요! |
||
|
|
||
| // long max = times[times.length - 1]; | ||
| long left = 1; | ||
| // long right = max * M; | ||
| long right = Integer.MAX_VALUE + 1L; | ||
|
|
||
|
|
||
| /* | ||
| 1. times int->long | ||
| 2. times * M | ||
| 3. right 대입 | ||
| */ | ||
| // long right = times[times.length - 1] * M; | ||
| /* | ||
| 1. times * M int형 계산 범위 안에서 | ||
| 2. | ||
| */ | ||
| long mid = 0; | ||
|
|
||
| System.out.println(right); | ||
| long ans = Long.MAX_VALUE; | ||
|
|
||
| while (left <= right) { | ||
| mid = (left + right) / 2; | ||
|
|
||
| if (isPassed(times, M, mid)) { | ||
| ans = ans > mid ? mid : ans; | ||
| right = mid - 1; | ||
| } else { | ||
| left = mid + 1; | ||
| } | ||
| } | ||
| System.out.println(ans); | ||
| } | ||
|
|
||
| public static boolean isPassed(int[] times, int n, long mid) { | ||
| long amount = 0; | ||
|
|
||
| for (int i = 0; i < times.length; ++i) { | ||
| amount += mid / times[i]; | ||
| } | ||
|
|
||
| if (amount >= n) return true; | ||
| else return false; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
while문 대신 (diff[i] - 1) / divisor 방식으로 해도 될 것 같아요!