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 27e5bed

Browse files
Merge pull request #314 from Jewan1120/main
[23μ£Όμ°¨] λ°±μ œμ™„
2 parents 36b368c + 29eb031 commit 27e5bed

File tree

6 files changed

+450
-0
lines changed

6 files changed

+450
-0
lines changed

β€ŽBOJ/1000-5000번/JW_1135.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
4+
public class JW_1135 {
5+
6+
static int n;
7+
static int[] dp;
8+
static ArrayList<ArrayList<Integer>> tree = new ArrayList<>();
9+
10+
public static void main(String[] args) throws Exception {
11+
n = read();
12+
dp = new int[n];
13+
// 트리 ꡬ쑰 λ§Œλ“€κΈ°
14+
for (int i = 0; i < n; i++) {
15+
tree.add(new ArrayList<>());
16+
int parent = read();
17+
// 루트 μ œμ™Έ
18+
if (parent == -1)
19+
continue;
20+
tree.get(parent).add(i);
21+
}
22+
recursive(0);
23+
System.out.println(dp[0]);
24+
}
25+
26+
// μž¬κ·€μ μœΌλ‘œ ν•΄λ‹Ή λ…Έλ“œκ°€ κ±Έλ¦¬λŠ” 계산
27+
private static void recursive(int cur) {
28+
ArrayList<Integer> al = new ArrayList<>();
29+
for (int next : tree.get(cur)) {
30+
recursive(next);
31+
al.add(dp[next]); // νƒμƒ‰ν•œ λ…Έλ“œκ°€ κ°€μ§€λŠ” 값을 μ €μž₯
32+
}
33+
// κ·Έλ¦¬λ””ν•˜κ²Œ, κ°€μž₯ 였래 κ±Έλ¦¬λŠ” λ…Έλ“œλ₯Ό λ¨Όμ € νƒμƒ‰ν•΄μ€˜μ•Ό 함
34+
al.sort(Collections.reverseOrder());
35+
36+
// 1μ΄ˆμ— ν•œ λ²ˆμ”© μ „νŒŒ κ°€λŠ₯
37+
// μ΅œλŒ“κ°’μ΄ κ²°κ΅­ μ΅œμ†Ÿκ°’μ΄ 됨
38+
for (int i = 0; i < al.size(); i++)
39+
dp[cur] = Math.max(dp[cur], al.get(i) + i + 1);
40+
}
41+
42+
private static int read() throws Exception {
43+
int c, n = System.in.read() & 15;
44+
boolean m = n == 13;
45+
if (m)
46+
n = System.in.read() & 15;
47+
while ((c = System.in.read()) >= 48)
48+
n = (n << 3) + (n << 1) + (c & 15);
49+
if (c == 13)
50+
System.in.read();
51+
return m ? ~n + 1 : n;
52+
}
53+
}

β€ŽBOJ/1000-5000번/JW_1450.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import java.util.Map;
2+
import java.util.TreeMap;
3+
4+
public class JW_1450 {
5+
6+
static int n, c;
7+
static TreeMap<Long, Integer> tmA = new TreeMap<>();
8+
static TreeMap<Long, Integer> tmB = new TreeMap<>();
9+
static TreeMap<Long, Integer> prefixSumA = new TreeMap<>();
10+
static TreeMap<Long, Integer> prefixSumB = new TreeMap<>();
11+
12+
public static void main(String[] args) throws Exception {
13+
n = read();
14+
c = read();
15+
int[] aArr = new int[n / 2];
16+
int[] bArr = new int[n - n / 2];
17+
18+
for (int i = 0; i < aArr.length; i++)
19+
aArr[i] = read();
20+
for (int i = 0; i < bArr.length; i++)
21+
bArr[i] = read();
22+
recursive(0, 0, aArr, tmA);
23+
recursive(0, 0, bArr, tmB);
24+
25+
makePrefixSum(tmA, prefixSumA);
26+
makePrefixSum(tmB, prefixSumB);
27+
28+
System.out.println(calculate());
29+
}
30+
31+
private static void recursive(int depth, long sum, int[] arr, TreeMap<Long, Integer> map) {
32+
if (sum > c)
33+
return;
34+
if (depth == arr.length) {
35+
map.put(sum, map.getOrDefault(sum, 0) + 1);
36+
return;
37+
}
38+
recursive(depth + 1, sum + arr[depth], arr, map);
39+
recursive(depth + 1, sum, arr, map);
40+
}
41+
42+
private static void makePrefixSum(TreeMap<Long, Integer> tm, TreeMap<Long, Integer> prefixSum) {
43+
int sum = 0;
44+
for (Map.Entry<Long, Integer> entry : tm.entrySet()) {
45+
sum += entry.getValue();
46+
prefixSum.put(entry.getKey(), sum);
47+
}
48+
}
49+
50+
private static long calculate() {
51+
long count = 0;
52+
53+
for (Map.Entry<Long, Integer> entry : tmA.entrySet()) {
54+
long aSum = entry.getKey();
55+
int aWays = entry.getValue();
56+
Map.Entry<Long, Integer> bEntry = prefixSumB.floorEntry(c - aSum);
57+
if (bEntry != null) {
58+
count += (long) aWays * bEntry.getValue();
59+
}
60+
}
61+
62+
return count;
63+
}
64+
65+
private static int read() throws Exception {
66+
int c, n = System.in.read() & 15;
67+
while ((c = System.in.read()) >= 48)
68+
n = (n << 3) + (n << 1) + (c & 15);
69+
if (c == 13)
70+
System.in.read();
71+
return n;
72+
}
73+
}

