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 9e2cf6e

Browse files
authored
Merge pull request #325 from GreatAlgorithm-Study/dahye
[24주차] 고다혜
2 parents b51477b + 0e32c58 commit 9e2cf6e

File tree

6 files changed

+405
-0
lines changed

6 files changed

+405
-0
lines changed

‎BOJ/1000-5000번/DH_2629.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 양팔저울
6+
*/
7+
8+
public class DH_2629 {
9+
static final int LIMIT = 15_000; // 추를 통해 알 수 있는 최대 무게
10+
static int N, M;
11+
static int[] arr;
12+
static boolean[][] dp; // [idx][weight]: idx번째 추까지 고려했을 때, weight 무게를 만들 수 있는지
13+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
static StringTokenizer st;
15+
16+
public static void main(String[] args) throws Exception {
17+
initInput();
18+
solution();
19+
}
20+
21+
static void solution() throws Exception {
22+
StringBuilder sb = new StringBuilder();
23+
24+
dp = new boolean[N + 1][LIMIT + 1];
25+
26+
getPossibleWeight(0, 0);
27+
28+
st = new StringTokenizer(br.readLine());
29+
30+
for(int i = 0; i < M; i++) {
31+
32+
int num = Integer.parseInt(st.nextToken());
33+
34+
// 제한 무게를 넘거나, 모든 구슬을 확인했을 때 num무게를 만들 수 없다면 N출력
35+
if(num > LIMIT || !dp[N][num]) sb.append("N ");
36+
// 가능하다면 Y 출력
37+
else sb.append("Y ");
38+
}
39+
40+
System.out.println(sb);
41+
}
42+
43+
static void getPossibleWeight(int idx, int weight) {
44+
if(dp[idx][weight]) return;
45+
dp[idx][weight] = true;
46+
47+
if(idx == N) return;
48+
49+
getPossibleWeight(idx + 1, weight + arr[idx]); // 현재 기준이 되는 저울에 올리기
50+
getPossibleWeight(idx + 1, weight); // 넘어가기
51+
getPossibleWeight(idx + 1, Math.abs(weight - arr[idx])); // 반대 저울에 올리기
52+
}
53+
54+
static void initInput() throws Exception {
55+
56+
N = Integer.parseInt(br.readLine()); // 추의 개수
57+
arr = new int[N];
58+
59+
st = new StringTokenizer(br.readLine());
60+
for(int i = 0; i < arr.length; i++) arr[i] = Integer.parseInt(st.nextToken());
61+
62+
M = Integer.parseInt(br.readLine());
63+
}
64+
}

