2024-TEAM-05 / algorithm-for-kakao

카카였 기좜 문제 κ°€μ¦ˆμ•„πŸ£
0 stars 0 forks source link

[λ°±μ€€] 택배 #48

Open hye-on opened 5 days ago

hye-on commented 5 days ago

πŸ”— 택배

uijin-j commented 1 day ago

πŸ“‘ λŒ“κΈ€ ν…œν”Œλ¦Ώ

μ½”λ“œ 풀이

```java import java.io.*; import java.util.*; /** * 14:32 μ‹œμž‘! */ public class Main { /* * 그리디 * 1 2 10 : 1λ²ˆμ—μ„œ +10 * 1 3 20 : 1-2λ²ˆμ—μ„œ +20 * 2 3 10 : 2λ²ˆμ—μ„œ +10 * 1 4 30 : 1-3λ²ˆμ—μ„œ +10 * 2 4 20 * 3 4 20 : 3λ²ˆμ—μ„œ +20 */ public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(bf.readLine()); int n = Integer.parseInt(st.nextToken()); int c = Integer.parseInt(st.nextToken()); int m = Integer.parseInt(bf.readLine()); int[][] info = new int[m][3]; for(int i = 0; i < m; ++i) { st = new StringTokenizer(bf.readLine()); info[i][0] = Integer.parseInt(st.nextToken()); info[i][1] = Integer.parseInt(st.nextToken()); info[i][2] = Integer.parseInt(st.nextToken()); } Arrays.sort(info, (a, b) -> a[1] - b[1] == 0 ? a[0] - b[0] : a[1] - b[1]); int[] remain = new int[n+1]; Arrays.fill(remain, c); int count = 0; for(int i = 0; i < m; ++i) { int from = info[i][0]; int to = info[i][1]; int cnt = info[i][2]; for(int j = from; j < to; ++j) { cnt = Math.min(cnt, remain[j]); } if(cnt == 0) continue; for(int j = from; j < to; ++j) { remain[j] -= cnt; } count += cnt; } System.out.println(count); } } ```

μ½”λ©˜νŠΈ

- μ²˜μŒμ—λŠ” λ³΄λ‚΄λŠ” λ§ˆμ„ κΈ°μ€€μœΌλ‘œ 정렬을 μƒκ°ν•΄μ„œ 그리디 λ¬Έμ œκ°€ μ•„λ‹Œ 쀄 μ•Œμ•˜λ„€μš”πŸ˜’ μ•Œκ³ λ³΄λ‹ˆ λ„μ°©ν•˜λŠ” λ§ˆμ„μ„ κΈ°μ€€μœΌλ‘œ μ •λ ¬ν•˜κ³ (μ΅œλŒ€ν•œ 빨리 λ‚΄λ €μ€˜μ•Ό 뒀에 더 내렀쀄 수 있기 λ•Œλ¬Έ) κ·Έλ¦¬λ””λ‘œ ν’€λ©΄ λ˜λŠ” λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€!
hye-on commented 1 day ago

πŸ“‘ λŒ“κΈ€ ν…œν”Œλ¦Ώ

μ½”λ“œ 풀이

```cpp #include #include #include #include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, c, m; cin >> n >> c >> m; vector> v(m); // {도착, μ‹œμž‘, λ°•μŠ€} for (int i = 0; i < m; i++) { int start, end, box; cin >> start >> end >> box; v[i] = { end, start, box }; // 도착지 κΈ°μ€€ 정렬을 μœ„ν•΄ endλ₯Ό 첫번째둜 } sort(v.begin(), v.end()); // κΈ°λ³Έ μ •λ ¬λ§ŒμœΌλ‘œ 도착지, μ‹œμž‘μ§€ 순 정렬됨 vector capacity(n + 1, c); int ans = 0; for (int i = 0; i < m; i++) { int end = get<0>(v[i]); int start = get<1>(v[i]); int box = get<2>(v[i]); int possible = c; for (int j = start; j < end; j++) { possible = min(possible, capacity[j]); } int mBox = min(possible, box); for (int j = start; j < end; j++) { capacity[j] -= mBox; } ans += mBox; } cout << ans << '\n'; return 0; } ```

μ½”λ©˜νŠΈ

- μΆœλ°œν•˜λŠ” λ„μ‹œλ₯Ό κΈ°μ€€μœΌλ‘œ μ •λ ¬ν•˜κ³  λ„μ°©ν•˜λŠ” μ• κ°€ 더 λΉ λ₯Έ 것이 λ“±μž₯ν•  경우 κ΅μ²΄ν•˜λŠ” κ²ƒμœΌλ‘œ ν’€μ—ˆλŠ”λ° ν‹€λ¦° 엣지 μΌ€μ΄μŠ€κ°€ 계속 μžˆμ—ˆλ˜ 것 κ°™μŠ΅λ‹ˆλ‹€ γ…  λ„μ°©ν•˜λŠ” λ„μ‹œ κΈ°μ€€μœΌλ‘œ μ •λ Ών•΄μ„œ ν’€μ–΄μ•Όν–ˆλ„€μš”