|
| 1 | +```java |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.*; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | + |
| 7 | +public class Main { |
| 8 | + static int villageCnt, totalSpace, curSpace,ans, deliveryCnt; |
| 9 | + static PriorityQueue<Delivery> pq = new PriorityQueue<>(); |
| 10 | + static int[] spaces; |
| 11 | + static Delivery[] deliveries; |
| 12 | + |
| 13 | + |
| 14 | + static class Delivery implements Comparable<Delivery>{ |
| 15 | + int from; |
| 16 | + int to; |
| 17 | + int boxCnt; |
| 18 | + int overlap = 0; |
| 19 | + |
| 20 | + public Delivery(int from, int to, int boxCnt) { |
| 21 | + this.from = from; |
| 22 | + this.to = to; |
| 23 | + this.boxCnt = boxCnt; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public int compareTo(Delivery d) { |
| 28 | + |
| 29 | + return Integer.compare(this.to,d.to); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public static void main(String[] args) throws IOException { |
| 34 | + init(); |
| 35 | + process(); |
| 36 | + print(); |
| 37 | + } |
| 38 | + |
| 39 | + public static void init() throws IOException { |
| 40 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 41 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 42 | + villageCnt = Integer.parseInt(st.nextToken()); |
| 43 | + totalSpace =Integer.parseInt(st.nextToken()); |
| 44 | + curSpace = 0; |
| 45 | + ans = 0; |
| 46 | + |
| 47 | + spaces = new int[villageCnt+1]; |
| 48 | + deliveryCnt = Integer.parseInt(br.readLine()); |
| 49 | + deliveries = new Delivery[deliveryCnt]; |
| 50 | + |
| 51 | + for (int i = 0; i < deliveryCnt; i++) { |
| 52 | + st = new StringTokenizer(br.readLine()); |
| 53 | + int from = Integer.parseInt(st.nextToken()); |
| 54 | + int to = Integer.parseInt(st.nextToken()); |
| 55 | + int boxCnt = Integer.parseInt(st.nextToken()); |
| 56 | + |
| 57 | + Delivery d = new Delivery(from,to,boxCnt); |
| 58 | + deliveries[i] = d; |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + public static void process() throws IOException { |
| 67 | + getOverlap(); |
| 68 | + for (int i = 0; i < deliveryCnt; i++) pq.add(deliveries[i]); |
| 69 | + |
| 70 | + while(!pq.isEmpty()) { |
| 71 | + Delivery d = pq.poll(); |
| 72 | + |
| 73 | + put(d); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + public static void put(Delivery d) { |
| 80 | + |
| 81 | + int limit = d.boxCnt; |
| 82 | + |
| 83 | + for (int i = d.from; i < d.to; i++) { |
| 84 | + limit = Math.min(limit, totalSpace - spaces[i]); |
| 85 | + } |
| 86 | + |
| 87 | + for (int i = d.from; i <d.to; i++) { |
| 88 | + spaces[i] += limit; |
| 89 | + } |
| 90 | + ans += limit; |
| 91 | + } |
| 92 | + |
| 93 | + public static void getOverlap() { |
| 94 | + int[] cumul = new int[villageCnt+1]; |
| 95 | + |
| 96 | + for (int i = 0; i < deliveryCnt; i++) { |
| 97 | + Delivery d = deliveries[i]; |
| 98 | + for (int j = d.from; j < d.to; j++) { |
| 99 | + cumul[j]++; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + for (int i = 0; i < deliveryCnt; i++) { |
| 104 | + Delivery d = deliveries[i]; |
| 105 | + int maxCnt = 0; |
| 106 | + for (int j = d.from; j < d.to; j++) { |
| 107 | + maxCnt = Math.max(maxCnt, cumul[j]); |
| 108 | + } |
| 109 | + d.overlap = maxCnt; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + public static void print() { |
| 119 | + for (int i =1 ; i <= villageCnt; i++) System.out.print(spaces[i] + " "); |
| 120 | + System.out.println(); |
| 121 | + System.out.println(ans); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +``` |
0 commit comments