‎BOJ/20001-25000번/DH_21758.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 꿀 따기
6+
*/
7+
8+
public class DH_21758 {
9+
public static void main(String[] args) throws Exception {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
int N = Integer.parseInt(br.readLine());
12+
13+
StringTokenizer st = new StringTokenizer(br.readLine());
14+
15+
// arr, S1: 왼쪽에서 오른쪽으로 움직일 때 사용
16+
int[] arr = new int[N];
17+
for(int i = 0; i < arr.length; i++) arr[i] = Integer.parseInt(st.nextToken());
18+
19+
// tmp, S2: 오른쪽에서 왼쪽으로 움직일 때 사용 (arr를 뒤집어줌)
20+
int[] tmp = new int[N];
21+
for(int i = 0; i < N; i++) tmp[i] = arr[N - 1 - i];
22+
23+
// 누적합 구하기
24+
int[] S1 = new int[N + 1], S2 = new int[N + 1];
25+
for(int i = 1; i < N + 1; i++) {
26+
S1[i] = S1[i - 1] + arr[i - 1];
27+
S2[i] = S2[i - 1] + tmp[i - 1];
28+
}
29+
30+
int result = 0;
31+
result = Math.max(result, getHoney(S1, arr, N));
32+
result = Math.max(result, getHoney(S2, tmp, N));
33+
34+
System.out.println(result);
35+
}
36+
37+
// 꿀벌들이 얻을 수 있는 꿀의 최대값 구하기
38+
static int getHoney(int[] S, int[] arr, int N) {
39+
int bee1 = 0, result = 0;
40+
41+
// 첫 번째 꿀벌이 얻을 수 있는 꿀의 양
42+
int getBee1 = S[N] - S[bee1] - arr[bee1];
43+
44+
// '꿀벌 - 꿀벌 - 꿀' 일 때
45+
for(int bee2 = 1; bee2 < N - 1; bee2++) {
46+
int tmpGetBee1 = getBee1 - arr[bee2]; // 다른 꿀벌이 있는 위치의 꿀은 못얻음
47+
48+
// 두 번째 꿀벌이 얻ᄋ르 수 있는 꿀의 양
49+
int getBee2 = S[N] - S[bee2] - arr[bee2];
50+
result = Math.max(result, tmpGetBee1 + getBee2);
51+
}
52+
53+
// '꿀벌 - 꿀 - 꿀벌' 일 때
54+
for(int honey = 1; honey < N - 1; honey++) {
55+
int tmpGetBee1 = S[honey + 1] - S[bee1] - arr[bee1]; // 다른 꿀벌이 있는 위치의 꿀은 못얻음
56+
int getBee2 = S[N] - S[honey] - arr[N - 1];
57+
58+
result = Math.max(result, tmpGetBee1 + getBee2);
59+
}
60+
61+
return result;
62+
}
63+
}

