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 830036a

Browse files
authored
Merge pull request #291 from GreatAlgorithm-Study/dahye
[21์ฃผ์ฐจ] ๊ณ ๋‹คํ˜œ
2 parents b899edc + 99a1e05 commit 830036a

6 files changed

+441
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* ์ค‘๋Ÿ‰์ œํ•œ
6+
*/
7+
8+
public class DH_1939 {
9+
static class Node {
10+
int e, w;
11+
12+
public Node(int e, int w) {
13+
this.e = e;
14+
this.w = w;
15+
}
16+
}
17+
public static void main(String[] args) throws Exception {
18+
System.setIn(new FileInputStream("./input/BOJ1939.txt"));
19+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
20+
StringTokenizer st = new StringTokenizer(br.readLine());
21+
22+
int N = Integer.parseInt(st.nextToken()); // ์„ฌ์˜ ๊ฐœ์ˆ˜
23+
int M = Integer.parseInt(st.nextToken()); // ๋‹ค๋ฆฌ์— ๋Œ€ํ•œ ์ •๋ณด
24+
25+
ArrayList<ArrayList<Node>> adj = new ArrayList<>();
26+
for(int i = 0; i < N + 1; i++) adj.add(new ArrayList<>());
27+
28+
// ์ธ์ ‘๋ฆฌ์ŠคํŠธ ๋งŒ๋“ค๊ธฐ
29+
for(int i = 0; i < M; i++) {
30+
st = new StringTokenizer(br.readLine());
31+
32+
int a = Integer.parseInt(st.nextToken());
33+
int b = Integer.parseInt(st.nextToken());
34+
int w = Integer.parseInt(st.nextToken());
35+
36+
adj.get(a).add(new Node(b, w));
37+
adj.get(b).add(new Node(a, w));
38+
}
39+
40+
st = new StringTokenizer(br.readLine());
41+
int a = Integer.parseInt(st.nextToken());
42+
int b = Integer.parseInt(st.nextToken());
43+
44+
int s = 0, e = 1_000_000_000;
45+
46+
// ์ตœ๋Œ“๊ฐ’ ๊ตฌํ•˜๊ธฐ โ†’ upper bound ๊ตฌํ•˜๊ธฐ
47+
while(s <= e) {
48+
int m = (s + e) / 2;
49+
50+
// ์ตœ๋Œ€ ํ•˜์ค‘์ด m ์ผ ๋•Œ, s์—์„œ e๊นŒ์ง€ ๊ฐˆ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธ
51+
if(isValid(a, b, m, adj)) s = m + 1;
52+
// ๊ฐˆ ์ˆ˜ ์—†๋‹ค๋ฉด ์ตœ๋Œ€ํ•˜์ค‘ ๋Š˜๋ฆฌ๊ธฐ
53+
else e = m - 1;
54+
}
55+
56+
System.out.println(e);
57+
}
58+
59+
// ์‹œ์ž‘ ์ง€์ ์—์„œ ๋ ์ง€์ ๊นŒ์ง€ ๊ฐˆ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธ
60+
static boolean isValid(int s, int e, int m, ArrayList<ArrayList<Node>> adj) {
61+
boolean flag = false;
62+
63+
ArrayDeque<Integer> q = new ArrayDeque<Integer>();
64+
q.add(s);
65+
66+
boolean[] v = new boolean[adj.size()];
67+
68+
// ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฐ„์„ ์€ ๋ชจ๋‘ ๊ฐ€ ๋ด์•ผ ๋˜๊ธฐ ๋•Œ๋ฌธ์—, pollํ•  ๋•Œ ๋ฐฉ๋ฌธ์ฒดํฌํ•ด์•ผ๋จ!
69+
while(!q.isEmpty()) {
70+
int current = q.poll();
71+
72+
if(v[current]) continue;
73+
v[current] = true;
74+
75+
if(current == e) {
76+
flag = true;
77+
break;
78+
}
79+
80+
for(Node next: adj.get(current)) {
81+
if(next.w >= m) q.add(next.e);
82+
}
83+
}
84+
return flag;
85+
}
86+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* ๋กœ๋ด‡ ์กฐ์ข…ํ•˜๊ธฐ
6+
*/
7+
8+
public class DH_2169 {
9+
static int INF = - 1_000 * 1_000 * 10;
10+
11+
public static void main(String[] args) throws Exception {
12+
System.setIn(new FileInputStream("./input/BOJ2169.txt"));
13+
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
StringTokenizer st = new StringTokenizer(br.readLine());
16+
17+
int N = Integer.parseInt(st.nextToken());
18+
int M = Integer.parseInt(st.nextToken());
19+
20+
int[][] arr = new int[N + 1][M + 1];
21+
int[][][] dp = new int[3][N + 1][M + 1];
22+
23+
for(int k = 0; k < 3; k++) {
24+
for(int r = 0; r < dp[0].length; r++) Arrays.fill(dp[k][r], INF);
25+
}
26+
for(int r = 1; r < arr.length; r++) {
27+
st = new StringTokenizer(br.readLine());
28+
for(int c = 1; c < arr[0].length; c++) {
29+
arr[r][c] = Integer.parseInt(st.nextToken());
30+
}
31+
}
32+
33+
// dp ํ…Œ์ด๋ธ” ์ฑ„์šฐ๊ธฐ
34+
for(int r = 1; r < dp[0].length; r++) {
35+
if(r == 1) {
36+
dp[0][r][0] = 0;
37+
for(int c = 1; c < dp[0][0].length; c++) {
38+
dp[0][r][c] = arr[r][c] + dp[0][r][c - 1];
39+
}
40+
continue;
41+
}
42+
43+
// ์œ„์—์„œ ์˜ค๋Š” ๊ฒฝ์šฐ
44+
for(int c = 1; c < dp[0][0].length; c++) {
45+
dp[1][r][c] = Math.max(dp[0][r - 1][c], Math.max(dp[1][r - 1][c], dp[2][r - 1][c])) + arr[r][c];
46+
}
47+
48+
// ์™ผ์ชฝ์—์„œ ์˜ค๋Š” ๊ฒฝ์šฐ
49+
for(int c = 1; c < dp[0][0].length; c++) {
50+
dp[0][r][c] = Math.max(dp[0][r][c - 1], dp[1][r][c - 1]) + arr[r][c];
51+
}
52+
53+
// ์˜ค๋ฅธ์ชฝ์—์„œ ์˜ค๋Š” ๊ฒฝ์šฐ
54+
for(int c = dp[0][0].length - 2; c > 0; c--) {
55+
dp[2][r][c] = Math.max(dp[1][r][c + 1], dp[2][r][c + 1]) + arr[r][c];
56+
}
57+
58+
59+
}
60+
System.out.println(Math.max(dp[0][N][M], Math.max(dp[1][N][M], dp[2][N][M])));
61+
}
62+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* ์„  ๊ธ‹๊ธฐ
6+
*/
7+
8+
public class DH_2170 {
9+
static class Point implements Comparable<Point> {
10+
int s, e;
11+
public Point(int s, int e) {
12+
this.s = s;
13+
this.e = e;
14+
}
15+
16+
@Override
17+
public int compareTo(Point o) {
18+
if(this.s != o.s) return Integer.compare(this.s, o.s);
19+
return Integer.compare(this.e, o.e);
20+
}
21+
}
22+
23+
static final int MIN = -1_000_000_000, MAX = 1_000_000_000;
24+
public static void main(String[] args) throws Exception {
25+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
26+
StringTokenizer st;
27+
28+
int N = Integer.parseInt(br.readLine());
29+
30+
PriorityQueue<Point> pq = new PriorityQueue<Point>();
31+
32+
// ์šฐ์„ ์ˆœ์œ„ํ์— ์ ๋“ค ๋„ฃ์–ด์ฃผ๊ธฐ
33+
for(int i = 0; i < N; i++) {
34+
st = new StringTokenizer(br.readLine());
35+
36+
int s = Integer.parseInt(st.nextToken());
37+
int e = Integer.parseInt(st.nextToken());
38+
39+
pq.add(new Point(s, e));
40+
}
41+
42+
// (MAX, MAX) ๋กœ ๋„ฃ์œผ๋ฉด ๋งˆ์ง€๋ง‰์— ๋„ฃ์€ ์ ์ด MAX์ผ ๋•Œ, ์˜ฌ๋ฐ”๋ฅธ ์ •๋‹ต์ด ์•ˆ๋‚˜์˜ด
43+
pq.add(new Point(MAX + 1, MAX + 1));
44+
45+
int start = MIN, end = MIN;
46+
int result = 0;
47+
48+
while(!pq.isEmpty()) {
49+
Point current = pq.poll();
50+
51+
// ํ˜„์žฌ ์‹œ์ž‘์ ์ด ์ด์ „๊นŒ์ง€ ํ™•์ธํ•œ ์ง€์ ๋“ค์˜ ๋์ ๋ณด๋‹ค ํฌ๋ฉด
52+
// ์ด์ „ ์ ๋“ค์„ ์ด์–ด์„œ ๋งŒ๋“ค์–ด์ง„ ์„ ๋ถ„์˜ ๊ธธ์ด ๋”ํ•ด์ฃผ๊ธฐ
53+
if(end < current.s) {
54+
result += end - start;
55+
56+
start = current.s;
57+
end = current.e;
58+
59+
} else {
60+
end = Math.max(end, current.e);
61+
}
62+
}
63+
64+
System.out.println(result);
65+
}
66+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* ๊ด„ํ˜ธ์˜ ๊ฐ’
6+
*/
7+
8+
public class DH_2504 {
9+
public static void main(String[] args) throws Exception {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
String s = br.readLine();
12+
13+
long result = 0;
14+
15+
Stack<Character> stack = new Stack<Character>(); // ๊ด„ํ˜ธ, ์ˆซ์ž ์กด์žฌ์— ๋Œ€ํ•œ ์ •๋ณด ์ €์žฅ
16+
Stack<Long> numStack = new Stack<Long>(); // ์ˆซ์ž์— ๋Œ€ํ•œ ์ •๋ณด ์ €์žฅ
17+
18+
for(int i = 0; i < s.length(); i++) {
19+
char ch = s.charAt(i);
20+
21+
if(ch == '(' || ch == '[') stack.push(ch); // ์—ฌ๋Š” ๊ด„ํ˜ธ๋ผ๋ฉด ์Šคํƒ์— ๋„ฃ์–ด์ฃผ๊ธฐ
22+
else {
23+
24+
boolean flag = false; // ๊ด„ํ˜ธ๊ฐ€ ์ž˜ ๋‹ซํ˜”์„ ๋•Œ, flag๋ฅผ true๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ
25+
26+
ch = ch == ')' ? '(' : '['; // ๊ด„ํ˜ธ ๋’ค์ง‘์–ด์ฃผ๊ธฐ
27+
28+
if(!stack.isEmpty()) {
29+
30+
long tmp = 0; // ์ˆซ์ž ๊ณ„์‚ฐ์„ ์œ„ํ•œ ๋ณ€์ˆ˜
31+
32+
while(!stack.isEmpty()) {
33+
// ๊ด„ํ˜ธ๊ฐ€ ๊ฐ™์„ ๋•Œ
34+
if(ch == stack.peek()) {
35+
stack.pop();
36+
stack.push('.'); // ์ˆซ์ž๊ฐ€ ์žˆ์Œ์„ ํ‘œ์‹œ
37+
38+
int mul = ch == '(' ? 2 : 3;
39+
numStack.push(tmp == 0 ? mul : mul * tmp);
40+
41+
// ๊ด„ํ˜ธ๊ฐ€ ๋‹ซํ˜€์•ผ flag true๋กœ ํ•ด์คŒ
42+
flag = true;
43+
break;
44+
}
45+
46+
// ์ˆซ์ž๊ฐ€ ์กด์žฌํ•  ๋•Œ
47+
else if(stack.peek() == '.') {
48+
stack.pop();
49+
tmp += numStack.pop();
50+
}
51+
// ์œ ํšจํ•˜์ง€ ์•Š์€ ์ž…๋ ฅ์ผ ๋•Œ
52+
else break;
53+
}
54+
}
55+
56+
if(!flag) break;
57+
}
58+
59+
}
60+
61+
boolean flag = true;
62+
63+
// stack์— ๊ด„ํ˜ธ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ฉด์„œ ์ตœ์ข…๊ฐ’ ๊ณ„์‚ฐํ•ด์ฃผ๊ธฐ
64+
while(!stack.isEmpty()) {
65+
if(stack.pop() == '.') result += numStack.pop();
66+
else {
67+
flag = false;
68+
break;
69+
}
70+
}
71+
72+
if(!flag) System.out.println(0);
73+
else System.out.println(result);
74+
}
75+
}

0 commit comments

Comments
(0)

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