-
Notifications
You must be signed in to change notification settings - Fork 4
[7주차] 이예진 #96
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
[7주차] 이예진 #96
Changes from all commits
143350d
4f3d5c4
07576c6
369f649
9cd5807
dc5237b
33f678a
e1c1013
9270765
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import java.io.*; | ||
|
||
public class YJ_1535 { | ||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int N = Integer.parseInt(br.readLine()); | ||
|
||
String[] s = br.readLine().split("\\s"); | ||
String[] h = br.readLine().split("\\s"); | ||
int[] exhaustion = new int[N+1]; | ||
int[] happy = new int[N+1]; | ||
|
||
for(int i=1; i<N+1; i++){ | ||
exhaustion[i] = Integer.parseInt(s[i-1]); | ||
happy[i] = Integer.parseInt(h[i-1]); | ||
} | ||
|
||
final int MAX_STAMINA = 100; | ||
int[] dp = new int[MAX_STAMINA+1]; //dp[체력]= 최대 기쁨 | ||
for(int i = 1; i < N+1; i++){ | ||
for(int stamina=MAX_STAMINA; stamina > 0; stamina--) { //뒤에서 부터 특정 체력에 대한 기쁨을 갱신하면서 반복 | ||
int total = stamina-exhaustion[i]; //현재 체력에서 체력소모를 빼서 현재 상태에서 가능한 상황을 고려 | ||
if (total > 0) { | ||
dp[stamina] = Math.max(dp[stamina], dp[total] + happy[i]); //"이미 갱신된 기쁨" vs "현재 체력소모를 포함하기 위해 이전 상태의 기쁨 + 현재 기쁨"을 비교하기 | ||
//System.out.printf("dp[%d] = Math.max(dp[%d], dp[%d]+happy[%d])\n", stamina, stamina, total , i); | ||
//System.out.printf("%d = Math.max(%d, %d)\n\n", dp[stamina], dp[stamina], dp[total] + happy[i]); | ||
} | ||
} | ||
} | ||
|
||
System.out.println(dp[MAX_STAMINA]); //최대체력인 dp[100]은 항상 최대기쁨 값이 보장된다 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import java.io.*; | ||
|
||
public class YJ_2073 { | ||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
String[] DP = br.readLine().split("\\s"); | ||
int D = Integer.parseInt(DP[0]); | ||
int P = Integer.parseInt(DP[1]); | ||
|
||
int[] length = new int[P]; | ||
int[] capacity = new int[P]; | ||
for(int i=0; i<P; i++){ | ||
String[] data = br.readLine().split("\\s"); | ||
length[i] = Integer.parseInt(data[0]); | ||
capacity[i] = Integer.parseInt(data[1]); | ||
} | ||
|
||
int[] dp = new int[D+1]; //dp[특정거리] = 최대 용량 | ||
dp[0] = Integer.MAX_VALUE; //초기 상태에서의 최소 용량을 설정하기 위해서 최대 정수를 설정 | ||
|
||
// 특정 거리에 만들 수 있는 수도관(여러 파이프들로 구성) 중 가장 큰 용량 구하기 | ||
for(int i=0; i<P; i++){ | ||
for(int distance=D; distance>=length[i]; distance--){ | ||
//특정 거리에 만들 수 있는 수도관(여러 파이프들로 구성) 중 가장 큰 용량 구하기 | ||
dp[distance] = Math.max(dp[distance], Math.min(capacity[i],dp[distance-length[i]])); | ||
} | ||
} | ||
|
||
System.out.println(dp[D]); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Scanner; | ||
import java.util.TreeMap; | ||
import java.util.TreeSet; | ||
|
||
public class YJ_21939 { | ||
static TreeMap<Integer, TreeSet<Integer>> workbook = new TreeMap<>(); //<난이도,문제번호> | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
int N = scanner.nextInt(); | ||
for (int i = 0; i < N; i++) { | ||
int number = scanner.nextInt(); | ||
int difficulty = scanner.nextInt(); | ||
workbook.computeIfAbsent(difficulty, set -> new TreeSet<>()) | ||
.add(number); | ||
} | ||
|
||
while (scanner.hasNext()) { | ||
String say = scanner.next(); | ||
switch (say) { | ||
case "recommend": | ||
recommend(scanner.nextInt()); | ||
break; | ||
case "add": | ||
add(scanner.nextInt(), scanner.nextInt()); | ||
break; | ||
case "solved": | ||
solved(scanner.nextInt()); | ||
break; | ||
} | ||
} | ||
|
||
scanner.close(); | ||
} | ||
|
||
static void recommend(int difficulty) { | ||
if (difficulty == 1) { | ||
System.out.println(workbook.lastEntry().getValue().last()); | ||
} else { | ||
System.out.println(workbook.firstEntry().getValue().first()); | ||
} | ||
} | ||
|
||
static void add(int number, int difficulty) { | ||
workbook.computeIfAbsent(difficulty, set -> new TreeSet<>()) | ||
.add(number); | ||
} | ||
|
||
static void solved(int number) { | ||
Iterator<Entry<Integer, TreeSet<Integer>>> iterator = workbook.entrySet().iterator(); | ||
while (iterator.hasNext()) { | ||
Map.Entry<Integer, TreeSet<Integer>> entry = iterator.next(); | ||
if (entry.getValue().remove(number)) { | ||
if (entry.getValue().isEmpty()) { | ||
iterator.remove(); | ||
} | ||
return; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class YJ_5021 { | ||
static Map<String,ArrayList<String>> families = new HashMap<>(); //<자식,자식의부모 리스트> | ||
static Map<String,Double> bloodRatio = new HashMap<>(); //<계승후보,혈통비율> | ||
|
||
public static void main(String[] args) throws IOException{ | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
String[] data = br.readLine().split("\\s"); | ||
final int N = Integer.parseInt(data[0]); | ||
final int M = Integer.parseInt(data[1]); | ||
|
||
final String king = br.readLine(); | ||
for(int i=0; i<N; i++){ | ||
String[] family = br.readLine().split("\\s"); | ||
//위상정렬 그래프 초기화 | ||
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. 위상정렬은 indegree라는 진입차수 배열과 큐를 사용해야 되는데, 예진님은 위상정렬은 사용하지 않고 DFS로만 풀이했다고 해야될 것 같습니다! 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. 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. 그래프의 구조 자체는 비순환 유향 그래프인 것이고, 문제 풀이 유형으로 DFS나 위상정렬 방식으로 풀이할 수 있는 것으로 이해하면 될 것 같습니다! |
||
String child = family[0]; | ||
families.computeIfAbsent(child, k -> new ArrayList<>()); | ||
String dad = family[1]; | ||
String mom = family[2]; | ||
families.get(child).add(dad); | ||
families.get(child).add(mom); | ||
|
||
//모든 사람들 혈통 1 초기화 | ||
bloodRatio.put(child, 1.0); | ||
bloodRatio.put(dad, 1.0); | ||
bloodRatio.put(mom, 1.0); | ||
} | ||
//왕은 최상위 혈통으로 0 | ||
bloodRatio.put(king, 0.0); | ||
|
||
//후보자 별로 혈통 계산하기 | ||
for(String name : bloodRatio.keySet()){ | ||
findAnHeir(name); | ||
} | ||
|
||
//계승자 찾기 | ||
String nextKing = br.readLine(); | ||
for(int i=1; i<M; i++){ | ||
String candidate = br.readLine(); | ||
if(bloodRatio.getOrDefault(nextKing,-1.0) < bloodRatio.getOrDefault(candidate,-1.0)){ | ||
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.
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. @KodaHye 맞아요! |
||
nextKing = candidate; | ||
} | ||
} | ||
System.out.println(nextKing); | ||
} | ||
|
||
static double findAnHeir(String name){ | ||
//왕이거나, 이미 혈통을 계산했을 경우 | ||
if(!bloodRatio.get(name).equals(1.0)){ | ||
return bloodRatio.get(name); | ||
} | ||
|
||
ArrayList<String> parent = families.get(name); | ||
//왕족이 아닌 부모는 상위 부모가 없음 | ||
if(Objects.isNull(parent)){ | ||
yeahdy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bloodRatio.put(name,-1.0); | ||
return bloodRatio.get(name); | ||
} | ||
//깊이 탐색을 통해 자식의 부모로 올라가면서 현재 자식의 혈통을 계산 | ||
double dad = findAnHeir(parent.get(0)); | ||
double mom = findAnHeir(parent.get(1)); | ||
|
||
//모든 사람은 아버지의 혈통과 어머니의 혈통을 반 씩 받게 된다 | ||
//dfs 탐색이 깊어질수록 혈통이 옅어짐 (1/2 > 1/4 > 1/16 ...) | ||
bloodRatio.put(name,(dad+mom)/2.0); | ||
|
||
return bloodRatio.get(name); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
|
||
public class YJ_시공의_돌풍 { | ||
static class Dust{ | ||
int x; | ||
int y; | ||
int data; | ||
|
||
Dust(int x, int y, int data){ | ||
this.x = x; | ||
this.y = y; | ||
this.data = data; | ||
} | ||
} | ||
|
||
static int[][] room; | ||
static int[][] temp; | ||
static boolean[][] visited; | ||
public static void main(String[] args) throws IOException { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
String[] data = br.readLine().split("\\s"); | ||
int n = Integer.parseInt(data[0]); | ||
int m = Integer.parseInt(data[1]); | ||
int t = Integer.parseInt(data[2]); | ||
|
||
visited = new boolean[n][m]; | ||
room = new int[n][m]; | ||
int[] tornado = new int[2]; | ||
int z = 0; | ||
for(int i=0; i<n; i++){ | ||
String[] d = br.readLine().split("\\s"); | ||
yeahdy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for(int j=0; j<m; j++){ | ||
int dust = Integer.parseInt(d[j]); | ||
if(dust == -1){ | ||
visited [i][j] = true; | ||
tornado[z++] = i; | ||
} | ||
room[i][j] = dust; | ||
} | ||
} | ||
|
||
int result = 0; | ||
while(t-- > 0){ | ||
//먼지가 인접한 4방향의 상하좌우 칸으로 확산: bfs 너비탐색 | ||
bfs(n,m); | ||
//시공의 돌풍이 청소를 시작 | ||
goTornado(n,m,tornado); | ||
//확산된 먼지 합산 | ||
result = calculateTotalDust(n,m); | ||
} | ||
System.out.println(result); | ||
} | ||
|
||
static void bfs(int n, int m){ | ||
final int SPREAD = 5; | ||
yeahdy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Queue<Dust> queue = new LinkedList<>(); | ||
int[] nx = {1,0,-1,0}; | ||
int[] ny = {0,1,0,-1}; | ||
|
||
temp = new int[n][m]; | ||
queue.offer(new Dust(0,0,room[0][0])); | ||
|
||
while(!queue.isEmpty()){ | ||
Dust dust = queue.poll(); | ||
int share = dust.data/SPREAD; | ||
|
||
int currX = dust.x; | ||
int currY = dust.y; | ||
if(visited[currX][currY]){ | ||
continue; | ||
} | ||
|
||
for(int i=0; i<4; i++){ | ||
int x = currX + nx[i]; | ||
int y = currY + ny[i]; | ||
|
||
if(stop(x,y,n,m)){ | ||
continue; | ||
} | ||
int next = room[x][y]; | ||
if(next == -1){ | ||
continue; | ||
} | ||
|
||
temp[x][y] += share; | ||
room[currX][currY] -= share; | ||
queue.offer(new Dust(x,y,next)); | ||
} | ||
visited[currX][currY] = true; | ||
} | ||
|
||
sumDust(n,m); | ||
} | ||
|
||
private static boolean stop(int x, int y, int n, int m){ | ||
return x<0 || y<0 || x>=n || y>=m; | ||
} | ||
|
||
private static void sumDust(int n, int m){ | ||
for(int x=0; x<n; x++){ | ||
for(int y=0; y<m; y++){ | ||
room[x][y] += temp[x][y]; | ||
} | ||
} | ||
} | ||
|
||
static void goTornado(int n, int m, int[] tornado){ | ||
up(0,0,tornado[0],m-1); | ||
down(tornado[1],0,n-1,m-1); | ||
cleaning(tornado); | ||
} | ||
// 0,0,윗태풍r,윗태풍c | ||
private static void up(int startR, int startC, int endR, int endC){ | ||
int corner = room[startR][startC]; | ||
//맨윗 줄 ← | ||
for(int c = startC; c<endC; c++){ | ||
room[startR][c] = room[startR][c+1]; | ||
} | ||
//맨오른쪽 줄 ↑ | ||
for(int r = startR; r<endR; r++){ | ||
room[r][endC] = room[r+1][endC]; | ||
} | ||
//맨아랫 줄 → | ||
for(int c = endC; c>startC; c--){ | ||
room[endR][c] = room[endR][c-1]; | ||
} | ||
//맨왼쪽 줄 ↓ | ||
for(int r = endR; r>startR; r--){ | ||
room[r][startR] = room[r-1][startR]; | ||
} | ||
room[startR+1][startC] = corner; | ||
} | ||
|
||
// 아랫태풍r,0,끝r,끝c | ||
private static void down(int startR, int startC, int endR, int endC){ | ||
int corner = room[startR][startC]; | ||
//맨왼쪽 줄 ↓ | ||
for(int r = startR; r<endR; r++){ | ||
room[r][startC] = room[r+1][startC]; | ||
} | ||
//맨아랫 줄 ← | ||
for(int c = startC; c<endC; c++){ | ||
room[endR][c] = room[endR][c+1]; | ||
} | ||
//맨오른쪽 줄 ↑ | ||
for(int r = endR; r>startR; r--){ | ||
room[r][endC] = room[r-1][endC]; | ||
} | ||
//맨윗쪽 줄 → | ||
for(int c = endC; c>startC; c--){ | ||
room[startR][c] = room[startR][c-1]; | ||
} | ||
room[startR][startC+1] = corner; | ||
} | ||
|
||
private static void cleaning(int[] tornado){ | ||
room[tornado[0]][0] = -1; | ||
room[tornado[1]][0] = -1; | ||
room[tornado[0]][1] = 0; | ||
room[tornado[1]][1] = 0; | ||
} | ||
|
||
static int calculateTotalDust(int n, int m){ | ||
int result = 0; | ||
for(int x=0; x<n; x++){ | ||
for(int y=0; y<m; y++){ | ||
if(room[x][y] == -1){ | ||
continue; | ||
} | ||
visited [x][y] = false; | ||
result += room[x][y]; | ||
} | ||
} | ||
return result; | ||
} | ||
} |