ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ Introduction
์คํฐ๋ ์ค ์ฝํ ์ ๊ฐ๋ ๋ฑ์ฅํ๋ ์ ํ์ธ ์น ๋ธ๋ผ์ฐ์ ํ์ ๋ฌธ์ ๋ฅผ ํ๊ฒ ๋์๋ค. ๊ตฌ๊ธ๋ง์ ํด๋ ๋ต์ด ๋ง์ง ์๊ณ , ํผ ์ฌ๋๋ ์ ์ ๋ฌธ์ ๋ผ ๋์์ด ๋ ๊น ์ถ์ด ์ฌ๋ฆฐ๋ค.
๐ ๋ฌธ์
https://www.acmicpc.net/problem/23294
๐ ์ค๋ช
๊ฐ๋จํ ๋ค๋ก ๊ฐ๊ธฐ์ ์์ผ๋ก ๊ฐ๊ธฐ์ ์๋ ํ์ด์ง ๋ฒํธ๋ค์ ๊ฐ๊ฐ Deque ๊ณผ Stack ์ ๋ฃ์ด์ ํ์๋ค. volume ์ด๋ผ๋ ๋ณ์๋ก ํ์ฌ ์ฌ์ฉ๋๋ ์บ์ ์ฉ๋์ ๊ณ์ ์ถ์ ํ๊ณ , ์ด ๊ฐ์ด max (๋ฌธ์ ์์๋ C) ๋ฅผ ๋์ด์๋ฉด Deque ์ removeFirst ๋ฉ์๋๋ฅผ ์ด์ฉํด ๊ฐ์ฅ ์์ ์ ๋ฐฉ๋ฌธํ ํ์ด์ง๋ฅผ ์ ๊ฑฐํด์ค๋ค. ํ์ด์ง๋ฅผ ๋ฐฉ๋ฌธํ ๋๋ง๋ค Stack ์ clear() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด ๋น์์ฃผ๊ณ , ๋น์์ง ๋งํผ ์ฉ๋์ ๊ณ์ฐํด volume ์์ ๋นผ ์ฃผ์๋ค.
1. ์ฒ์์ ์ ๋ ฅ์ ๋ฐ์ ๋ ๋ฌด์ฌ์ฝ toCharArray() ๋ฅผ ์ฌ์ฉํ๋๋ฐ, A i (ํ์ด์ง ์ ๊ทผ ๋ช ๋ น) ์ ์ ์ฅํ char ๋ฐฐ์ด์์ 2๋ฒ์งธ๋ฅผ ์ ๊ทผํ๋๊น ๊ณต๋ฐฑ ๋ฌธ์๊ฐ ๋์ ๋ฌธ์ ๊ฐ ํ๋ฆฌ์ง ์์๋ค. ์ดํ์ StringTokenizer ๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ๋ฐ๋ฅด๊ฒ ์ ๋ ฅ์ ๋ฐ๋๋ก ๋ณ๊ฒฝํด์ฃผ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์๋ค.
2. Stack ๊ณผ Deque ์๋ฃ๊ตฌ์กฐ๋ ์ธ๋ฑ์ค๋ฅผ ๊ฐ์ง๊ณ ์์์ ์ ๊ทผํ ์ ์๋ค. ๊ทธ๋์ ๋ชจ๋ ArrayList ํํ๋ก ๋ฐ๊ฟ์ค ๋ค์ ์ธ๋ฑ์ค๋ฅผ ์ฐธ์กฐํ์ฌ ์์๋ฅผ ๊ฐ์ ธ์ค๋๋ก ํด์ผ ํ๋ค. ์ถ๋ ฅ์ ํ ๋๋ ๋ง์ฐฌ๊ฐ์ง๋ก ArrayList๋ก ๋ณํํ ๋ค์ ๋งจ ๋ค์ ์์๋ถํฐ (๊ฐ์ฅ ์ต๊ทผ์ ๋ฐฉ๋ฌธํ ์) ์ถ๋ ฅํ๋๋ก ํ๋ค.
๐ก ํ์ด
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.util.StringTokenizer;
public class Main {
static Deque<Integer> past;
static Stack<Integer> rollback;
static int volume = 0;
static int current = -1;
static int[] sizes;
static int max;
public static void goBackward() {
if (past.isEmpty()) {
return;
}
rollback.push(current);
current = past.removeLast();
}
public static void goFrontward() {
if (rollback.isEmpty()) {
return;
}
past.addLast(current);
current = rollback.pop();
}
public static void access(int target) {
if (current != -1) {
past.addLast(current);
}
if (!rollback.isEmpty()) {
int total = 0;
for (int r : rollback) {
total += sizes[r];
}
volume -= total;
rollback.clear();
}
current = target;
volume += sizes[target];
if (volume > max) {
while (volume > max && !past.isEmpty()) {
volume -= sizes[past.getFirst()];
past.removeFirst();
}
}
}
public static void compress() {
int cur = -1;
List<Integer> p = new ArrayList<>(past);
for (int i = 0; i < p.size(); i++) {
if (cur != -1 && cur == p.get(i)) {
while (i < p.size() && p.get(i) >= 0 && cur == p.get(i)) {
volume -= sizes[p.get(i)];
p.set(i, -1);
i++;
}
// p.set(i-1, cur);
}
if (i < p.size()) {
cur = p.get(i);
}
}
int n = 0;
while (p.contains(-1)) {
if (p.get(n) == -1) {
p.remove(n);
continue;
}
n++;
}
past = new LinkedList<>(p);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int N = Integer.valueOf(st.nextToken());
int Q = Integer.valueOf(st.nextToken());
max = Integer.valueOf(st.nextToken());
sizes = new int[N + 1];
st = new StringTokenizer(br.readLine(), " ");
for (int i = 1; i <= N; i++) {
sizes[i] = Integer.valueOf(st.nextToken());
}
past = new LinkedList<>();
rollback = new Stack<>();
for (int i = 0; i < Q; i++) {
st = new StringTokenizer(br.readLine(), " ");
String command1 = st.nextToken();
String command2 = "";
if (st.hasMoreTokens()) {
command2 = st.nextToken();
}
if (command1.equals("B")) {
goBackward();
}
if (command1.equals("F")) {
goFrontward();
}
if (command1.equals("A")) {
access(Integer.valueOf(command2));
}
if (command1.equals("C")) {
compress();
}
}
System.out.println(current);
StringBuilder sb1 = new StringBuilder();
if (past.isEmpty()) {
System.out.println(-1);
} else {
List<Integer> p = new ArrayList<>(past);
for (int i = p.size() - 1; i >= 0; i--) {
if (i == 0) {
sb1.append(p.get(i));
} else {
sb1.append(p.get(i) + " ");
}
}
System.out.println(sb1);
}
StringBuilder sb2 = new StringBuilder();
if (rollback.isEmpty()) {
System.out.print(-1);
} else {
List<Integer> r = new ArrayList<>(rollback);
for (int i = r.size()-1; i >= 0; i--) {
if (i == 0) {
sb2.append(r.get(i));
} else {
sb2.append(r.get(i) + " ");
}
}
System.out.print(sb2);
}
}
}
'Algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Algorithms] ํ๋ก๊ทธ๋๋จธ์ค Level 2 ์์ ์ฐพ๊ธฐ (0) | 2023.06.26 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ธฐ์ง๊ตญ ์ค์น (1) | 2022.12.17 |
[์๊ณ ๋ฆฌ์ฆ] Longest Common Subsequence(LCS) - 1๋ถ (0) | 2022.12.09 |
[์๊ณ ๋ฆฌ์ฆ] ํ๋ก๊ทธ๋๋จธ์ค - ๋์คํฌ ์ปจํธ๋กค๋ฌ 1๋ถ (1) | 2022.12.08 |
[์๊ณ ๋ฆฌ์ฆ] Dijkstra's algorithm 2๋ถ - ๊ตฌํ ์ค๋น (0) | 2022.12.05 |
๊ณต์ง์ฌํญ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
- Total
- Today
- Yesterday
๋งํฌ
TAG
- DTO
- N+1
- json web token
- Jackson
- spring
- ๊ฐ์ ์๋ฒ
- ์ธ์ฆ/์ธ๊ฐ
- Spring Boot
- ci/cd
- Firebase
- JPA
- Java Data Types
- ๋์ปค
- ์ญ์ง๋ ฌํ
- FCM
- google cloud
- gitlab
- @RequestBody
- ์ฝํ
- ์ง์ฐ ๋ก๋ฉ
- JPQL
- docker
- ํ๋ก๊ทธ๋๋จธ์ค
- LazyInitializationException
- DeSerialization
- ์๊ณ ๋ฆฌ์ฆ
- JOIN FETCH
- ๊น๋ฉ
- ๊ธฐ์ง๊ตญ ์ค์น
- ์ค์๊ฐ๋ฐ์ดํฐ
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
๊ธ ๋ณด๊ดํจ