From 5c3f5444d17e3fc137162d2b3759b262f2278f95 Mon Sep 17 00:00:00 2001 From: Jonghwan Lee <123362165+0224LJH@users.noreply.github.com> Date: Thu, 9 Oct 2025 18:23:34 +0900 Subject: [PATCH] =?UTF-8?q?[20251009]=20PGM=20/=20Lv2=20/=20=ED=83=9D?= =?UTF-8?q?=EB=B0=B0=20=EB=B0=B0=EB=8B=AC=EA=B3=BC=20=EC=88=98=EA=B1=B0?= =?UTF-8?q?=ED=95=98=EA=B8=B0=20/=20=EC=9D=B4=EC=A2=85=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...30\352\261\260\355\225\230\352\270\260.md" | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 "0224LJH/202510/09 PGM \355\203\235\353\260\260 \353\260\260\353\213\254\352\263\274 \354\210\230\352\261\260\355\225\230\352\270\260.md" diff --git "a/0224LJH/202510/09 PGM \355\203\235\353\260\260 \353\260\260\353\213\254\352\263\274 \354\210\230\352\261\260\355\225\230\352\270\260.md" "b/0224LJH/202510/09 PGM \355\203\235\353\260\260 \353\260\260\353\213\254\352\263\274 \354\210\230\352\261\260\355\225\230\352\270\260.md" new file mode 100644 index 00000000..2cd40fa1 --- /dev/null +++ "b/0224LJH/202510/09 PGM \355\203\235\353\260\260 \353\260\260\353\213\254\352\263\274 \354\210\230\352\261\260\355\225\230\352\270\260.md" @@ -0,0 +1,100 @@ +```java +import java.io.*; +import java.util.*; + + +class Solution { + + static int limit,houseCnt,pIdx, dIdx; + static long totalCost; + static House[] houses; + + static class House implements Comparable{ + int idx; + int delivery; + int pickup; + + public House(int idx, int delivery, int pickup ){ + this.idx = idx; + this.delivery = delivery; + this.pickup = pickup; + } + + @Override + public int compareTo(House h){ + return Integer.compare(h.idx, this.idx); // 거리 높은순으로 나옴 + } + } + + public long solution(int cap, int n, int[] deliveries, int[] pickups) { + limit = cap; + houseCnt = n; + totalCost = 0; + houses = new House[houseCnt+1]; + for (int i = 1; i<= houseCnt; i++){ + houses[i] = new House(i, deliveries[i-1], pickups[i-1]); + } + + process(); + + + long answer = totalCost; + return answer; + } + + private void process(){ + dIdx = houseCnt; + pIdx = houseCnt; + + while (dIdx != 0 || pIdx != 0){ + processEach(); + } + } + + + + private void processEach(){ + int dLeft = limit; + int pLeft = limit; + + while(dIdx != 0 && houses[dIdx].delivery == 0){ + dIdx--; + } + while(pIdx != 0 && houses[pIdx].pickup == 0){ + pIdx--; + } + + int curCost = Math.max(dIdx, pIdx) * 2; + // System.out.println(curCost + " ->" + dIdx + " "+ pIdx); + totalCost += curCost; + + while(dIdx != 0 && dLeft != 0){ + House h = houses[dIdx]; + if (h.delivery > dLeft){ + h.delivery -= dLeft; + dLeft = 0; + break; + } + + dLeft -= h.delivery; + h.delivery = 0; + dIdx--; + } + + while(pIdx != 0 && pLeft != 0){ + House h = houses[pIdx]; + if (h.pickup > pLeft){ + h.pickup -= pLeft; + pLeft = 0; + break; + } + + pLeft -= h.pickup; + h.pickup = 0; + pIdx--; + } + // System.out.println(curCost + " ->" + dIdx + " "+ pIdx); + + } +} +```