Skip to content

Commit 19aef82

Browse files
author
최재익
committedSep 17, 2024
*
1 parent 1d421b0 commit 19aef82

File tree

14 files changed

+932
-1
lines changed

14 files changed

+932
-1
lines changed
 
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package week5.암호생성기;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.*;
7+
8+
public class SWEA_1225 {
9+
public static void main(String[] args) throws IOException {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
StringTokenizer st;
12+
13+
14+
for(int tc=0; tc<10; tc++){
15+
br.readLine();
16+
st = new StringTokenizer(br.readLine());
17+
18+
Deque<Integer> dq = new ArrayDeque<>();
19+
for(int i=0; i<8; i++){
20+
dq.add(Integer.parseInt(st.nextToken()));
21+
}
22+
23+
int minus = 1;
24+
int n=0;
25+
while(true){
26+
n = (minus%5==0)?5:minus%5;
27+
int num = dq.pollFirst()-(n);
28+
minus++;
29+
30+
if(num<=0){
31+
dq.add(0);
32+
break;
33+
}
34+
35+
dq.add(num);
36+
}
37+
38+
List<Integer> list = new ArrayList<>(dq);
39+
System.out.print("#"+(tc+1)+" ");
40+
for(int i=0; i<list.size(); i++){
41+
System.out.print(list.get(i)+" ");
42+
}
43+
System.out.println();
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package week6.장훈이의높은선반;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
public class SWEA_1486 {
9+
static int min;
10+
static boolean[] selected;
11+
static int n;
12+
static int top;
13+
static int[] arr;
14+
public static void main(String[] args) throws IOException {
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
StringTokenizer st;
17+
18+
int T = Integer.parseInt(br.readLine());
19+
20+
for(int tc=0; tc<T; tc++){
21+
st = new StringTokenizer(br.readLine());
22+
23+
n = Integer.parseInt(st.nextToken());
24+
top = Integer.parseInt(st.nextToken());
25+
26+
arr = new int[n];
27+
st = new StringTokenizer(br.readLine());
28+
for(int i=0; i<n; i++){
29+
arr[i] = Integer.parseInt(st.nextToken());
30+
}
31+
32+
selected = new boolean[n];
33+
min = Integer.MAX_VALUE;
34+
35+
subset(0, 0);
36+
37+
System.out.println("#"+(tc+1)+" "+(min-top));
38+
}
39+
}
40+
41+
static void subset(int depth, int length){
42+
if(depth == n){
43+
if(length<top)return;
44+
45+
min = Math.min(min, length);
46+
return;
47+
}
48+
49+
subset(depth+1, length+arr[depth]);
50+
subset(depth+1, length);
51+
}
52+
}
+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package week7.벌꿀채취;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.StringTokenizer;
9+
10+
public class SWEA_2115 {
11+
static class Honey{
12+
int row;
13+
int col;
14+
int amount;
15+
boolean visited;
16+
17+
public Honey(int row, int col, int amount, boolean visited){
18+
this.row = row;
19+
this.col = col;
20+
this.amount = amount;
21+
this.visited = visited;
22+
}
23+
}
24+
25+
static int price;
26+
static int price_b;
27+
static int subset_sum;
28+
static int max_price;
29+
static int n, m, c;
30+
static Honey[][] map;
31+
static boolean[][] visited;
32+
static List<Honey> a_honey;
33+
static List<Honey> b_honey;
34+
public static void main(String[] args) throws IOException {
35+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
36+
StringTokenizer st;
37+
int T = Integer.parseInt(br.readLine());
38+
39+
for(int tc=0; tc<T; tc++){
40+
st = new StringTokenizer(br.readLine());
41+
42+
n = Integer.parseInt(st.nextToken());
43+
m = Integer.parseInt(st.nextToken());
44+
c = Integer.parseInt(st.nextToken());
45+
46+
map = new Honey[n][n];
47+
for(int i=0; i<n; i++){
48+
st = new StringTokenizer(br.readLine());
49+
for(int j=0; j<n; j++){
50+
map[i][j] = new Honey(i, j, Integer.parseInt(st.nextToken()), false);
51+
}
52+
}
53+
54+
visited = new boolean[n][n];
55+
a_honey = new ArrayList<>();
56+
b_honey = new ArrayList<>();
57+
max_price = 0;
58+
59+
combination(0, 0);
60+
61+
System.out.println(max_price);
62+
}
63+
}
64+
65+
static int getSum(List<Honey> honeys, boolean[] selected){
66+
int sum = 0;
67+
for(int i=0; i<honeys.size(); i++){
68+
if(selected[i]){
69+
sum+=honeys.get(i).amount;
70+
}
71+
}
72+
return sum;
73+
}
74+
75+
static void subset(List<Honey> honey, int depth, boolean[] selected){
76+
if(getSum(honey, selected) > c)return;
77+
78+
if(depth == m){
79+
price = Math.max(subset_sum, getSum(honey, selected));
80+
return;
81+
}
82+
83+
selected[depth] = true;
84+
subset(honey, depth+1, selected);
85+
86+
selected[depth] = false;
87+
subset(honey, depth+1, selected);
88+
}
89+
90+
static void updateMaxPrice(List<List<Honey>> selected){
91+
//System.out.println(selected.size());
92+
subset_sum = 0;
93+
for(List<Honey> honeys : selected){
94+
price = 0;
95+
subset(honeys, 0, new boolean[m]);
96+
subset_sum += price;
97+
}
98+
max_price = Math.max(subset_sum, max_price);
99+
}
100+
101+
static void combination(int startRow, int depth){
102+
if(depth==2){
103+
// System.out.println("==================");
104+
// System.out.println();
105+
// System.out.println("[a의 꿀통]");
106+
// for(Honey a : a_honey){
107+
// System.out.println(a.row+" "+a.col);
108+
// }
109+
// System.out.println();
110+
//
111+
// System.out.println("[b의 꿀통]");
112+
// for(Honey b : b_honey){
113+
// System.out.println(b.row+" "+b.col);
114+
// }
115+
// System.out.println();
116+
117+
//updateMaxPrice();
118+
119+
List<List<Honey>> selected = new ArrayList<>();
120+
selected.add(a_honey);
121+
selected.add(b_honey);
122+
123+
updateMaxPrice(selected);
124+
125+
return;
126+
}
127+
128+
for(int i=startRow; i<n; i++){
129+
for(int j=0; j<n; j++){
130+
if(map[i][j].visited)continue;
131+
132+
int nextCol = j;
133+
boolean flag = true;
134+
int size = m;
135+
136+
//m개의 연속된 꿀통 고르기
137+
while(size-- > 1){
138+
nextCol++;
139+
if((nextCol>=n && size>0) || map[i][nextCol].visited){
140+
flag = false;
141+
break;
142+
}
143+
}
144+
145+
if(flag){
146+
if(a_honey.size()==0){
147+
addHoney(a_honey, i, j, nextCol);
148+
combination(i, depth+1);
149+
rollBack(a_honey);
150+
}
151+
else{
152+
addHoney(b_honey, i, j, nextCol);
153+
combination(i, depth+1);
154+
rollBack(b_honey);
155+
}
156+
157+
}
158+
}
159+
}
160+
}
161+
162+
static void addHoney(List<Honey> honeys, int row, int startCol, int endCol){
163+
for(int i=startCol; i<=endCol; i++){
164+
map[row][i].visited = true;
165+
honeys.add(map[row][i]);
166+
}
167+
}
168+
169+
static void rollBack(List<Honey> honeys){
170+
for(Honey h : honeys){
171+
int row = h.row;
172+
int col = h.col;
173+
map[row][col].visited = false;
174+
}
175+
honeys.clear();
176+
}
177+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package week7.벌꿀채취;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
/**
9+
* @author onyoo
10+
* @performance
11+
* @category
12+
* @note
13+
* 가로로 연속되도록 M개의 벌통을 선택 (겹치면 안됨)
14+
* 두 일꾼이 채취할 수 있는 꿀의 최대 양은 C를 넘지 않아야 한다
15+
* 꿀의 가치는 꿀의 양의 제곱만큼이다
16+
* 여기에서 "수익의 합이 최대" 가 되는 경우를 찾아야 한다
17+
* 이런 문제가 나오면 어떤걸 구해야하는지에 대해 집중해야한다.
18+
* 다른거 다 필요없고 최대값을 구해야하기 때문에,가장 최대가 되는 값이 나오도록 값을 필터링 하면 된다
19+
* 합계가 C보다 작으면서 제곱의 합이 최대가 되도록 한다. 즉, 최대 값을 갱신하면서 최대 값 보다 작다면 애초에 계산할 필요가 없다
20+
* 이러한 경우 작은 경우의 이후 계산과정을 진행할 수 없도록 하는 것이 좋다 -> 불필요한 계산을 줄이기 위하여
21+
* 여기서 또 중요한 아이디어는 2차원 배열을 1차원 배열처럼 펼쳐놓고 생각하는 것이다
22+
* 2차원 배열에서 M개의 연속된 벌통을 선택하는 경우의 수를 구하기에는 복잡하기 때문에
23+
* 2차원 배열을 1차원 배열처럼 생각한 뒤, 그걸 좌표계로 변경하는 아이디어를 생각해내야한다 이건,자주 사용되니까 알아두자
24+
* @see
25+
* https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5V4A46AdIDFAWu
26+
* @since 2023-10-11
27+
**/
28+
public class SWEA_2115_ANSWER {
29+
static int T; // 테스트 케이스의 개수
30+
static int N,M,C; // 벌통의 크기 , 선택할 수 있는 벌통의 개수 , 채취할 수 있는 꿀의 최대 양
31+
static int[][] map; // 벌통
32+
static int[] selected; // 선택한 벌통 저장배열
33+
static int sumA,sumB; // A
34+
static int answer;
35+
public static void main(String[] args) throws IOException {
36+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
37+
StringTokenizer st;
38+
39+
T = Integer.parseInt(br.readLine());
40+
41+
for(int t=1;t<=T;t++){
42+
st = new StringTokenizer(br.readLine()," ");
43+
N = Integer.parseInt(st.nextToken()); // 벌통의 크기
44+
M = Integer.parseInt(st.nextToken()); // 하나의 일꾼이 선택할 수 있는 벌통의 개수
45+
C = Integer.parseInt(st.nextToken()); // 채취할 수 있는 꿀의 최대 양
46+
47+
map = new int[N][N]; // 초기화
48+
selected = new int[2]; // 두명의 일꾼의 시작 지점
49+
answer = 0;
50+
51+
for(int i=0;i<N;i++){
52+
st = new StringTokenizer(br.readLine()," ");
53+
for(int j=0;j<N;j++){
54+
map[i][j] = Integer.parseInt(st.nextToken());
55+
}
56+
}
57+
dfs(0,0);
58+
System.out.printf("#%d %d \n",t,answer);
59+
}
60+
}
61+
static void dfs(int depth,int idx){
62+
// 두개의 벌통만 선택하고 그 뒤에 연속으로 M-1 개를 자동으로 고르게 하자
63+
if(depth == 2){
64+
// 두개의 벌통을 모두 선택했다
65+
if(selected[0] >= selected[1] - (M-1)) return;
66+
// 벌통 선택은 어짜피 일꾼의 앞뒤가 없기 때문에 뒤 부분만 확인해도 된다
67+
if((selected[0] + M - 1) / N != selected[0] / N || (selected[1] + M - 1) / N != selected[1] / N) return;
68+
// 같은 열에 있는지 확인해야한다
69+
// 채취하는 벌통에서 가장 최대로 나올 수 있는 값을 구해보자
70+
int honey = getHoney(selected[0],selected[1]); //
71+
answer = Math.max(answer,honey);
72+
return;
73+
}
74+
75+
for(int i=idx;i<N*N; i++){
76+
selected[depth] = i;
77+
dfs(depth+1,i+1);
78+
}
79+
}
80+
static int getHoney(int a,int b){
81+
//최대 벌꿀양을 구하자
82+
sumA = 0;
83+
for(int r=1;r<=M;r++){
84+
// i 만큼 뽑아보자
85+
combination(a,a+M,r,0,0,0,1);
86+
}
87+
sumB = 0;
88+
for(int r=1;r<=M;r++){
89+
combination(b,b+M,r,0,0,0,2);
90+
}
91+
92+
return sumA + sumB;
93+
}
94+
static void combination(int start,int end,int r,int depth,int sum,int rev,int type){
95+
if(depth == r){
96+
if(sum > C) return;
97+
if(type == 1) sumA = Math.max(sumA,rev);
98+
else sumB = Math.max(sumB,rev);
99+
return;
100+
}
101+
for(int i=start;i<end;i++){ // 연속으로 꿀을 채취해야하기 때문에 visited 배열이 필요 없다
102+
int h = map[i/N][i%N];
103+
combination(i+1,end,r,depth+1,sum + h,rev + (int) Math.pow(h,2),type);
104+
}
105+
}
106+
107+
108+
}
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package week7.최대상금;
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.StringTokenizer;
6+
7+
public class SWEA_1244 {
8+
static int answer;
9+
static int[] map;
10+
static int n;
11+
public static void main(String[] args) throws IOException {
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
int T = Integer.parseInt(br.readLine());
14+
15+
for(int tc=0; tc<T; tc++){
16+
StringTokenizer st = new StringTokenizer(br.readLine());
17+
char[] input = st.nextToken().toCharArray();
18+
n = Integer.parseInt(st.nextToken());
19+
20+
map = new int[input.length];
21+
for(int i=0; i<map.length; i++){
22+
map[i] = input[i]-'0';
23+
}
24+
25+
n = Math.min(n, map.length);
26+
27+
answer = 0;
28+
dfs(0);
29+
30+
System.out.println("#"+(tc+1)+" "+answer);
31+
}
32+
}
33+
34+
static void dfs(int depth){
35+
if(depth == n){
36+
StringBuilder sb = new StringBuilder();
37+
for(int num : map){
38+
sb.append(num);
39+
}
40+
41+
answer = Math.max(Integer.parseInt(sb.toString()), answer);
42+
return;
43+
}
44+
45+
for(int i=0; i<map.length-1; i++){
46+
for(int j=i+1; j<map.length; j++){
47+
int temp = map[i];
48+
map[i] = map[j];
49+
map[j] = temp;
50+
51+
dfs(depth+1);
52+
53+
temp = map[i];
54+
map[i] = map[j];
55+
map[j] = temp;
56+
}
57+
}
58+
}
59+
}

‎jaeik/week7/프로세서연결하기/SWEA_1767.java

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ static void dfs(int idx, int length){
7676
int l = 0;
7777
boolean isConnected = false;
7878

79+
//연결
7980
while(true){
8081
row += dr[i];
8182
col += dc[i];
@@ -94,6 +95,7 @@ static void dfs(int idx, int length){
9495
dfs(idx+1, length+l);
9596
}
9697

98+
//연결 해제
9799
while(true){
98100
row -= dr[i];
99101
col -= dc[i];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package week8.색종이붙이기;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
public class BOJ_17136 {
9+
static int[][] map;
10+
static int[] paper = {0, 5, 5, 5, 5, 5};
11+
static int min = Integer.MAX_VALUE;
12+
public static void main(String[] args) throws IOException {
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
StringTokenizer st;
15+
16+
map = new int[10][10];
17+
for(int i=0; i<10; i++){
18+
st = new StringTokenizer(br.readLine());
19+
for(int j=0; j<10; j++){
20+
map[i][j] = Integer.parseInt(st.nextToken());
21+
}
22+
}
23+
24+
dfs(0, 0, 0);
25+
26+
if(min == Integer.MAX_VALUE)min=-1;
27+
28+
System.out.println(min);
29+
}
30+
31+
static void dfs(int row, int col, int count){
32+
if(row==9 && col>9){
33+
min = Math.min(min, count);
34+
return;
35+
}
36+
37+
if(min<=count)return;
38+
39+
if(col>9){
40+
dfs(row+1, 0, count);
41+
return;
42+
}
43+
44+
if(map[row][col]==1){
45+
for(int i=5; i>=1; i--){
46+
if(paper[i]>0 && canAttach(row, col, i)){
47+
attach(row, col, i, 0);
48+
paper[i]--;
49+
dfs(row, col+1, count+1);
50+
attach(row, col, i, 1);
51+
paper[i]++;
52+
}
53+
}
54+
}
55+
else {
56+
dfs(row, col+1, count);
57+
}
58+
}
59+
60+
static void attach(int row, int col, int size, int state){
61+
for(int i=row; i<row+size; i++){
62+
for(int j=col; j<col+size; j++){
63+
map[i][j] = state;
64+
}
65+
}
66+
}
67+
68+
static boolean canAttach(int row, int col, int size){
69+
for(int i=row; i<row+size; i++){
70+
for(int j=col; j<col+size; j++){
71+
if(i<0 || j<0 || i>=10 || j>=10)return false;
72+
if(map[i][j]==0)return false;
73+
}
74+
}
75+
return true;
76+
}
77+
}

‎jaeik/week8/야구/BOJ_17281.java

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package week8.야구;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
public class BOJ_17281 {
9+
static int[] lineup;
10+
static boolean[] visited;
11+
static int result = 0;
12+
static int score;
13+
static int out;
14+
static boolean[] base;
15+
static int batter;
16+
static int N;
17+
static int[][] inning;
18+
public static void main(String[] args) throws IOException {
19+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
20+
StringTokenizer st;
21+
22+
N = Integer.parseInt(br.readLine());
23+
inning = new int[N][9];
24+
for(int i=0; i<N; i++){
25+
st = new StringTokenizer(br.readLine());
26+
for(int j=0; j<9; j++){
27+
inning[i][j] = Integer.parseInt(st.nextToken());
28+
}
29+
}
30+
31+
visited = new boolean[9];
32+
lineup = new int[9];
33+
visited[3] = true;
34+
lineup[3] = 0;
35+
36+
permutation(0);
37+
38+
System.out.println(result);
39+
}
40+
41+
static void permutation(int num){
42+
if(num == 9){
43+
game();
44+
return;
45+
}
46+
47+
for(int i=1; i<9; i++){
48+
if(!visited[i]){
49+
visited[i] = true;
50+
lineup[i] = num;
51+
permutation(num+1);
52+
visited[i] = false;
53+
}
54+
}
55+
}
56+
57+
static void game(){
58+
score = 0;
59+
batter = 0;
60+
61+
for(int i=0; i<N; i++){
62+
out = 0;
63+
base = new boolean[4];
64+
65+
while(out!=3){
66+
for(int j=batter; j<9; j++){
67+
int hit = inning[i][lineup[j]];
68+
attack(hit);
69+
70+
//아웃 카운트가 3이면 다음 이닝으로
71+
if(out==3) {
72+
batter = (j+1)%9;
73+
break;
74+
}
75+
}
76+
}
77+
}
78+
79+
result = Math.max(result, score);
80+
}
81+
82+
static void attack(int hit){
83+
switch (hit){
84+
case 0:
85+
out++;
86+
break;
87+
case 1:
88+
updateBase(1);
89+
break;
90+
case 2:
91+
updateBase(2);
92+
break;
93+
case 3:
94+
updateBase(3);
95+
break;
96+
case 4:
97+
updateBase(4);
98+
break;
99+
}
100+
}
101+
102+
static void updateBase(int hit){
103+
//주자들 위치 및 점수 업데이트
104+
for(int i=3; i>=1; i--){
105+
if(base[i]){
106+
if(i+hit>3){
107+
base[i] = false;
108+
score++;
109+
}else{
110+
base[i] = false;
111+
base[i+hit] = true;
112+
}
113+
}
114+
}
115+
//현재 타자 위치 및 점수 업데이트
116+
if(hit==4){
117+
score++;
118+
}else{
119+
base[hit] = true;
120+
}
121+
}
122+
}

‎jaeik/week9/BOJ_10828.java

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package week9;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.Stack;
7+
import java.util.StringTokenizer;
8+
9+
public class BOJ_10828 {
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
int N = Integer.parseInt(br.readLine());
13+
StringTokenizer st;
14+
15+
Stack<Integer> stack = new Stack<>();
16+
for(int i=0; i<N; i++){
17+
String[] input = br.readLine().split(" ");
18+
19+
if(input.length==1){
20+
switch (input[0]){
21+
case "top":
22+
if (stack.size() == 0) {
23+
System.out.println(-1);
24+
}else{
25+
System.out.println(stack.peek());
26+
}
27+
break;
28+
29+
case "size":
30+
System.out.println(stack.size());
31+
break;
32+
33+
case "empty":
34+
if (stack.isEmpty()) {
35+
System.out.println(1);
36+
} else {
37+
System.out.println(0);
38+
}
39+
break;
40+
41+
case "pop":
42+
if(stack.isEmpty()){
43+
System.out.println(-1);
44+
}else {
45+
System.out.println(stack.pop());
46+
}
47+
break;
48+
}
49+
}else{
50+
stack.push(Integer.parseInt(input[1]));
51+
}
52+
}
53+
}
54+
}

‎jaeik/week9/BOJ_1181.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package week9;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.Arrays;
7+
import java.util.Comparator;
8+
9+
public class BOJ_1181 {
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
int N = Integer.parseInt(br.readLine());
13+
14+
String[] arr = new String[N];
15+
for(int i=0; i<N; i++){
16+
arr[i] = br.readLine();
17+
}
18+
19+
Arrays.sort(arr, new Comparator<String>() {
20+
@Override
21+
public int compare(String o1, String o2) {
22+
if(o1.length() == o2.length())return o1.compareTo(o2);
23+
else return o1.length()-o2.length();
24+
}
25+
});
26+
27+
StringBuilder sb = new StringBuilder();
28+
sb.append(arr[0]).append("\n");
29+
30+
for(int i=1; i<N; i++){
31+
if(!arr[i].equals(arr[i-1]))sb.append(arr[i]).append("\n");
32+
}
33+
34+
System.out.println(sb);
35+
}
36+
}

‎jaeik/week9/BOJ_2751.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package week9;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.Arrays;
7+
8+
public class BOJ_2751 {
9+
public static void main(String[] args) throws IOException {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
int N = Integer.parseInt(br.readLine());
12+
13+
int[] arr = new int[N];
14+
for(int i=0; i<N; i++){
15+
arr[i] = Integer.parseInt(br.readLine());
16+
}
17+
Arrays.sort(arr);
18+
19+
for(int i=0; i<N; i++){
20+
System.out.println(arr[i]);
21+
}
22+
}
23+
}

‎jaeik/week9/적의적/BOJ_12893.java

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package week9.적의적;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.Arrays;
7+
import java.util.StringTokenizer;
8+
9+
public class BOJ_12893 {
10+
static int N, M;
11+
static int[] enemy;
12+
static int[] parent;
13+
public static void main(String[] args) throws IOException {
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
StringTokenizer st = new StringTokenizer(br.readLine());
16+
17+
N = Integer.parseInt(st.nextToken());
18+
M = Integer.parseInt(st.nextToken());
19+
20+
parent = new int[N+1];
21+
for(int i=1; i<=N; i++){
22+
parent[i] = i;
23+
}
24+
enemy = new int[N+1];
25+
26+
int answer = 1;
27+
for(int i=0; i<N; i++){
28+
st = new StringTokenizer(br.readLine());
29+
int x = Integer.parseInt(st.nextToken());
30+
int y = Integer.parseInt(st.nextToken());
31+
32+
int rootX = find(x);
33+
int rootY = find(y);
34+
35+
if(rootX==rootY){
36+
answer = 0;
37+
break;
38+
}
39+
40+
int enemyX = enemy[x];
41+
int enemyY = enemy[y];
42+
43+
if(enemyX != 0){
44+
union(enemyX, rootY);
45+
}else{
46+
enemy[rootX] = rootY;
47+
}
48+
49+
if(enemyY != 0){
50+
union(enemyY, rootX);
51+
}else{
52+
enemy[rootY] = rootX;
53+
}
54+
}
55+
56+
System.out.println(answer);
57+
}
58+
59+
static int find(int x){
60+
if(parent[x]==x)return x;
61+
return parent[x]=find(parent[x]);
62+
}
63+
64+
static void union(int x, int y){
65+
x = find(x);
66+
y = find(y);
67+
68+
if(x==y)return;
69+
70+
parent[y] = x;
71+
}
72+
}

‎jaeik/최소스패닝트리/BOJ_1197.java ‎jaeik/week9/최소스패닝트리/BOJ_1197.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package 최소스패닝트리;
1+
package week9.최소스패닝트리;
22

33
import java.io.BufferedReader;
44
import java.io.IOException;

‎jaeik/week9/친구비/BOJ_16562.java

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package week9.친구비;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
6+
import java.io.BufferedReader;
7+
import java.io.IOException;
8+
import java.io.InputStreamReader;
9+
import java.util.StringTokenizer;
10+
11+
public class BOJ_16562 {
12+
static int numOfRelation;
13+
static int N, M, K;
14+
static int[] parent;
15+
static int[] rank;
16+
static int[] friends;
17+
public static void main(String[] args) throws IOException {
18+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
19+
StringTokenizer st = new StringTokenizer(br.readLine());
20+
21+
N = Integer.parseInt(st.nextToken());
22+
M = Integer.parseInt(st.nextToken());
23+
K = Integer.parseInt(st.nextToken());
24+
25+
parent = new int[N+1];
26+
rank = new int[N+1];
27+
for(int i=1; i<=N; i++){
28+
parent[i] = i;
29+
rank[i] = 1;
30+
}
31+
32+
friends = new int[N+1];
33+
st = new StringTokenizer(br.readLine());
34+
for(int i=1; i<=N; i++){
35+
int cost = Integer.parseInt(st.nextToken());
36+
friends[i] = cost;
37+
}
38+
39+
for(int i=0; i<M; i++){
40+
st = new StringTokenizer(br.readLine());
41+
int x = Integer.parseInt(st.nextToken());
42+
int y = Integer.parseInt(st.nextToken());
43+
44+
union(x, y);
45+
}
46+
47+
numOfRelation = 0;
48+
int cost = getCost();
49+
if(numOfRelation!=N){
50+
System.out.println("Oh no");
51+
}else{
52+
System.out.println(cost);
53+
}
54+
}
55+
56+
static void updateRelation(int num){
57+
for(int i=1; i<=N; i++){
58+
if(parent[i]==num)numOfRelation++;
59+
}
60+
}
61+
62+
static int getCost(){
63+
int cost = 0;
64+
boolean[] visited = new boolean[N+1];
65+
for(int i=1; i<=N; i++){
66+
if(visited[parent[i]])continue;
67+
if(cost+friends[parent[i]]>K)break;
68+
cost += friends[parent[i]];
69+
visited[parent[i]] = true;
70+
updateRelation(parent[i]);
71+
}
72+
return cost;
73+
}
74+
75+
static int find(int x){
76+
if(parent[x]==x)return x;
77+
return parent[x]=find(parent[x]);
78+
}
79+
80+
static void union(int x, int y){
81+
x = find(x);
82+
y = find(y);
83+
84+
if(x==y)return;
85+
86+
if(friends[x] <= friends[y]){
87+
parent[y] = x;
88+
89+
for(int i=1; i<=N; i++){
90+
if(parent[i]==y){
91+
parent[i]=x;
92+
}
93+
}
94+
}else{
95+
parent[x] = y;
96+
for(int i=1; i<=N; i++){
97+
if(parent[i]==x){
98+
parent[i]=y;
99+
}
100+
}
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)
Please sign in to comment.