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

Commit 6fae217

Browse files
Merge pull request #315 from GreatAlgorithm-Study/jimin
[23주차] 손지민
2 parents 27e5bed + c022392 commit 6fae217

File tree

6 files changed

+504
-0
lines changed

6 files changed

+504
-0
lines changed

‎BOJ/1000-5000번/JM_1135.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.ArrayList;
5+
import java.util.PriorityQueue;
6+
import java.util.StringTokenizer;
7+
8+
/**
9+
* 완전 틀림, 해결책 제시 X
10+
*/
11+
public class JM_1135 {
12+
static class Node {
13+
int num;
14+
int time;
15+
16+
public Node(int num, int time) {
17+
this.num = num;
18+
this.time = time;
19+
}
20+
}
21+
static int N;
22+
static ArrayList<Integer>[] trees;
23+
24+
private static int dfs(int curr) {
25+
int maxTime = 0;
26+
PriorityQueue<Node> queue = new PriorityQueue<>((o1, o2) -> o2.time - o1.time);
27+
for(int next: trees[curr]) {
28+
int nextTime = dfs(next);
29+
queue.offer(new Node(next, nextTime));
30+
}
31+
32+
int t = 1;
33+
while(!queue.isEmpty()) {
34+
Node next = queue.poll();
35+
maxTime = Math.max(maxTime, next.time + t);
36+
t++;
37+
}
38+
39+
return maxTime;
40+
}
41+
42+
public static void main(String[] args) throws IOException {
43+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
44+
StringTokenizer st = new StringTokenizer(br.readLine());
45+
N = Integer.parseInt(st.nextToken());
46+
trees = new ArrayList[N];
47+
48+
st = new StringTokenizer(br.readLine());
49+
for (int i = 0; i < N; i++) {
50+
trees[i] = new ArrayList<>();
51+
int parent = Integer.parseInt(st.nextToken());
52+
if(parent != -1) trees[parent].add(i);
53+
}
54+
System.out.println(dfs(0));
55+
}
56+
}

‎BOJ/1000-5000번/JM_1450.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.ArrayList;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.StringTokenizer;
8+
9+
/**
10+
* 완전 틀림, 해결책 제시 X
11+
*/
12+
public class JM_1450 {
13+
static int N;
14+
static int C;
15+
static int[] W;
16+
17+
private static int upperBound(int target, List<Integer> A) {
18+
int lo = 0;
19+
int hi = A.size() - 1;
20+
int ans = A.size();
21+
while (lo <= hi) {
22+
int mid = (lo + hi) / 2;
23+
if(target < A.get(mid)) {
24+
ans = mid;
25+
hi = mid - 1;
26+
}
27+
else lo = mid + 1;
28+
}
29+
return ans;
30+
}
31+
32+
private static void combi(int start, int end, int sum, List<Integer> combiSums) {
33+
if(sum > C) return;
34+
if(start == end) {
35+
combiSums.add(sum);
36+
return;
37+
}
38+
combi(start + 1, end, sum, combiSums);
39+
combi(start + 1, end, sum + W[start], combiSums);
40+
}
41+
42+
private static int solve() {
43+
List<Integer> left = new ArrayList<>();
44+
List<Integer> right = new ArrayList<>();
45+
46+
combi(0, N / 2, 0, left);
47+
combi(N / 2, N, 0, right);
48+
49+
Collections.sort(right);
50+
51+
int count = 0;
52+
for(int leftSum : left) {
53+
count += upperBound(C - leftSum, right);
54+
}
55+
return count;
56+
}
57+
58+
public static void main(String[] args) throws IOException {
59+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
60+
StringTokenizer st = new StringTokenizer(br.readLine());
61+
N = Integer.parseInt(st.nextToken());
62+
C = Integer.parseInt(st.nextToken());
63+
W = new int[N];
64+
65+
st = new StringTokenizer(br.readLine());
66+
for (int i = 0; i < N; i++) {
67+
W[i] = Integer.parseInt(st.nextToken());
68+
}
69+
70+
System.out.println(solve());
71+
}
72+
}

