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 e38687b

Browse files
authored
Merge pull request #347 from GreatAlgorithm-Study/jimin
[26주차] 손지민
2 parents c583b92 + 5e718e2 commit e38687b

File tree

3 files changed

+325
-0
lines changed

3 files changed

+325
-0
lines changed

‎BOJ/1000-5000번/JM_2151.java‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
public class JM_2151 {
8+
static class Point implements Comparable<Point> {
9+
int y, x;
10+
int dir;
11+
int dist;
12+
13+
public Point(int y, int x, int dir, int dist) {
14+
this.y = y;
15+
this.x = x;
16+
this.dir = dir;
17+
this.dist = dist;
18+
}
19+
20+
@Override
21+
public int compareTo(Point o) {
22+
return this.dist - o.dist;
23+
}
24+
}
25+
static int N;
26+
static char[][] map;
27+
static Point start;
28+
static Point end;
29+
static final int[][] DIR = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
30+
31+
private static boolean inRange(int y, int x) {
32+
return 0 <= y && y < N && 0 <= x && x < N;
33+
}
34+
35+
private static int solve() {
36+
PriorityQueue<Point> pq = new PriorityQueue<>();
37+
boolean[][][] visited = new boolean[N][N][4];
38+
39+
for (int i = 0; i < 4; i++) {
40+
pq.add(new Point(start.y, start.x, i, 0));
41+
}
42+
43+
while (!pq.isEmpty()) {
44+
Point curr = pq.poll();
45+
46+
visited[curr.y][curr.x][curr.dir] = true;
47+
48+
if(curr.y == end.y && curr.x == end.x) return curr.dist;
49+
50+
int ny = curr.y + DIR[curr.dir][0];
51+
int nx = curr.x + DIR[curr.dir][1];
52+
53+
if(!inRange(ny, nx) || map[ny][nx] == '*' || visited[ny][nx][curr.dir]) continue;
54+
55+
if(map[ny][nx] == '!') {
56+
pq.add(new Point(ny, nx, (curr.dir + 1) % 4, curr.dist + 1));
57+
pq.add(new Point(ny, nx, (curr.dir + 3) % 4, curr.dist + 1));
58+
}
59+
pq.add(new Point(ny, nx, curr.dir, curr.dist));
60+
61+
}
62+
63+
return -1;
64+
}
65+
66+
public static void main(String[] args) throws IOException {
67+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
68+
StringTokenizer st = new StringTokenizer(br.readLine());
69+
N = Integer.parseInt(st.nextToken());
70+
71+
map = new char[N][N];
72+
for (int i = 0; i < N; i++) {
73+
map[i] = br.readLine().toCharArray();
74+
for (int j = 0; j < N; j++) {
75+
if(map[i][j] == '#') {
76+
if(start == null) start = new Point(i, j, -1, 0);
77+
else end = new Point(i, j, -1, 0);
78+
}
79+
}
80+
}
81+
82+
System.out.println(solve());
83+
}
84+
}

