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 ac31a56

Browse files
Merge pull request #154 from GreatAlgorithm-Study/xubin
[11์ฃผ์ฐจ] ๋ฐฐ์ˆ˜๋นˆ
2 parents 9aac077 + 7543a9b commit ac31a56

File tree

9 files changed

+589
-0
lines changed

9 files changed

+589
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.StringTokenizer;
5+
6+
public class SB_1052 {
7+
static int N, K;
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
StringTokenizer st = new StringTokenizer(br.readLine());
11+
12+
N = Integer.parseInt(st.nextToken());
13+
K = Integer.parseInt(st.nextToken());
14+
15+
int bottle = 0;
16+
while (Integer.bitCount(N) > K) { // ๋ฌผ๋ณ‘์˜ 2์ œ๊ณฑ๊ทผ ๋ฌถ์Œ(๋น„ํŠธ๋ณ€ํ™˜์‹œ 1์˜ ๊ฐœ์ˆ˜)์ด K๊ฐ€ ๋ ๋•Œ๊นŒ์ง€ ๋ฌผ๋ณ‘ ์ถ”๊ฐ€
17+
int lowPos = N & -N; // ๊ฐ€์žฅ 1์˜ ์œ„์น˜์— ๋ฌผ๋ณ‘ ์ถ”๊ฐ€
18+
N+=lowPos; // ๋ฌผ๋ณ‘ ๊ฐฏ์ˆ˜ ์—…๋ฐ์ดํŠธ
19+
bottle += lowPos; // ์ถ”๊ฐ€ํ•œ ๋ฌผ๋ณ‘๋งŒํผ ๋”ํ•ด์ฃผ๊ธฐ
20+
}
21+
System.out.println(bottle);
22+
}
23+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.StringTokenizer;
7+
8+
public class SB_1716 {
9+
static int N, M;
10+
static List<List<Node>> adj = new ArrayList<>();
11+
static int[][] parents;
12+
static int mxDepth;
13+
static int[] depths;
14+
static int[] dist; // ๊ฐ ์ •์ ์—์„œ ๋ฃจํŠธ๋กœ ๋ถ€ํ„ฐ ๋–จ์–ด์ง„ ๊ฑฐ๋ฆฌ
15+
16+
private static int LCA(int a, int b) { // ๊ณตํ†ต ์กฐ์ƒ ๊ตฌํ•˜๊ธฐ
17+
if (depths[a] < depths[b]) { // a์˜ ๊นŠ์ด๊ฐ€ ๊นŠ๋„๋ก ์„ค์ •
18+
int tmp = b;
19+
b = a;
20+
a = tmp;
21+
}
22+
23+
for (int i = mxDepth - 1; i >= 0; i--) { // ๊นŠ์ด ๋™์ผํ•˜๊ธฐ ๋งž์ถ”๊ธฐ
24+
if (depths[a] - depths[b] >= (1 << i)) { // ๊นŠ์ด ์ฐจ๊ฐ€ 2์ œ๊ณฑ๊ผด๋งŒํผ ํฌ๋‹ค๋ฉด
25+
a = parents[a][i]; // ๋” ๊นŠ์€ ๋…ธ๋“œ๋ฅผ i๋งŒํผ ์ ํ”„
26+
}
27+
}
28+
29+
if (a==b) return a; // ๊นŠ์ด ์กฐ์ •ํ–ˆ์„๋•Œ ๋ฐ”๋กœ ๋ถ€๋ชจ๊ฐ€ ๊ฐ™์œผ๋ฉด ๋
30+
31+
for (int i = mxDepth - 1; i >= 0; i--) { // ๋ถ€๋ชจ๊ฐ€ ๋‹ค๋ฅด๋ฉด ๊ฐ™์ด ๋ถ€๋ชจ์ฐพ์œผ๋Ÿฌ ๋– ๋‚˜๊ธฐ
32+
if (parents[a][i] != parents[b][i]) {
33+
a = parents[a][i];
34+
b = parents[b][i];
35+
}
36+
}
37+
return parents[a][0]; // ์ตœ์ข…์ ์œผ๋กœ ์ฐพ์€ ๋ถ€๋ชจ ๊ฐ’ ๋ฐ˜ํ™˜
38+
}
39+
private static void setTree(int depth, int x, int p, int cost) {
40+
depths[x] = depth;
41+
dist[x] = cost; // ๋ฃจํŠธ์—์„œ ํ˜„์žฌ ๋…ธ๋“œx๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ
42+
for (Node nxt : adj.get(x)) {
43+
if (nxt.u==p) continue; // ๋‹ค์Œ ๋…ธ๋“œ๊ฐ€ ๋ถ€๋ชจ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฅดํ‚ค๋ฉด ํŒจ์“ฐ
44+
parents[nxt.u][0] = x; // ๋‹ค์Œ ๋…ธ๋“œ์˜ ๋ฐ”๋กœ ์œ„ ๋ถ€๋ชจ๋ฅผ ์ž์‹ ์œผ๋กœ ์„ค์ •
45+
setTree(depth + 1, nxt.u, x, cost + nxt.cost);
46+
}
47+
}
48+
49+
private static void setParents() { // 2์ œ๊ณฑ๋ณ„ ๋ถ€๋ชจ๊ฐ’ ๊ตฌํ•˜๊ธฐ, setTree์—์„œ ๊ตฌํ•œ ๋ฐ”๋กœ ์ง์ „ ๋ถ€๋ชจ๊ฐ’์„ ๋ฐ”ํƒ•์œผ๋กœ 2^i๋ฒˆ์งธ ๋ถ€๋ชจ ๊ฐ’ ๊ตฌํ•˜๊ธฐ
50+
for (int i = 1; i <= mxDepth; i++) {
51+
for (int j = 1; j < N + 1; j++) {
52+
parents[j][i] = parents[parents[j][i - 1]][i - 1]; // j๋…ธ๋“œ์˜ 2^i๋ฒˆ์งธ ๊ฐ’์€, 2^i์˜ ์กฐ์ƒ์˜ ์กฐ์ƒ ๊ฐ’
53+
}
54+
}
55+
}
56+
public static void main(String[] args) throws IOException {
57+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
58+
StringTokenizer st;
59+
StringBuilder sb = new StringBuilder();
60+
N = Integer.parseInt(br.readLine());
61+
62+
depths = new int[N + 1];
63+
dist = new int[N + 1];
64+
mxDepth = (int)Math.ceil(Math.log(N) / Math.log(2)) + 1; // ์ตœ๋Œ€๋†’์ด ๊ตฌํ•˜๊ธฐ
65+
parents = new int[N + 1][mxDepth+1];
66+
for (int i = 0; i < N + 1; i++) {
67+
adj.add(new ArrayList<>());
68+
}
69+
for (int i = 0; i < N - 1; i++) {
70+
st = new StringTokenizer(br.readLine());
71+
int u = Integer.parseInt(st.nextToken());
72+
int v = Integer.parseInt(st.nextToken());
73+
int c = Integer.parseInt(st.nextToken());
74+
adj.get(u).add(new Node(v, c));
75+
adj.get(v).add(new Node(u, c));
76+
}
77+
78+
// ํŠธ๋ฆฌ ๊ตฌ์„ฑํ•˜๊ธฐ
79+
setTree(1, 1, 0, 0); // 1๋ฒˆ๋…ธ๋“œ๋ฅผ ๋ฃจํŠธ๋กœ ๊นŠ์ด1๋ถ€ํ„ฐ ์‹œ์ž‘
80+
setParents();
81+
82+
M = Integer.parseInt(br.readLine());
83+
while (M-- > 0) {
84+
st = new StringTokenizer(br.readLine());
85+
int a = Integer.parseInt(st.nextToken());
86+
int b = Integer.parseInt(st.nextToken());
87+
sb.append(dist[a] + dist[b] - (dist[LCA(a, b)] * 2)).append('\n');
88+
}
89+
System.out.println(sb);
90+
}
91+
static class Node{
92+
int u, cost;
93+
public Node(int u, int cost) {
94+
this.u = u;
95+
this.cost = cost;
96+
}
97+
}
98+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.StringTokenizer;
5+
6+
public class SB_15831 {
7+
static int N, B, W;
8+
static int[] walks;
9+
static int[] pebb = new int[2]; // 0:ํฐ๋Œ, 1:๊ฒ€์ •
10+
static int mx = 0;
11+
public static void main(String[] args) throws IOException {
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
StringTokenizer st = new StringTokenizer(br.readLine());
14+
15+
N = Integer.parseInt(st.nextToken());
16+
B = Integer.parseInt(st.nextToken()); // ๊ฒ€์ • ์ตœ๋Œ€ ๊ฐœ์ˆ˜
17+
W = Integer.parseInt(st.nextToken()); // ํฐ์ƒ‰ ์ตœ์†Œ ๊ฐœ์ˆ˜
18+
19+
walks = new int[N];
20+
String line = br.readLine();
21+
for (int i = 0; i < N; i++) {
22+
char c = line.charAt(i);
23+
walks[i] = (c == 'W') ? 0 : 1;
24+
}
25+
26+
int left = 0;
27+
int right = 0;
28+
while (right < N) {
29+
pebb[walks[right]]++; // ์˜ค๋ฅธ์ชฝ์— ํ•ด๋‹นํ•˜๋Š” ์ƒ‰์˜ ๋Œ++
30+
31+
while (pebb[1]>B){ // ๋”์ด์ƒ ๋ชป๊ฐˆ๊ฒฝ์šฐ ์กฐ๊ฑด๋งŒ์กฑํ• ๋•Œ๊นŒ์ง€ left๋นผ๊ธฐ
32+
pebb[walks[left]]--;
33+
left++;
34+
}
35+
36+
if (pebb[0] >= W){ // ํฐ๋Œ ๋งŒ์กฑํ•˜๋ฉด ๊ฑฐ๋ฆฌ ๊ฐฑ์‹ 
37+
mx = Math.max(mx, right - left + 1);
38+
}
39+
right++;
40+
}
41+
System.out.println(mx);
42+
}
43+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.StringTokenizer;
5+
6+
public class SB_19939 {
7+
static int N, K;
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
StringTokenizer st = new StringTokenizer(br.readLine());
11+
12+
N = Integer.parseInt(st.nextToken());
13+
K = Integer.parseInt(st.nextToken());
14+
15+
// ์šฐ์„ , ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์œผ๋ ค๋ฉด (0~K-1)๊นŒ์ง€ ๋”ํ•œ๊ฐ’์ด N-K๊ฐ’๋ณด๋‹ค ์ปค์•ผํ•จ
16+
// 0๋ถ€ํ„ฐ K-1๊นŒ์ง€ ํ•ฉ (K๊ฐœ์˜ ๊ทธ๋ฃน์— ์ตœ์†Œ ํ•œ ๊ฐœ์”ฉ ๊ณต์„ ๋ฐฐ๋ถ„ํ•˜๋Š” ๋ฐ ํ•„์š”ํ•œ ์ตœ์†Œ ๊ณต ๊ฐœ์ˆ˜) : ๋“ฑ์ฐจ์ˆ˜์—ด์˜ ํ•ฉ
17+
int need = K * (K - 1) / 2;
18+
19+
if ((N-K) < need) {
20+
System.out.println(-1);
21+
return;
22+
}
23+
24+
// ์ตœ์†Œ 1๊ฐœ์”ฉ ๋ฐฐ๋ถ„ ํ›„ ๋‚จ์€ ๊ณต T
25+
int T = N - need;
26+
27+
if (T%K==0) System.out.println(K - 1); // ๋‚˜๋ˆ ๋–จ์–ด์ง€๋ฉด 0~K-1๋กœ ๋ถ„๋ฐฐํ•  ์ˆ˜ ์žˆ๊ณ , ์ด๋•Œ ์ตœ์†Œ ์ฐจ๋Š” (K-1)-0์œผ๋กœ K-1
28+
else System.out.println(K); // ๋‚˜๋ˆ ๋–จ์–ด์ง€์ง€ ์•Š์œผ๋ฉด, ๋ถ„๋ฐฐ์— 1์”ฉ ์ถ”๊ฐ€ํ•˜๊ฒŒ๋˜๊ณ , ์ตœ์†Œ์™€ ์ตœ๋Œ€์ฐจ๊ฐ€ 1๋Š˜์–ด๋‚จ
29+
}
30+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.StringTokenizer;
5+
6+
public class SB_9466 {
7+
static int[] choose;
8+
static boolean[] isFinish;
9+
static int[] visited;
10+
static int order = 0;
11+
static int cnt = 0;
12+
13+
private static void dfs(int x) {
14+
visited[x] = ++order; // ํ˜„์žฌ ์ง€์  ๋ช‡๋ฒˆ์งธ๋กœ ๋ฐฉ๋ฌธํ–ˆ๋Š”์ง€ ์ฒดํฌ
15+
16+
int nxt = choose[x]; // ํ˜„์žฌ ํ•™์ƒ์ด ์„ ํƒํ•œ ํ•™์ƒ
17+
18+
if (visited[nxt]==0) { // ์„ ํƒ๋œ ํ•™์ƒ์„ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜์œผ๋ฉด dfs์ง„ํ–‰
19+
dfs(nxt);
20+
} else if (!isFinish[nxt]) { // ๋ฐฉ๋ฌธ์€ ํ–ˆ๋Š”๋ฐ dfs๊ฐ€ ๋๋‚˜์ง€ ์•Š์•˜์œผ๋ฉด ์‚ฌ์ดํด
21+
cnt += visited[x] - visited[nxt] + 1;
22+
}
23+
24+
isFinish[x] = true;
25+
}
26+
private static void init(int n) {
27+
choose = new int[n + 1];
28+
isFinish = new boolean[n+1];
29+
visited = new int[n+1];
30+
order = 0;
31+
cnt = 0;
32+
}
33+
public static void main(String[] args) throws IOException {
34+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
35+
StringTokenizer st;
36+
StringBuilder sb = new StringBuilder();
37+
38+
int T = Integer.parseInt(br.readLine());
39+
while (T-- > 0) {
40+
int N = Integer.parseInt(br.readLine());
41+
init(N);
42+
st = new StringTokenizer(br.readLine());
43+
for (int v = 1; v < N+1; v++) {
44+
int u = Integer.parseInt(st.nextToken());
45+
choose[v] = u;
46+
}
47+
48+
for (int i = 1; i < N + 1; i++) {
49+
if (!isFinish[i]) {
50+
dfs(i);
51+
}
52+
}
53+
sb.append(N - cnt).append('\n');
54+
}
55+
System.out.println(sb);
56+
}
57+
}

0 commit comments

Comments
(0)

AltStyle ใซใ‚ˆใฃใฆๅค‰ๆ›ใ•ใ‚ŒใŸใƒšใƒผใ‚ธ (->ใ‚ชใƒชใ‚ธใƒŠใƒซ) /