8980번: 택배

입력의 첫 줄은 마을 수 N과 트럭의 용량 C가 빈칸을 사이에 두고 주어진다. N은 2이상 2,000이하 정수이고, C는 1이상 10,000이하 정수이다. 다음 줄에, 보내는 박스 정보의 개수 M이 주어진다. M은 1이

www.acmicpc.net

 

 

 


 

 

 

  • 풀이

 

1. 우선순위 큐에 넣으면서 정렬을 시켜줍니다.

2. weight이란 int 배열을 활용해서 모든 마을에 최대로 보낼 수 있는 만큼 택배를 싫어보냅니다.

3. 보낼 때 최대 박스 개수는 answer이란 변수에 추가시켜줍니다.

 

참고) 시간을 좀 더 빨리하기 위해 우선순위 큐를 사용했습니다.

 

 

 

 

  • 반례

 

5 4
5
2 4 1
4 5 3
1 5 1
3 4 2
1 2 2
correct = 9

6 5
6
5 6 2
4 5 3
1 2 2
3 6 2
3 4 3
2 6 1
correct = 12

4 5
4
3 4 1
2 3 4
1 2 1
1 4 4
correct = 7

5 2
6
3 5 1
1 4 1
4 5 1
2 3 1
1 2 1
1 5 2
correct = 5

5 5
4
2 3 2
2 5 3
3 4 1
2 4 1
correct = 6

5 4
7
2 5 4
3 4 1
1 4 4
1 5 3
3 5 1
2 3 3
1 2 3
correct = 9

6 3
8
2 6 3
5 6 1
4 5 3
4 6 2
1 2 1
1 4 1
2 3 2
3 5 2
correct = 8

6 4
12
3 6 4
3 4 3
5 6 4
3 5 4
4 5 4
2 5 4
4 6 3
1 4 4
1 5 1
2 6 3
2 3 3
1 2 3
correct = 18

5 3
3
2 3 2
3 4 1
2 5 2
correct = 4

 

 

 

  • 코드

 

import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;
import java.util.PriorityQueue;

public class Main {
	static int C, answer;
	static int[] weight;
	static PriorityQueue<Delivery> queue;

	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);

		int N = in.nextInt();
		C = in.nextInt();
		weight = new int[N + 1];
		int countOfDelivery = in.nextInt();
		answer = 0;
		queue = new PriorityQueue<>();

		for (int i = 0; i < countOfDelivery; i++) {
			queue.add(new Delivery(in.nextInt(), in.nextInt(), in.nextInt()));
		}

		move();
	}

	private static void move() {
		while (!queue.isEmpty()) {
			Delivery delivery = queue.poll();
			
			int temp, max = 0;
			for (int i = delivery.from; i < delivery.to; i++) {
				max = Math.max(max, weight[i]);
			}

			answer += (temp = Math.min(delivery.weight, C - max));

			for (int i = delivery.from; i < delivery.to; i++) {
				weight[i] += temp;
			}
		}
	}
}

class Delivery implements Comparable<Delivery> {
	int from;
	int to;
	int weight;

	public Delivery(int from, int to, int weight) {
		this.from = from;
		this.to = to;
		this.weight = weight;
	}

	@Override
	public int compareTo(Delivery o) {
		if (this.to < o.to) {
			return -1;
		} else if (this.to == o.to) {
			if (this.from < o.from) {
				return -1;
			}
		}
		return 1;
	}
}

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;
	}
}

+ Recent posts