-
Notifications
You must be signed in to change notification settings - Fork 1
[유병규-16주차 알고리즘 스터디] #75
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
Gyulguma
wants to merge
4
commits into
main
Choose a base branch
from
ybg6539/week-16
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
4 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
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,65 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
||
int n = Integer.parseInt(st.nextToken()); | ||
int m = Integer.parseInt(st.nextToken()); | ||
|
||
st = new StringTokenizer(br.readLine()); | ||
|
||
int s = Integer.parseInt(st.nextToken()); | ||
int e = Integer.parseInt(st.nextToken()); | ||
|
||
List<Edge>[] graph = new ArrayList[n+1]; | ||
for(int i=1; i<=n; i++) { | ||
graph[i] = new ArrayList<>(); | ||
} | ||
|
||
for(int i=0; i<m; i++) { | ||
st = new StringTokenizer(br.readLine()); | ||
int a = Integer.parseInt(st.nextToken()); | ||
int b = Integer.parseInt(st.nextToken()); | ||
int c = Integer.parseInt(st.nextToken()); | ||
graph[a].add(new Edge(b, c)); | ||
graph[b].add(new Edge(a, c)); | ||
} | ||
|
||
boolean[] visited = new boolean[n+1]; | ||
PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> -Integer.compare(o1[1], o2[1])); | ||
pq.offer(new int[] {s, Integer.MAX_VALUE}); | ||
|
||
while(!pq.isEmpty()) { | ||
int[] info = pq.poll(); | ||
int current = info[0]; | ||
|
||
if(current == e) { | ||
System.out.println(info[1]); | ||
return; | ||
} | ||
|
||
if(visited[current]) continue; | ||
visited[current] = true; | ||
for(Edge edge : graph[current]) { | ||
if(visited[edge.to]) continue; | ||
pq.offer(new int[] {edge.to, Math.min(info[1], edge.weight)}); | ||
} | ||
} | ||
System.out.println(0); | ||
} | ||
|
||
private static class Edge{ | ||
int to; | ||
int weight; | ||
|
||
public Edge(int to, int weight) { | ||
this.to = to; | ||
this.weight = weight; | ||
} | ||
} | ||
} |
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,137 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class Main { | ||
private static int n,m; | ||
private static int[] red,blue; | ||
private static char[][] map; | ||
private static int[][] d = {{-1,0},{1,0},{0,-1},{0,1}}; | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
n = Integer.parseInt(st.nextToken()); | ||
m = Integer.parseInt(st.nextToken()); | ||
|
||
map = new char[n][m]; | ||
for(int i=0; i<n; i++) { | ||
String line = br.readLine(); | ||
for(int j=0; j<m; j++) { | ||
map[i][j] = line.charAt(j); | ||
if(map[i][j] == 'R') { | ||
red = new int[] {i,j}; | ||
map[i][j] = '.'; | ||
} | ||
if(map[i][j] == 'B') { | ||
blue = new int[] {i,j}; | ||
map[i][j] = '.'; | ||
} | ||
} | ||
} | ||
|
||
int count = bfs(); | ||
System.out.println(count); | ||
} | ||
|
||
private static int bfs() { | ||
Queue<int[]> q = new LinkedList<>(); | ||
boolean[][][][] visited = new boolean[n][m][n][m]; | ||
|
||
q.offer(new int[] {red[0], red[1], blue[0], blue[1], 0, 0, 4}); | ||
visited[red[0]][red[1]][blue[0]][blue[1]] = true; | ||
|
||
while(!q.isEmpty()) { | ||
int[] info = q.poll(); | ||
int rx = info[0]; | ||
int ry = info[1]; | ||
int bx = info[2]; | ||
int by = info[3]; | ||
int count = info[4]; | ||
int dStart = info[5]; | ||
int dCount = info[6]; | ||
|
||
int dx = (bx-rx == 0) ? 0 : (bx-rx)/Math.abs(bx-rx); | ||
int dy = (by-ry == 0) ? 0 : (by-ry)/Math.abs(by-ry); | ||
To: for(int i=0; i<dCount; i++) { | ||
int idx = dStart+i; | ||
|
||
int nbx = bx + d[idx][0]; | ||
int nby = by + d[idx][1]; | ||
int nrx = rx + d[idx][0]; | ||
int nry = ry + d[idx][1]; | ||
|
||
if(OOB(nbx, nby) || OOB(nrx, nry)) continue; | ||
|
||
//블루 먼저 | ||
if(dx == d[idx][0] && dy == d[idx][1]) { | ||
while(true) { | ||
if(map[nbx][nby] == 'O') continue To; | ||
if(map[nbx][nby] == '#') break; | ||
nbx += d[idx][0]; | ||
nby += d[idx][1]; | ||
} | ||
|
||
nbx -= d[idx][0]; | ||
nby -= d[idx][1]; | ||
|
||
while(true) { | ||
if(map[nrx][nry] == 'O') break; | ||
if(map[nrx][nry] == '#' || (nbx == nrx && nby == nry)) break; | ||
nrx += d[idx][0]; | ||
nry += d[idx][1]; | ||
} | ||
|
||
if(map[nrx][nry] == 'O') return count+1; | ||
|
||
nrx -= d[idx][0]; | ||
nry -= d[idx][1]; | ||
|
||
if(visited[nrx][nry][nbx][nby]) continue To; | ||
int ndStart = idx < 2 ? 2 : 0; | ||
q.offer(new int[] {nrx, nry, nbx, nby, count+1, ndStart, 2}); | ||
visited[nrx][nry][nbx][nby] = true; | ||
} | ||
//레드 먼저 | ||
else { | ||
while(true) { | ||
if(map[nrx][nry] == 'O') break; | ||
if(map[nrx][nry] == '#') break; | ||
nrx += d[idx][0]; | ||
nry += d[idx][1]; | ||
} | ||
|
||
if(map[nrx][nry] != 'O') { | ||
nrx -= d[idx][0]; | ||
nry -= d[idx][1]; | ||
} | ||
|
||
|
||
while(true) { | ||
if(map[nbx][nby] == 'O') continue To; | ||
if(map[nbx][nby] == '#' || (nbx == nrx && nby == nry)) break; | ||
nbx += d[idx][0]; | ||
nby += d[idx][1]; | ||
} | ||
|
||
nbx -= d[idx][0]; | ||
nby -= d[idx][1]; | ||
|
||
if(map[nrx][nry] == 'O') return count+1; | ||
|
||
if(visited[nrx][nry][nbx][nby]) continue To; | ||
int ndStart = idx < 2 ? 2 : 0; | ||
q.offer(new int[] {nrx, nry, nbx, nby, count+1, ndStart, 2}); | ||
visited[nrx][nry][nbx][nby] = true; | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
private static boolean OOB(int x, int y) { | ||
if(x<0 || x>=n || y<0 || y>=m) return true; | ||
return false; | ||
} | ||
} |
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,71 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class Main { | ||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
||
int n = Integer.parseInt(st.nextToken()); | ||
int m = Integer.parseInt(st.nextToken()); | ||
|
||
int[] men = new int[n + 1]; | ||
int[] women = new int[m + 1]; | ||
|
||
st = new StringTokenizer(br.readLine()); | ||
for (int i=1; i<=n; i++) { | ||
men[i] = Integer.parseInt(st.nextToken()); | ||
} | ||
|
||
st = new StringTokenizer(br.readLine()); | ||
for (int i=1; i<=m; i++) { | ||
women[i] = Integer.parseInt(st.nextToken()); | ||
} | ||
|
||
// 정렬 | ||
Arrays.sort(men); | ||
Arrays.sort(women); | ||
|
||
// 작은 그룹과 큰 그룹 결정 | ||
int[] small, large; | ||
int smallSize, largeSize; | ||
|
||
if (n <= m) { | ||
small = men; | ||
large = women; | ||
smallSize = n; | ||
largeSize = m; | ||
} else { | ||
small = women; | ||
large = men; | ||
smallSize = m; | ||
largeSize = n; | ||
} | ||
|
||
// DP 배열 초기화 | ||
long[][] dp = new long[smallSize+1][largeSize+1]; | ||
|
||
// 초기값 설정 | ||
for (int i=0; i<=smallSize; i++) { | ||
for (int j=0; j<i; j++) { | ||
dp[i][j] = Long.MAX_VALUE; | ||
} | ||
} | ||
|
||
// DP 계산 | ||
for (int i=1; i<=smallSize; i++) { | ||
for (int j=i; j<=largeSize; j++) { | ||
if (j == i) { | ||
dp[i][j] = dp[i-1][j-1] + Math.abs(small[i] - large[j]); | ||
} else { | ||
dp[i][j] = Math.min( | ||
dp[i][j-1], // j번째 사람을 선택하지 않는 경우 | ||
dp[i-1][j-1] + Math.abs(small[i] - large[j]) // j번째 사람을 선택하는 경우 | ||
); | ||
} | ||
} | ||
} | ||
|
||
System.out.println(dp[smallSize][largeSize]); | ||
} | ||
} |
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,60 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class Main { | ||
private static int n, k; | ||
private static int[] num; | ||
|
||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
n = Integer.parseInt(st.nextToken()); | ||
k = Integer.parseInt(st.nextToken()); | ||
|
||
st = new StringTokenizer(br.readLine()); | ||
num = new int[n]; | ||
int sum = 0; | ||
for(int i=0; i<n; i++) { | ||
num[i] = Integer.parseInt(st.nextToken()); | ||
sum += num[i]; | ||
} | ||
|
||
if(k == 1) { | ||
System.out.println(sum); | ||
return; | ||
} | ||
|
||
int left = 0; | ||
int right = sum; | ||
int mid = 0; | ||
while(left <= right) { | ||
mid = left + (right-left)/2; | ||
|
||
int count = counting(mid); | ||
|
||
if(count < k) { | ||
right = mid-1; | ||
continue; | ||
} | ||
|
||
left = mid+1; | ||
} | ||
|
||
System.out.println(right); | ||
} | ||
|
||
private static int counting(int min) { | ||
int count = 0; | ||
|
||
int sum = 0; | ||
for(int i=0; i<n; i++) { | ||
sum += num[i]; | ||
if(sum < min) continue; | ||
count++; | ||
sum = 0; | ||
} | ||
|
||
return count; | ||
} | ||
} |
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.
저는 j번째 사람을 선택하는 경우와 선택하지 않는 경우를 따로 처리하려고 dp 배열 두 개를 관리했는데 그럴 필요가 없었군요... 좋은 코드네요 👍