‎BOJ/30000-35000번/DH_30407.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 나비의 간식을 훔쳐먹은 춘배
6+
*/
7+
8+
public class DH_30407 {
9+
static int N, H, D, K, result;
10+
static int[] R;
11+
12+
public static void main(String[] args) throws Exception {
13+
initInput();
14+
15+
// 깜짝 놀라게 하는 경우가 없을 때
16+
powerset(0, H, D, -2);
17+
18+
// i 번째에 깜짝 놀라게 하기 시전
19+
for(int i = 0; i < N; i++) powerset(0, H, D, i);
20+
21+
if(result <= 0) System.out.println(-1);
22+
else System.out.println(result);
23+
}
24+
25+
static void powerset(int depth, int h, int dis, int surprised) {
26+
if(depth == N) {
27+
result = Math.max(result, h);
28+
return;
29+
}
30+
31+
if(h <= 0 || h < result) return;
32+
33+
//깜짝 놀라게 하기
34+
if(surprised == depth) {
35+
int punchPower = Math.max(0, R[depth] - dis);
36+
powerset(depth + 1, h - punchPower, dis, surprised);
37+
} else {
38+
39+
// 웅크리기 ------------------------------------------
40+
int punchPower = 0; // 나비의 펀치 세기
41+
42+
if(surprised + 1 == depth) punchPower = 0; // 깜짝 놀라게 하기 다음 턴의 경우 나비의 행동 무시함
43+
else punchPower = Math.max(0, (R[depth] - dis) / 2);
44+
45+
powerset(depth + 1, h - punchPower, dis, surprised);
46+
47+
// 네발로 걷기 ------------------------------------------
48+
int nextDis = dis + K;
49+
50+
if(surprised + 1 == depth) punchPower = 0; // 깜짝 놀라게 하기 다음 턴의 경우 나비의 행동 무시함
51+
else punchPower = Math.max(0, R[depth] - nextDis);
52+
53+
powerset(depth + 1, h - punchPower, nextDis, surprised);
54+
}
55+
}
56+
57+
static void initInput() throws Exception {
58+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
59+
StringTokenizer st;
60+
61+
N = Integer.parseInt(br.readLine()); // 냥냥펀치의 수
62+
63+
st = new StringTokenizer(br.readLine());
64+
65+
H = Integer.parseInt(st.nextToken()); // 춘배의 체력
66+
D = Integer.parseInt(st.nextToken()); // 현재 나비 사이의 거리
67+
K = Integer.parseInt(st.nextToken()); // 네발로 걷기 시 이동하는 거리
68+
69+
R = new int[N];
70+
71+
for(int i = 0; i < N; i++) R[i] = Integer.parseInt(br.readLine());
72+
}
73+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
/*
5+
* 초밥 회전 시스템을 구현을 통해 관리하면 매우 복잡해짐
6+
*
7+
* 주방장이 만든 초밥이 손님에게 도달하는 시간을 계산한 뒤, 사라지는 시간
8+
* 손님들이 초밥을 다 먹고 떠나는 시간을 계산하여 쿼리에 추가
9+
*
10+
* 초밥과 손님의 입장과 퇴장을 쿼리로 표현하여 시간 순으로 처리...
11+
* 모든 행동들이 시간 순으로 일어나기 때문에, 이렇게 구현하면 된다고 한다..!
12+
*/
13+
14+
public class DH_코드트리_오마카세 {
15+
static class Query implements Comparable<Query> {
16+
int cmd, t, x, n;
17+
String name;
18+
19+
public Query(int cmd, int t, int x, int n, String name) {
20+
this.cmd = cmd;
21+
this.t = t;
22+
this.x = x;
23+
this.n = n;
24+
this.name = name;
25+
}
26+
27+
@Override
28+
public int compareTo(Query o) {
29+
if(this.t != o.t) return Integer.compare(this.t, o.t); // 시간 순
30+
return Integer.compare(this.cmd, o.cmd); // 명령순
31+
}
32+
}
33+
static class Time {
34+
int in, out;
35+
36+
public Time(int in) {
37+
this.in = in;
38+
}
39+
40+
public void setOutTime(int time) {
41+
this.out = Math.max(time, this.out);
42+
}
43+
44+
}
45+
static int L, Q;
46+
47+
// 쿼리가 시간 순으로 입력될 수 있도록 함
48+
static ArrayList<Query> query = new ArrayList<>();
49+
static HashSet<String> people = new HashSet<String>(); // 등장한 사람 목록 관리
50+
static HashMap<String, PriorityQueue<Query>> pQuery = new HashMap<>(); // 사람들마다 쿼리를 관리함
51+
static HashMap<String, Integer> pos = new HashMap<>(); // 사람들마다 위치 관리
52+
static HashMap<String, Time> time = new HashMap<>(); // 사람들의 출입 시간 관리
53+
static StringBuilder sb = new StringBuilder();
54+
55+
public static void main(String[] args) throws Exception {
56+
System.setIn(new FileInputStream("./input/코드트리오마카세.txt"));
57+
58+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
59+
StringTokenizer st = new StringTokenizer(br.readLine());
60+
61+
L = Integer.parseInt(st.nextToken());
62+
Q = Integer.parseInt(st.nextToken());
63+
64+
for(int q = 0; q < Q; q++) {
65+
66+
st = new StringTokenizer(br.readLine());
67+
68+
int cmd = Integer.parseInt(st.nextToken());
69+
int t = 0, x = 0, n = 0;
70+
String name = null;
71+
72+
if(cmd == 100) {
73+
t = Integer.parseInt(st.nextToken());
74+
x = Integer.parseInt(st.nextToken());
75+
name = st.nextToken();
76+
77+
if(pQuery.get(name) == null) pQuery.put(name, new PriorityQueue<>());
78+
pQuery.get(name).add(new Query(cmd, t, x, n, name));
79+
}
80+
81+
if(cmd == 200) {
82+
t = Integer.parseInt(st.nextToken());
83+
x = Integer.parseInt(st.nextToken());
84+
name = st.nextToken();
85+
n = Integer.parseInt(st.nextToken());
86+
87+
people.add(name);
88+
89+
time.put(name, new Time(t));
90+
pos.put(name, x);
91+
}
92+
93+
if(cmd == 300) {
94+
t = Integer.parseInt(st.nextToken());
95+
}
96+
97+
query.add(new Query(cmd, t, x, n, name));
98+
}
99+
100+
// 각 사람마다 자신의 이름이 적힌 초밥을 언제 먹게 되는지 계산하고, 기존 쿼리에 추가
101+
for(String p: people) {
102+
103+
// 마지막으로 먹는 초밥 중 가장 늦은 시간이 됨!
104+
while(!pQuery.get(p).isEmpty()) {
105+
Query current = pQuery.get(p).poll();
106+
107+
int removeTime = 0; // 사람이 나가게 되는 시간
108+
109+
// 초밥 - 사람
110+
// 초밥이 사람이 등장하기 전에 미리 주어진 상황
111+
if(current.t < time.get(p).in) {
112+
113+
// 사람이 들어왔을 때, 초밥의 위치
114+
int sushiPos = (current.x + (time.get(p).in - current.t)) % L;
115+
int addTime = (pos.get(p) - sushiPos + L) % L;
116+
117+
removeTime = time.get(p).in + addTime;
118+
}
119+
120+
// 사람 - 초밥
121+
else {
122+
int addTime = (pos.get(p) - current.x + L) % L;
123+
removeTime = current.t + addTime;
124+
}
125+
126+
time.get(p).setOutTime(removeTime); // 나가는 시간을 제일 마지막 초밥을 먹는 시간으로 설정해야 됨
127+
128+
// 초밥이 사라지는 쿼리 추가
129+
query.add(new Query(111, removeTime, 0, 0, p));
130+
}
131+
}
132+
133+
// 사람들이 나가는 쿼리 추가
134+
for(String p: people) query.add(new Query(222, time.get(p).out, 0, 0, p));
135+
136+
Collections.sort(query);
137+
138+
int sushiCnt = 0, peopleCnt = 0;
139+
140+
for(Query current: query) {
141+
142+
if(current.cmd == 100) sushiCnt++; // 초밥이 생겼다가
143+
else if(current.cmd == 111) sushiCnt--; // 초밥을 먹었다가
144+
else if(current.cmd == 200) peopleCnt++; // 사람이 들어왔다가
145+
else if(current.cmd == 222) peopleCnt--; // 사람이 나갔다가
146+
else sb.append(peopleCnt).append(" ").append(sushiCnt).append("\n");
147+
}
148+
149+
System.out.println(sb);
150+
}
151+
152+
}

‎Programmers/Level2/DH_148652.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* 유사 칸토어 비트열
3+
*/
4+
5+
public class DH_148652 {
6+
static long solution(int n, long l, long r) {
7+
return getOneCnt(r, n) - getOneCnt(l - 1, n);
8+
}
9+
10+
static long getOneCnt(long e, int depth) {
11+
if(depth == 0) return 1; // 0번째 비트열: 1
12+
13+
long digit = (long) Math.pow(5, depth - 1); // depth번째에서 한 그룹당 몇 개의 자릿수를 가지고 있는지
14+
15+
// e가 속하는 구간이 어디인지 구하기 (0, 1, 2, 3, 4)
16+
int area = (int) (e / digit);
17+
if(e % digit == 0) area -= 1; // 구간 경계에 있는거 올바른 구간에 포함될 수 있도록 -1 해주기
18+
19+
int prevOneCnt = (int) Math.pow(4, depth - 1);
20+
21+
// 0기준 왼쪽 (1인 구역의 개수가 전체 자리수)개
22+
if(area < 2) return prevOneCnt * area + getOneCnt(e - (digit * area), depth - 1);
23+
// 0있는 부분
24+
else if(area == 2) return prevOneCnt * area;
25+
// 0기준 오른쪽 (1인 구역의 개수가 전체 자리수 - 1)개
26+
return prevOneCnt * (area - 1) + getOneCnt(e - (digit * area), depth - 1);
27+
}
28+
public static void main(String[] args) {
29+
int n = 2, l = 4, r = 17;
30+
System.out.println(solution(n, l, r));
31+
}
32+
}

0 commit comments

Comments
(0)

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