Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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

Merged
baexxbin merged 9 commits into GreatAlgorithm-Study:main from yeahdy:main
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions BOJ/1000-10000번/YJ_1535.java
View file Open in desktop
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]은 항상 최대기쁨 값이 보장된다
}
}
31 changes: 31 additions & 0 deletions BOJ/1000-5000번/YJ_2073.java
View file Open in desktop
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]);
}
}
63 changes: 63 additions & 0 deletions BOJ/20001-25000번/YJ_21939.java
View file Open in desktop
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;
}
}
}
}
71 changes: 71 additions & 0 deletions BOJ/5001-10000번/YJ_5021.java
View file Open in desktop
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");
//위상정렬 그래프 초기화
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위상정렬은 indegree라는 진입차수 배열과 큐를 사용해야 되는데, 예진님은 위상정렬은 사용하지 않고 DFS로만 풀이했다고 해야될 것 같습니다!

jewan100 reacted with thumbs up emoji
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KodaHye

위상정렬은 indegree라는 진입차수 배열과 큐를 사용

아하! 찾아보니 간선을 제거하는거나 Queue에서 제거하는 그런 코드가 있어야 하는군요
위상정렬를 이해는 했는데 구조가 비슷해서 위상정렬로 푼줄 알았네요 ᄒᄒ

그럼 구조만 위상정렬처럼 되어 있고 (부모-자식의 계층관계)

위상정렬 구조만 이용해서 dfs 로 풀이했다고 이해하면 될까요?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그래프의 구조 자체는 비순환 유향 그래프인 것이고, 문제 풀이 유형으로 DFS나 위상정렬 방식으로 풀이할 수 있는 것으로 이해하면 될 것 같습니다!

yeahdy reacted with thumbs up emoji
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)){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getOrDefault는 null 값 일 때, 두번째 인자를 반환하는 건가요?! if null로 확인하지 않아도 돼서 좋은것 같네요!!!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KodaHye 맞아요! getOrDefault() 유용하게 많이 사용되더라구요

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)){
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);
}
}
180 changes: 180 additions & 0 deletions CodeTree/2019-2020년/YJ_시공의_돌풍.java
View file Open in desktop
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");
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;
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;
}
}
Loading

AltStyle によって変換されたページ (->オリジナル) /