‎BOJ/10001-15000번/JM_19237.java

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.PriorityQueue;
5+
import java.util.StringTokenizer;
6+
7+
/**
8+
* 25% 틀림 -> 1000초 이상 아니고, 초과로
9+
*/
10+
public class JM_19237 {
11+
static class Smell {
12+
int sn;
13+
int k;
14+
15+
public Smell(int sn, int k) {
16+
this.sn = sn;
17+
this.k = k;
18+
}
19+
20+
public void set(int sn, int k) {
21+
this.sn = sn;
22+
this.k = k;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return "(" + sn + ", " + k + ')';
28+
}
29+
}
30+
static class Shark implements Comparable<Shark> {
31+
int sn;
32+
int y, x;
33+
int d;
34+
35+
public Shark(int sn, int y, int x, int d) {
36+
this.y = y;
37+
this.x = x;
38+
this.sn = sn;
39+
this.d = d;
40+
}
41+
42+
@Override
43+
public int compareTo(Shark o) {
44+
return this.sn - o.sn;
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return "Shark{" +
50+
"sn=" + sn +
51+
", y=" + y +
52+
", x=" + x +
53+
", d=" + d +
54+
'}';
55+
}
56+
}
57+
static int N; // 격자 NxN
58+
static int M; // 상어 개수
59+
static int K; // 냄새 사라지는 시간
60+
static Smell[][] map;
61+
static int[][][] sharkPriorityDir;
62+
static PriorityQueue<Shark> sharks;
63+
static PriorityQueue<Shark> nextShanks;
64+
static final int MAX_TIME = 1000;
65+
static final int[][] DIR = {{0, 0}, {-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 상,하,좌,우
66+
67+
private static boolean inRange(int y, int x) {
68+
return 0 <= y && y < N && 0 <= x && x < N;
69+
}
70+
71+
private static void reduceKAll() {
72+
for (int i = 0; i < N; i++) {
73+
for (int j = 0; j < N; j++) {
74+
if(map[i][j].sn == 0) continue;
75+
map[i][j].k--;
76+
if(map[i][j].k == 0) map[i][j].sn = 0;
77+
}
78+
}
79+
}
80+
81+
private static void move() {
82+
while (!nextShanks.isEmpty()) {
83+
Shark shank = nextShanks.poll();
84+
if(map[shank.y][shank.x].sn == 0 || map[shank.y][shank.x].sn == shank.sn) {
85+
map[shank.y][shank.x].set(shank.sn, K + 1); // K + 1로 초기화 필요
86+
sharks.add(shank);
87+
}
88+
}
89+
}
90+
91+
private static void addNextShark() {
92+
while (!sharks.isEmpty()) {
93+
Shark curr = sharks.poll();
94+
Shark prev = null; // 가능한 이동 경로가 없을 경우, 냄새가 있는 칸
95+
Shark next = null;
96+
97+
for(int nd : sharkPriorityDir[curr.sn][curr.d]) {
98+
if(nd == 0) continue;
99+
int ny = curr.y + DIR[nd][0];
100+
int nx = curr.x + DIR[nd][1];
101+
if(!inRange(ny,nx)) continue;
102+
103+
if(next == null && map[ny][nx].sn == 0) {
104+
next = new Shark(curr.sn, ny, nx, nd);
105+
}
106+
if(prev == null && map[ny][nx].sn == curr.sn) {
107+
prev = new Shark(curr.sn, ny, nx, nd);
108+
}
109+
}
110+
if(next == null) nextShanks.add(prev);
111+
else nextShanks.add(next);
112+
}
113+
}
114+
115+
private static int solve() {
116+
int time = 0;
117+
while (true) {
118+
addNextShark(); // 1. 우선순위 방향 결정
119+
move(); // 2. 이동
120+
reduceKAll(); // 3. 현재 k 시간 감소시키기
121+
122+
time++;
123+
if(time > MAX_TIME) return -1;
124+
if(sharks.size() == 1) break;
125+
}
126+
return time;
127+
}
128+
129+
public static void init(int[][] tmpMap, int[] startDir) {
130+
for (int i = 0; i < N; i++) {
131+
for (int j = 0; j < N; j++) {
132+
map[i][j] = new Smell(0, 0);
133+
if(tmpMap[i][j] != 0) {
134+
int n = tmpMap[i][j];
135+
map[i][j].set(n, K);
136+
sharks.add(new Shark(tmpMap[i][j], i, j, startDir[n]));
137+
}
138+
}
139+
}
140+
}
141+
142+
public static void main(String[] args) throws IOException {
143+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
144+
StringTokenizer st = new StringTokenizer(br.readLine());
145+
N = Integer.parseInt(st.nextToken());
146+
M = Integer.parseInt(st.nextToken());
147+
K = Integer.parseInt(st.nextToken());
148+
map = new Smell[N][N];
149+
sharks = new PriorityQueue<>();
150+
nextShanks = new PriorityQueue<>();
151+
152+
int[][] tmpMap = new int[N][N];
153+
for (int i = 0; i < N; i++) {
154+
st = new StringTokenizer(br.readLine());
155+
for (int j = 0; j < N; j++) {
156+
tmpMap[i][j] = Integer.parseInt(st.nextToken());
157+
}
158+
}
159+
160+
int[] startDir = new int[M + 1];
161+
st = new StringTokenizer(br.readLine());
162+
for (int i = 1; i <= M; i++) {
163+
startDir[i] = Integer.parseInt(st.nextToken());
164+
}
165+
166+
sharkPriorityDir = new int[M + 1][5][5];
167+
for (int m = 1; m <= M; m++) {
168+
for (int i = 1; i <= 4; i++) {
169+
st = new StringTokenizer(br.readLine());
170+
for (int j = 1; j <= 4; j++) {
171+
sharkPriorityDir[m][i][j] = Integer.parseInt(st.nextToken());
172+
}
173+
}
174+
}
175+
176+
init(tmpMap, startDir);
177+
System.out.println(solve());
178+
}
179+
}

0 commit comments

Comments
(0)

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