- 풀이
Queue를 이용해서 다리의 무게를 더해가면서 time을 늘려주었다.
- 코드
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
static int N,W,L,answer;
static Queue<Integer> truck;
public static void main(String[] args) throws Exception {
SetData();
System.out.println(answer);
}
// 데이터
private static void SetData() throws Exception {
InputReader in = new InputReader(System.in);
N = in.nextInt();
W = in.nextInt();
L = in.nextInt();
answer = 0;
truck = new LinkedList<Integer>();
for(int i = 0; i < N; i++) {
truck.add(in.nextInt());
}
answer = SaveAnswer();
}
private static int SaveAnswer() {
int count = 0;
int bridgeWeight=0;
Queue<Integer> bridge = new LinkedList<Integer>();
for(int i =0; i<W ; i++) {
bridge.add(0);
}
while(!bridge.isEmpty()) {
count++;
bridgeWeight-=bridge.poll();
if(!truck.isEmpty()) {
if(truck.peek()+bridgeWeight<=L) {
bridgeWeight+=truck.peek();
bridge.offer(truck.poll());
}else {
bridge.offer(0);
}
}
}
return count;
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
'algorithm' 카테고리의 다른 글
[BOJ/JAVA] 백준 11062번 : 카드 게임 (0) | 2021.08.04 |
---|---|
[BOJ/JAVA] 백준 11723번 : 집합 (0) | 2021.08.04 |
[BOJ/JAVA] 백준 18428번 : 감시 피하기 (0) | 2021.08.04 |
[BOJ/JAVA] 백준 14569번 : 시간표 짜기 (0) | 2021.08.02 |
[BOJ/JAVA] 백준 1303번 : 전쟁 - 전투 (0) | 2021.08.02 |