‎BOJ/1000-5000번/JM_3151.java‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.Arrays;
5+
import java.util.StringTokenizer;
6+
7+
public class JM_3151 {
8+
static int N;
9+
static int[] A;
10+
11+
private static int lowerBound(int startIndex, int target) {
12+
int lo = startIndex, hi = N - 1;
13+
int ans = N;
14+
while (lo <= hi){
15+
int mid = (lo + hi) / 2;
16+
if(target <= A[mid]) {
17+
ans = mid;
18+
hi = mid - 1;
19+
}
20+
else lo = mid + 1;
21+
}
22+
return ans;
23+
}
24+
25+
private static int upperBound(int startIndex, int target) {
26+
int lo = startIndex, hi = N - 1;
27+
int ans = N;
28+
while (lo <= hi){
29+
int mid = (lo + hi) / 2;
30+
if(target < A[mid]) {
31+
ans = mid;
32+
hi = mid - 1;
33+
}
34+
else lo = mid + 1;
35+
}
36+
return ans;
37+
}
38+
39+
private static long solve() {
40+
Arrays.sort(A);
41+
long count = 0;
42+
for (int i = 0; i < N; i++) {
43+
for (int j = i + 1; j < N; j++) {
44+
int target = 0 - (A[i] + A[j]);
45+
int lowerIndex = lowerBound(j + 1, target);
46+
int upperIndex = upperBound(j + 1, target);
47+
count += upperIndex - lowerIndex;
48+
}
49+
}
50+
return count;
51+
}
52+
53+
public static void main(String[] args) throws IOException {
54+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
55+
StringTokenizer st = new StringTokenizer(br.readLine());
56+
N = Integer.parseInt(st.nextToken());
57+
58+
A = new int[N];
59+
st = new StringTokenizer(br.readLine());
60+
for(int i = 0; i < N; i++) {
61+
A[i] = Integer.parseInt(st.nextToken());
62+
}
63+
System.out.println(solve());
64+
}
65+
}
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.Arrays;
5+
import java.util.StringTokenizer;
6+
7+
public class JM_코드트리_메신저 {
8+
static class Room {
9+
int c;
10+
boolean alert;
11+
int authority;
12+
13+
int parent;
14+
int leftChild;
15+
int rightChild;
16+
17+
int[] effect;
18+
19+
public Room(int c) {
20+
this.c = c;
21+
this.alert = true;
22+
this.parent = -1;
23+
this.leftChild = -1;
24+
this.rightChild = -1;
25+
this.effect = new int[DEPTH + 1];
26+
}
27+
28+
public void toggleAlert() {
29+
this.alert = !this.alert;
30+
}
31+
}
32+
static int N;
33+
static int Q;
34+
static Room[] rooms;
35+
static final int DEPTH = 20;
36+
37+
private static void update(int c) {
38+
while (c != 0) {
39+
Room curr = rooms[c];
40+
Arrays.fill(curr.effect, 0);
41+
for (int i = 0; i <= curr.authority; i++) {
42+
rooms[c].effect[i]++;
43+
}
44+
45+
if(curr.leftChild != -1 && rooms[curr.leftChild].alert) {
46+
for (int i = 1; i <= DEPTH; i++) {
47+
curr.effect[i - 1] += rooms[curr.leftChild].effect[i];
48+
}
49+
}
50+
51+
if(curr.rightChild != -1 && rooms[curr.rightChild].alert) {
52+
for (int i = 1; i <= DEPTH; i++) {
53+
curr.effect[i - 1] += rooms[curr.rightChild].effect[i];
54+
}
55+
}
56+
57+
c = rooms[c].parent;
58+
}
59+
}
60+
61+
private static void swapParent(int c1, int c2) {
62+
int p1 = rooms[c1].parent;
63+
int p2 = rooms[c2].parent;
64+
65+
if(rooms[p1].leftChild == c1) {
66+
rooms[p1].leftChild = c2;
67+
} else if(rooms[p1].rightChild == c1) {
68+
rooms[p1].rightChild = c2;
69+
}
70+
71+
if(rooms[p2].leftChild == c2) {
72+
rooms[p2].leftChild = c1;
73+
} else if(rooms[p2].rightChild == c2) {
74+
rooms[p2].rightChild = c1;
75+
}
76+
77+
rooms[c1].parent = p2;
78+
rooms[c2].parent = p1;
79+
80+
update(c1);
81+
update(c2);
82+
}
83+
84+
private static void changeAuthority(int c, int power) {
85+
rooms[c].authority = power > DEPTH ? DEPTH : power;
86+
update(c);
87+
}
88+
89+
private static void toggleNotification(int c) {
90+
rooms[c].toggleAlert();
91+
update(c);
92+
}
93+
94+
private static void initEffect(int c) {
95+
if(c == -1) return;
96+
97+
Room curr = rooms[c];
98+
for (int i = 0; i <= curr.authority; i++) {
99+
rooms[c].effect[i]++;
100+
}
101+
102+
initEffect(curr.leftChild);
103+
initEffect(curr.rightChild);
104+
105+
if(curr.leftChild != -1) {
106+
for (int i = 1; i <= DEPTH; i++) {
107+
curr.effect[i - 1] += rooms[curr.leftChild].effect[i];
108+
}
109+
}
110+
111+
if(curr.rightChild != -1) {
112+
for (int i = 1; i <= DEPTH; i++) {
113+
curr.effect[i - 1] += rooms[curr.rightChild].effect[i];
114+
}
115+
}
116+
}
117+
118+
private static void init(StringTokenizer st) {
119+
rooms = new Room[N + 1];
120+
for (int i = 0; i <= N; i++) {
121+
rooms[i] = new Room(i);
122+
}
123+
124+
for (int i = 1; i <= N; i++) {
125+
int pIdx = Integer.parseInt(st.nextToken());
126+
rooms[i].parent = pIdx;
127+
if(rooms[pIdx].leftChild == -1) rooms[pIdx].leftChild = i;
128+
else rooms[pIdx].rightChild = i;
129+
}
130+
131+
for (int i = 1; i <= N; i++) {
132+
rooms[i].authority = Integer.parseInt(st.nextToken());
133+
if(rooms[i].authority > DEPTH) rooms[i].authority = DEPTH;
134+
}
135+
136+
initEffect(0);
137+
}
138+
139+
public static void main(String[] args) throws IOException {
140+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
141+
StringTokenizer st = new StringTokenizer(br.readLine());
142+
N = Integer.parseInt(st.nextToken());
143+
Q = Integer.parseInt(st.nextToken());
144+
145+
StringBuilder sb = new StringBuilder();
146+
int c;
147+
while (Q-- > 0) {
148+
st = new StringTokenizer(br.readLine());
149+
int command = Integer.parseInt(st.nextToken());
150+
switch (command) {
151+
case 100: // 사내 메신저 준비
152+
init(st);
153+
break;
154+
case 200: // 알림망 설정 (ON/OFF)
155+
c = Integer.parseInt(st.nextToken());
156+
toggleNotification(c);
157+
break;
158+
case 300: // 권한 세기 변경
159+
c = Integer.parseInt(st.nextToken());
160+
int power = Integer.parseInt(st.nextToken());
161+
changeAuthority(c, power);
162+
break;
163+
case 400: // 부모 채팅방 교환
164+
int c1 = Integer.parseInt(st.nextToken());
165+
int c2 = Integer.parseInt(st.nextToken());
166+
swapParent(c1, c2);
167+
break;
168+
case 500: // 알림을 받을 수 있는 채팅방 수 조회
169+
c = Integer.parseInt(st.nextToken());
170+
sb.append(rooms[c].effect[0] - 1).append("\n");
171+
break;
172+
}
173+
}
174+
System.out.println(sb);
175+
}
176+
}

0 commit comments

Comments
(0)

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