β€ŽBOJ/15001-20000번/JW_19237.java

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import java.util.ArrayDeque;
2+
import java.util.Deque;
3+
4+
public class JW_19237 {
5+
6+
// 상어 객체
7+
static class Shark {
8+
int num, idx, dir; // μƒμ–΄μ˜ 번호, 인덱슀, λ°©ν–₯
9+
int[][] priority; // ν•΄λ‹Ή μƒμ–΄μ˜ μœ„μΉ˜μ— λ”°λ₯Έ λ°©ν–₯ μš°μ„  μˆœμœ„
10+
11+
Shark(int num, int idx, int dir, int[][] priority) {
12+
this.num = num;
13+
this.idx = idx;
14+
this.dir = dir;
15+
this.priority = priority;
16+
}
17+
18+
// λ°©ν–₯ μš°μ„  μˆœμœ„ λ°˜ν™˜
19+
int[] getPriority() {
20+
return priority[dir];
21+
}
22+
}
23+
24+
static int n, m, k;
25+
static int[] scentBoard, remainBoard, move; // λƒ„μƒˆ, 남은 μ‹œκ°„, λ°©ν–₯ 벑터
26+
static boolean[] visited;
27+
static Deque<Shark> sharks = new ArrayDeque<>(); // 상어 큐
28+
static Deque<int[]> scentDeque = new ArrayDeque<>(); // λƒ„μƒˆ 큐
29+
30+
public static void main(String[] args) throws Exception {
31+
n = read();
32+
m = read();
33+
k = read();
34+
move = new int[] { 0, -n, n, -1, 1 };
35+
scentBoard = new int[n * n];
36+
remainBoard = new int[n * n];
37+
visited = new boolean[n * n];
38+
39+
// κΈ°λ³Έ 정보 μž…λ ₯
40+
init();
41+
42+
int time = 0;
43+
while (time <= 1000) {
44+
int size = sharks.size();
45+
46+
// 1번 μƒμ–΄λ§Œ λ‚¨μ•˜λ‹€λ©΄ μ’…λ£Œ
47+
if (size == 1) {
48+
System.out.println(time);
49+
return;
50+
}
51+
52+
visited = new boolean[n * n]; // λ°©λ¬Έ λ°°μ—΄ μ΄ˆκΈ°ν™”
53+
// 상어λ₯Ό μž‘μ€ 번호 μˆœλŒ€λ‘œ κΊΌλ‚΄μ„œ 탐색
54+
while (size-- > 0) {
55+
Shark shark = sharks.poll();
56+
int num = shark.num, idx = shark.idx;
57+
int nextDir = getNextDir(shark); // λ‹€μŒ λ°©ν–₯ κ²°μ •
58+
int next = idx + move[nextDir]; // 이동
59+
60+
// 이미 λ„μ°©ν•΄μžˆλŠ” 상어가 μžˆλ‹€λ©΄
61+
// 즉, μžμ‹ λ³΄λ‹€ μž‘μ€ 번호의 상어가 λ„μ°©ν•΄μžˆλ‹€λ©΄ 쫓겨남
62+
if (visited[next])
63+
continue;
64+
65+
visited[next] = true;
66+
shark.idx = next;
67+
shark.dir = nextDir;
68+
sharks.offer(shark); // 상어 큐에 μ‚½μž…
69+
scentDeque.offer(new int[] { next, num }); // λƒ„μƒˆ 큐에 μ‚½μž…
70+
}
71+
removeScent(); // λƒ„μƒˆ 카운트 κ°μ†Œ
72+
putScent(); // λƒ„μƒˆ 배열에 적용
73+
time++;
74+
}
75+
System.out.println(-1);
76+
}
77+
78+
// κΈ°λ³Έ 정보 μž…λ ₯
79+
private static void init() throws Exception {
80+
int[] tempSharks = new int[m + 1];
81+
for (int i = 0; i < n * n; i++) {
82+
int num = read();
83+
if (num != 0) {
84+
tempSharks[num] = i; // μƒμ–΄μ˜ 인덱슀 μ €μž₯
85+
scentBoard[i] = num; // λƒ„μƒˆ μ‚½μž…
86+
remainBoard[i] = k; // 남은 μ‹œκ°„ 기둝
87+
}
88+
}
89+
int[] tempDir = new int[m + 1];
90+
for (int i = 1; i < m + 1; i++)
91+
tempDir[i] = read();
92+
93+
// 상어 객체 생성 및 큐에 μ‚½μž…
94+
for (int i = 1; i < m + 1; i++) {
95+
int idx = tempSharks[i], dir = tempDir[i];
96+
int[][] priority = new int[5][5]; // μš°μ„  μˆœμœ„ μž…λ ₯
97+
for (int j = 1; j < 5; j++)
98+
for (int k = 1; k < 5; k++)
99+
priority[j][k] = read();
100+
Shark shark = new Shark(i, idx, dir, priority);
101+
sharks.offer(shark);
102+
}
103+
}
104+
105+
// λ‹€μŒ λ°©ν–₯ κ²°μ •
106+
private static int getNextDir(Shark shark) {
107+
int[] priority = shark.getPriority(); // μš°μ„  μˆœμœ„μ— 따라 λ°©ν–₯ κ²°μ •
108+
int myScent = 0;
109+
for (int i = 1; i < 5; i++) {
110+
int dir = priority[i];
111+
int next = shark.idx + move[dir];
112+
if (isValid(shark.idx, next)) {
113+
if (scentBoard[next] == 0)
114+
return dir;
115+
else if (scentBoard[next] == shark.num && myScent == 0)
116+
myScent = dir;
117+
}
118+
}
119+
return myScent;
120+
}
121+
122+
// λƒ„μƒˆ 카운트 κ°μ†Œ
123+
private static void removeScent() {
124+
for (int i = 0; i < n * n; i++)
125+
if (scentBoard[i] != 0 && --remainBoard[i] == 0)
126+
scentBoard[i] = 0;
127+
}
128+
129+
// λƒ„μƒˆ 큐에 μžˆλŠ” μ’Œν‘œμ— λƒ„μƒˆ μ‚½μž…
130+
private static void putScent() {
131+
while (!scentDeque.isEmpty()) {
132+
int[] scent = scentDeque.poll();
133+
int idx = scent[0], num = scent[1];
134+
scentBoard[idx] = num;
135+
remainBoard[idx] = k;
136+
}
137+
}
138+
139+
// 인덱슀 μœ νš¨μ„± 검사
140+
private static boolean isValid(int cur, int next) {
141+
// 전체 인덱슀λ₯Ό λ²—μ–΄λ‚˜λŠ” 경우
142+
if (next < 0 || next >= n * n)
143+
return false;
144+
// 쒌, 우둜 움직일 λ•Œ, 같은 행에 μžˆλŠ”μ§€ 확인
145+
int curRow = cur / n;
146+
int nextRow = next / n;
147+
if (Math.abs(cur - next) == 1 && curRow != nextRow)
148+
return false;
149+
return true;
150+
}
151+
152+
private static int read() throws Exception {
153+
int c, n = System.in.read() & 15;
154+
while ((c = System.in.read()) >= 48)
155+
n = (n << 3) + (n << 1) + (c & 15);
156+
if (c == 13)
157+
System.in.read();
158+
return n;
159+
}
160+
}

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /