ํ”Œ๋ ˆํ‹ฐ๋„˜1 : ๋™์  ๊ณ„ํš๋ฒ•, Convex Hull ๋ฌธ์ œ์ด๋‹ค.

์ƒ๊ฐ

์ผ๋‹จ ์™„์ „ ํƒ์ƒ‰์„ ์ƒ๊ฐํ•ด ๋ณธ๋‹ค. ์Œ.. ๋ง๋„ ์•ˆ๋œ๋‹ค. ๋ช‡๊ฐœ๋ฅผ ๊ฒน์ณ์„œ ๋งŒ๋“œ๋Š”์ง€ ์™„์ „ํƒ์ƒ‰์„ ํ•œ๋‹ค๋ฉด ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ์ด๋‹ค. ์ด ๋ฌธ์ œ๋Š” ๊ทธ๋ƒฅ ์ง๊ด€์ ์œผ๋กœ ๋™์  ๊ณ„ํš๋ฒ• ๋ƒ„์ƒˆ๊ฐ€ ๋‚œ๋‹ค. ์–ด๋– ํ•œ ์กฐ๊ฑด์—์„œ ํŠน์ • ์œ„์น˜๊นŒ์ง€์˜ ๋•…์˜ ์ตœ์†Œ๊ฐ’์ด ๋‹ค์Œ์„ ๊ตฌํ•˜๋Š”๋ฐ ์˜ํ–ฅ์„ ์ค„ ๊ฒƒ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์ •์˜

dp[n] = n๋ฒˆ์งธ ๋•…๊นŒ์ง€ ๊ตฌ๋งคํ–ˆ์„ ๋•Œ ๋น„์šฉ์˜ ์ตœ์†Ÿ๊ฐ’

์ด๋ ‡๊ฒŒ ์ •์˜ํ–ˆ๋‹ค๋ฉด, ์ด์ œ๋Š” ์ ํ™”์‹์„ ์–ด๋–ป๊ฒŒ ์„ธ์šธ์ง€ ๊ณ ๋ฏผํ•ด์•ผ ํ•œ๋‹ค. n๋ฒˆ์งธ ๋•…๊นŒ์ง€ ๊ตฌ๋งคํ–ˆ์„ ๋•Œ ๋น„๊ตํ•ด๋ด์•ผ ํ•˜๋Š” ๊ฐ’๋“ค์„ ๋‚˜์—ดํ•ด ๋ณด์ž.

  1. n๋ฒˆ์งธ ๋•…์„ ๊ทธ๋ƒฅ ์‚ฐ๋‹ค. ๊ทธ๋ฆฌ๊ณ  n-1๊นŒ์ง€์˜ ์ตœ์†Œ๊ฐ’๊ณผ ๋”ํ•œ๋‹ค.
  2. n-1๋ฒˆ์งธ ๋•…๊ณผ ๋ฌถ์–ด์„œ ๊ณ„์‚ฐํ•ด๋ณธ๋‹ค. ๊ทธ๋ฆฌ๊ณ  n-2๊นŒ์ง€์˜ ์ตœ์†Ÿ๊ฐ’๊ณผ ๋”ํ•œ๋‹ค.
  3. n-2๋ฒˆ์งธ ๋•…๊ณผ ๋ฌถ์–ด์„œ ๊ณ„์‚ฐํ•ด๋ณธ๋‹ค. ๊ทธ๋ฆฌ๊ณ  n-3๊นŒ์ง€์˜ ์ตœ์†Ÿ๊ฐ’๊ณผ ๋”ํ•œ๋‹ค.

โ€ฆ

ํ˜ธ์˜ค. ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™์€ ๋ƒ„์ƒˆ๊ฐ€ ๋‚œ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•  ๊ฒฝ์šฐ ๊นŒ์ง€ ์ค„์ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

dp\[i\] = min\_{j=i}^{j=1}(dp\[j-1\] + \sum\_{k=j}^{k=i}a_k)
dp[i] = min(dp[j-1] + j๋ถ€ํ„ฐ i๊นŒ์ง€์˜ ๋•…์„ ํ•ฉ์ณ ์‚ฐ ๊ธˆ์•ก)

์ด ๊ฒƒ์„ ์ˆ˜์‹์œผ๋กœ ์ •๋ฆฌํ•ด๋ณด๋ฉด, ์œ„์™€ ๊ฐ™๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด j๋ถ€ํ„ฐ i๊ฐ€์ง€์˜ ๋•…์„ ํ•ฉ์ณ ์‚ฐ ๊ธˆ์•ก์€ ์–ด๋–ป๊ฒŒ ๊ตฌํ• ๊นŒ?

j๋ถ€ํ„ฐ i๊ฐ€์ง€์˜ ๋•…์„ ํ•ฉ์ณ ์‚ฐ ๊ธˆ์•ก

์‹ค์ œ๋กœ ๋•…์„ ์‚ฌ๋ณธ๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ๊ฐ€์žฅ ๋นจ๋ฆฌ ์ง๊ด€์„ ์–ป๋Š” ๊ฒƒ์€ ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ณด๋Š” ๊ฒƒ์ด๋‹ค. ๊ฒฐ๊ตญ ์ด ๋ฌธ์ œ๋Š”, ๋•…์„ ๊ตฌ์ž…ํ•˜๋Š”๋ฐ ์žˆ์–ด์„œ ์ด๊ฑธ ํ†ต์งœ๋กœ ํ•œ๋ฒˆ์— ์‚ฌ๋ฒ„๋ฆฐ๋‹ค๋Š” ์–˜๊ธฐ์™€ ๊ฐ™๋‹ค.

์ž…๋ ฅ์ด 100 1, 20 20์ด ๋“ค์–ด์™”๋‹ค๊ณ  ์ƒ๊ฐํ•˜์ž. ์ด ๋ถ€๋ถ„์— ํ•ด๋‹นํ•˜๋Š” ๋•…์„ ๊ทธ๋ฆฌ๋ฉด ์œ„์™€ ๊ฐ™๋‹ค. ๊ทธ๋Ÿฐ๋ฐ, ๋‚ด๊ฐ€ ์ด ๋‘ ๋•…์„ ํ•ฉ์นœ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๊ฒฐ๊ตญ ์ค‘์š”ํ•œ ๊ฒƒ์„ ์ตœ๋Œ€ ๋„ˆ๋น„์™€ ์ตœ๋Œ€ ๋†’์ด์ด๋‹ค. ์•„๊นŒ ์œ„์—์„œ ๋ณธ ๊ฒƒ์ฒ˜๋Ÿผ ๊ฒฐ๊ตญ dp[i]์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋งจ ์•„๋ž˜ ๋•…๋ถ€ํ„ฐ ํฌํ•จํ•ด์„œ ์œ„์ชฝ ๋•…์„ 1๊ฐœ, 2๊ฐœ 3๊ฐœ โ€ฆ ๋ฅผ ์„ ํƒํ•˜์—ฌ ํ•ฉ์ณ์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด, ๋•…์„ ํ•ฉ์น˜๋Š” ๋ฐ ์žˆ์–ด์„œ 3๊ฐœ๋ฅผ ํ•ฉ์นœ๋‹ค๋ฉด, 3๊ฐœ์˜ ๋•…์˜ ๋„ˆ๋น„์™€ ๋†’์ด๋ฅผ ๋‹ค ๋น„๊ตํ•ด์„œ ์–ป์–ด๋‚ด์•ผ ํ•˜๋Š”๋ฐ.. ๊ตณ์ด ๊ทธ๋ž˜์•ผ ํ• ๊นŒ?

์ •๋ ฌํ•˜๊ธฐ

๊ตณ์ด ๊ทธ๋Ÿด ํ•„์š” ์—†๋‹ค. dp[i]์„ ๊ตฌํ•˜๋Š”๋ฐ ์žˆ์–ด์„œ ๋ฌด์กฐ๊ฑด ํฌํ•จํ•ด์•ผ ํ•˜๋Š” ๋•…์€ i๋ฒˆ์งธ ๋•…์ด๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋‚ด๊ฐ€ ํ•ฉ์น˜๊ณ  ์‹ถ์€ ๋ฒ”์œ„์˜ ๋•…์€ j๋ฒˆ์งธ ๋•…์ด๋‹ค. ์ด ๋‘๋•…์„ ๊ฐ๊ฐ ground[i], ground[j] ๋ผ ํ•˜๊ฒ ๋‹ค.

๋•…์„ ๊ตฌํ•˜๋Š”๋ฐ ์žˆ์–ด์„œ ์œ„์˜ ๋ฌธ์ œ์ฒ˜๋Ÿผ ์ผ์ผํžˆ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜์ง€๋ง๊ณ , ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ข‹๊ฒ ๋‹ค.

ground[i] = ํ•ฉ์น  ๋•…์˜ ์ตœ๋Œ€ height

ground[j] = ํ•ฉ์น  ๋•…์˜ ์ตœ๋Œ€ width

์ด๋Ÿฌ๋ฉด, ์‹œ๊ฐ„ ๋ณต์žก๋„ ์— ๊ฐ€๋Šฅํ•˜๋‹ค..!

ํ•„์š”์—†๋Š” ๋•… ์ œ๊ฑฐ

๊ทธ๋Ÿฐ๋ฐ ์ •๋ ฌ์„ ํ•˜๋‹ค๋ณด๋‹ˆ ์ด๋Ÿฐ ๋•…๋„ ์žˆ๋‹ค.

์Œ.. ์ด๋Ÿฐ ๋•…์€ ์–ด์ฉŒ์ง€? ์ž˜ ์ƒ๊ฐํ•ด๋ณด๋ฉด, ์ด ๋•…์€ ํ•„์š”์—†๋‹ค. ์ด๋ฏธ ๋•…์„ ์‚ด ๋•Œ, ํฌํ•จ๋˜๋Š” ๋•…์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 20 20์„ ๊ตฌ๋งคํ•œ๋‹ค๋ฉด, 15 15๋Š” ํ•ฉ์ณ์‚ด ๊ฒฝ์šฐ ์ž์—ฐ์Šค๋ ˆ ๊ตฌ๋งคํ•˜๊ฒŒ ๋˜๋Š” ๋•…์ด๋‹ค. ํ•ฉ์ณ์‚ฌ์ง€ ์•Š๋Š” ๋‹ค๋ฉด ์˜คํžˆ๋ ค ๋น„์šฉ๋งŒ ๋Š˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ตญ ์ด ๋•…์€ ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•˜๋Š”๋ฐ ๋„์›€์ด ๋˜์ง€ ์•Š๋Š” ๋•…์ด๋‹ค. ๋˜ํ•œ ์ด ๋•…์„ ๋ฐฐ์—ด์— ๊ฐ–๊ณ  ์žˆ๊ฒŒ ๋œ๋‹ค๋ฉด, ์šฐ๋ฆฌ๊ฐ€ ์œ„์—์„œ ๋งŒ๋“ค๊ณ  ์‹ถ์€ ์ •๋ ฌ ๊ทœ์น™์— ์œ„๋ฐฐ๋˜๋ฏ€๋กœ์จ ์ƒ์ˆ˜์‹œ๊ฐ„ ์•ˆ์— ๊ตฌํ•˜๋Š” ๊ฒƒ์ด ์–ด๋ ค์›Œ์ง„๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋Ÿฐ ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๋Š” ๋•…์€ ์ œ๊ฑฐํ•œ๋‹ค.

์ •๋ ฌํ•œ ๋•…์—์„œ, ์ง€๊ธˆ ๋•…์˜ ๋†’์ด๋ณด๋‹ค ๋‹ค์Œ ๋•…์˜ ๋†’์ด๊ฐ€ ํฌ๋ฉด ์ถ”๊ฐ€ํ•œ๋‹ค.

์ ํ™”์‹

dp[i] = min(dp[j-1] + (ground[j].w * ground[i].h)) > (๋‹จ, i >= j >= 1)

Code

#include<iostream>
#include<vector>
#include<string>
#include <algorithm>
using namespace std;
 
typedef long long ll;
struct Square {
    ll w, h;
};
 
int N;
vector<Square> temp;
vector<Square> ground;
vector<ll> dp;
 
bool compare(Square a, Square b){
    if (a.w == b.w) return a.h < b.h;
    return a.w > b.w;
}
 
int main(){
    cin >> N;
    temp.resize(N);
    for (int i = 0; i < N; i++) {
        cin >> temp[i].w >> temp[i].h;
    }
    sort(temp.begin(), temp.end(), compare);
 
    ground.push_back(temp[0]);
    ground.push_back(temp[0]);
    for (int i = 1; i < N; i++) {
        Square next = temp[i];
        Square now = ground.back();
        if (next.h > now.h) ground.push_back(next);
    }
 
    int size = int(ground.size());
    dp.resize(size);
 
    for (int i = 1; i < size; i++) {
        dp[i] = dp[i-1] + ground[i].w * ground[i].h;
        for (int j = i; j > 0; j--) {
            dp[i] = min(dp[i], dp[j-1] + (ground[j].w * ground[i].h));
        }
    }
    cout << dp[size-1] << '\n';
    return 0;
}

Convex Hull

ํ•˜์ง€๋งŒ.. ํ”Œ๋ ˆํ‹ฐ๋„˜์˜ ์œ„์—„์ด ์žˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ’€๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚œ๋‹ค. ๋‹น์—ฐํžˆ ์ž…๋ ฅ์ด 1000000์ด๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์•…์˜ ๊ฒฝ์šฐ ์ด๋‹ค. ๊ทธ๋ž˜์„œ ์—ฌ๊ธฐ์„œ๋Š” ์ถ”๊ฐ€์ ์ธ ์ตœ์ ํ™”๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

Reference