Skip to content

Commit 488631a

Browse files
committed
51주차
1 parent 194324f commit 488631a

File tree

9 files changed

+1092
-0
lines changed

9 files changed

+1092
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.example._51week;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.*;
7+
8+
public class BookSharing {
9+
10+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
12+
public static void main(String[] args) throws IOException {
13+
int testCnt = Integer.parseInt(br.readLine());
14+
15+
StringTokenizer st;
16+
for (int i = 0; i < testCnt; i++) {
17+
solution();
18+
}
19+
}
20+
21+
private static void solution() throws IOException {
22+
// input
23+
StringTokenizer st;
24+
PriorityQueue<int[]> students = new PriorityQueue<>((e1, e2) -> {
25+
int sCompare = Integer.compare(e1[0], e2[0]);
26+
if (sCompare == 0) {
27+
return Integer.compare(e1[1], e2[1]);
28+
}
29+
30+
return sCompare;
31+
});
32+
33+
st = new StringTokenizer(br.readLine());
34+
int bookCnt = Integer.parseInt(st.nextToken());
35+
int studentCnt = Integer.parseInt(st.nextToken());
36+
37+
st = new StringTokenizer(br.readLine());
38+
int s = Integer.parseInt(st.nextToken());
39+
int e = Integer.parseInt(st.nextToken());
40+
41+
students.add(new int[]{s, e});
42+
43+
// solution
44+
int cnt = 0;
45+
for (int sIdx = 1; sIdx <= bookCnt; sIdx++) {
46+
int[] student = new int[1];
47+
48+
while (!students.isEmpty()) {
49+
student = students.poll();
50+
int start = student[0];
51+
int end = student[1];
52+
53+
if (start >= sIdx) {
54+
sIdx = start;
55+
cnt++;
56+
break;
57+
}
58+
}
59+
}
60+
61+
System.out.println(cnt);
62+
}
63+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.example._51week;
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 ControllingRobot {
10+
11+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
13+
private static final int LEFT = 0;
14+
private static final int RIGHT = 1;
15+
private static int[][][] dp;
16+
private static int[][] map;
17+
private static int rowSize;
18+
private static int colSize;
19+
20+
public static void main(String[] args) throws IOException {
21+
StringTokenizer st = new StringTokenizer(br.readLine());
22+
rowSize = Integer.parseInt(st.nextToken());
23+
colSize = Integer.parseInt(st.nextToken());
24+
25+
map = new int[rowSize][colSize];
26+
dp = new int[2][rowSize][colSize];
27+
for (int row = 0; row < rowSize; row++) {
28+
map[row] = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
29+
}
30+
31+
dp[LEFT][0][0] = map[0][0];
32+
dp[RIGHT][0][0] = map[0][0];
33+
for (int col = 1; col < colSize; col++) {
34+
dp[LEFT][0][col] = map[0][col] + dp[LEFT][0][col - 1];
35+
dp[RIGHT][0][col] = map[0][col] + dp[RIGHT][0][col - 1];
36+
}
37+
38+
for (int row = 1; row < rowSize; row++) {
39+
// L -> R
40+
for (int col = 0; col < colSize; col++) {
41+
int left = col - 1 >= 0 ? dp[LEFT][row][col - 1] : Integer.MIN_VALUE;
42+
int leftup = dp[LEFT][row - 1][col];
43+
int rightup = dp[RIGHT][row - 1][col];
44+
45+
dp[LEFT][row][col] = Math.max(left, Math.max(leftup, rightup)) + map[row][col];
46+
}
47+
48+
// R -> L
49+
for (int col = colSize - 1; col >= 0; col--) {
50+
int right = col + 1 < colSize ? dp[RIGHT][row][col + 1] : Integer.MIN_VALUE;
51+
int leftup = dp[LEFT][row - 1][col];
52+
int rightup = dp[RIGHT][row - 1][col];
53+
54+
dp[RIGHT][row][col] = Math.max(right, Math.max(leftup, rightup)) + map[row][col];
55+
}
56+
57+
}
58+
59+
System.out.println(Math.max(dp[LEFT][rowSize - 1][colSize - 1], dp[RIGHT][rowSize - 1][colSize - 1]));
60+
}
61+
62+
private static void printRow(int row) {
63+
System.out.println(row+"행");
64+
System.out.println("left: ");
65+
for (int col = 0; col < colSize; col++) {
66+
System.out.print(dp[LEFT][row][col]+" ");
67+
}
68+
System.out.println();
69+
70+
System.out.println("right: ");
71+
for (int col = 0; col < colSize; col++) {
72+
System.out.print(dp[RIGHT][row][col]+" ");
73+
}
74+
System.out.println();
75+
System.out.println();
76+
}
77+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package org.example._51week;
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 IceBerg {
10+
11+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
13+
private static final int[] dr = {-1, 0, 1, 0};
14+
private static final int[] dc = {0, 1, 0, -1};
15+
16+
private static int[][] map;
17+
private static int rowSize;
18+
private static int colSize;
19+
20+
21+
public static void main(String[] args) throws IOException {
22+
StringTokenizer st = new StringTokenizer(br.readLine());
23+
rowSize = Integer.parseInt(st.nextToken());
24+
colSize = Integer.parseInt(st.nextToken());
25+
map = new int[rowSize][colSize];
26+
27+
for (int i = 0; i < rowSize; i++) {
28+
map[i] = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
29+
}
30+
31+
for (int year = 1; ; year++) {
32+
int iceBergSize = melt();
33+
34+
if (iceBergSize == 0) {
35+
System.out.println(0);
36+
return;
37+
}
38+
39+
boolean isSplitted = inspect();
40+
if (isSplitted) {
41+
System.out.println(year);
42+
return;
43+
}
44+
}
45+
}
46+
47+
private static int melt() {
48+
int[][] temp = new int[rowSize][colSize];
49+
50+
for (int row = 1; row < rowSize; row++) {
51+
for (int col = 1; col < colSize; col++) {
52+
for (int i = 0; i < 4; i++) {
53+
if (map[row][col] == 0) {
54+
continue;
55+
}
56+
57+
int nextRow = row + dr[i];
58+
int nextCol = col + dc[i];
59+
60+
if (nextRow < 0 || nextRow >= rowSize || nextCol < 0 || nextCol >= colSize) {
61+
continue;
62+
}
63+
64+
if (map[nextRow][nextCol] == 0) {
65+
temp[row][col]++;
66+
}
67+
}
68+
}
69+
}
70+
71+
int iceBergSize = 0;
72+
for (int row = 1; row < rowSize; row++) {
73+
for (int col = 1; col < colSize; col++) {
74+
map[row][col] -= temp[row][col];
75+
if(map[row][col] < 0) {
76+
map[row][col] =0;
77+
}
78+
79+
if (map[row][col] != 0) {
80+
iceBergSize++;
81+
}
82+
}
83+
}
84+
85+
return iceBergSize;
86+
}
87+
88+
private static boolean inspect() {
89+
boolean[][] visited = new boolean[rowSize][colSize];
90+
int iceBergCnt = 0;
91+
92+
for (int row = 1; row < rowSize; row++) {
93+
for (int col = 1; col < colSize; col++) {
94+
if (map[row][col] == 0) {
95+
continue;
96+
}
97+
98+
if (visited[row][col]) {
99+
continue;
100+
}
101+
102+
iceBergCnt++;
103+
if (iceBergCnt >= 2) {
104+
return true;
105+
}
106+
dfs(row, col, visited);
107+
}
108+
}
109+
110+
return false;
111+
}
112+
113+
private static void dfs(int row, int col, boolean[][] visited) {
114+
for (int i = 0; i < 4; i++) {
115+
int nextRow = row + dr[i];
116+
int nextCol = col + dc[i];
117+
118+
if (nextRow < 0 || nextRow >= rowSize || nextCol < 0 || nextCol >= colSize) {
119+
continue;
120+
}
121+
122+
if (map[nextRow][nextCol] == 0) {
123+
continue;
124+
}
125+
126+
if (visited[nextRow][nextCol]) {
127+
continue;
128+
}
129+
130+
visited[nextRow][nextCol] = true;
131+
dfs(nextRow, nextCol, visited);
132+
}
133+
}
134+
}

0 commit comments

Comments
 (0)