Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14-redish03 #55

Merged
merged 3 commits into from
Feb 18, 2024
Merged

14-redish03 #55

merged 3 commits into from
Feb 18, 2024

Conversation

Redish03
Copy link
Collaborator

@Redish03 Redish03 commented Feb 12, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

ํ‰๋ฒ”ํ•œ ๋ฐฐ๋‚ญ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

2day + a -> ํฌ๊ธฐ..

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

โ“ ๋ฌธ์ œ ์ดํ•ด

  • ๋ฌผ๊ฑด์ด N๊ฐœ ์ฃผ์–ด์ง€๊ณ , ๊ฐ ๋ฌผ๊ฑด์€ ๋ฌด๊ฒŒ w์™€ ๊ฐ€์น˜ v ๋ฅผ ๊ฐ€์ง„๋‹ค. ํ•ด๋‹น ๋ฌผ๊ฑด์„ ๋ฐฐ๋‚ญ์— ๋„ฃ์œผ๋ฉด V๋งŒํผ ์ฆ๊ธธ ์ˆ˜ ์žˆ๋‹ค.
  • ๊ฐ€๋ฐฉ์— ๋ฌด๊ฒŒ ์ œํ•œ์ด ์žˆ๋‹ค. ๊ฐ€๋ฐฉ์— ์ตœ๋Œ€ K ๋งŒํผ์˜ ๋ฌด๊ฒŒ๋งŒ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

โ— ๋ฌธ์ œ ์ ‘๊ทผ 1

์ผ๋‹จ ์žฌ๊ท€๋ถ€ํ„ฐ ์ƒ๊ฐํ–ˆ๋‹ค. ์™„ํƒ์— ๊ฐ€๊นŒ์šด ๋Š๋‚Œ์ธ๋ฐ, ์‚ฌ์‹ค ์“ฐ๋‹ค๊ฐ€ ์•„๋‹Œ ๊ฒƒ ๊ฐ™์•„์„œ ๊ทธ๋งŒ๋’€๋‹ค. ์‹œ๊ฐ„ ์ œํ•œ๋„ ์žˆ๊ณ , ๋กœ์ง์„ ์งค ๋จธ๋ฆฌ๊ฐ€ ์•ˆ๋๋‹ค. ใ…Žใ…Ž

#include <iostream>
#include <vector>

using namespace std;

int N, K;
vector<pair<int, int>> items(100);
int max_enjoy = -987654321;
// int max_enjoy[100];
// first -> ๋ฌด๊ฒŒ w, second -> ๊ฐ€์น˜ v

void rec(int cur_weight, int idx, int cur_enjoy)
{
    for (int i = idx; i < N; i++)
    {
        // cur_weight๊ตฌํ•˜๊ธฐ
        // ๋งŒ์•ฝ K ๋ณด๋‹ค ํฌ๋‹ค๋ฉด return
        //

        if (cur_weight <= K)
        {
            cur_weight += items[i].first;
            cur_enjoy += items[i].second;
        }
        rec(cur_weight, i, cur_enjoy);
        cur_weight -= items[i].first;
        cur_enjoy -= items[i].second;
    }
}

int main()
{
    cin >> N >> K;

    for (int i = 0; i < N; i++)
    {
        cin >> items[i].first >> items[i].second;
    }

    rec(0, 0, 0);

    cout << max_enjoy;
}
  • ์‚ฌ์‹ค ์žฌ๊ท€ ์ž˜๋งŒ ์งœ๋ฉด ๊ฐ€๋Šฅ..ํ•  ๊ฒƒ ๊ฐ™๋‹ค. ์•„๋‹Œ ๊ฑธ ์•Œ์•˜์–ด์„œ ํ•˜๋‹ค๊ฐ€ ๊ทธ๋งŒ๋‘”..

โ—๋ฌธ์ œ ์ ‘๊ทผ 2

  • DP๋กœ ์–ด๋–ป๊ฒŒ ํ’€์ง€ ๊ณ ์˜ค์˜ค๋ฏผ ํ•˜๋‹ค๊ฐ€ ๊ฒฐ๊ตญ ํฌ๊ธฐํ•˜๊ณ  ๋‹ต์ง€๋ฅผ ๋ดค๋‹ค. ๋‚œ...์‘์•  ๐Ÿ‘ถ ๐Ÿผ

  • ๋ฐฐ๋‚ญ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ DP์˜ ๋Œ€ํ‘œ์ ์ธ ์œ ํ˜• ์ค‘ ํ•˜๋‚˜์ด๋‹ค.
    image

  • ์ผ๋‹จ, ๋ฌธ์ œ๋ฅผ ์ข€ ๋ณธ์งˆ์ ์œผ๋กœ ์ƒ๊ฐํ•ด๋ณด์ž. ๊ฐ€๋ฐฉ์—๋Š” ๋ฌด๊ฒŒ ์ œํ•œ์ด ์žˆ๋‹ค. ๋งŒ์•ฝ ๋ฌด๊ฒŒ ์ œํ•œ์„ ๋„˜์–ด์„œ๋ฉด, ์–ด๋Š ์•„์ดํ…œ๋„ ๋„ฃ์„ ์ˆ˜ ์—†๊ฒŒ ๋œ๋‹ค.

  • ์•„๋ฌด ๊ฒƒ๋„ ์•ˆ ๋„ฃ์—ˆ๊ฑฐ๋‚˜, ๋ฌด๊ฒŒ ์ œํ•œ์„ ๋„˜๊ธฐ์ง€ ์•Š๊ณ  ๋ฌผ๊ฑด์„ ๋„ฃ์œผ๋ฉด ์šฐ๋ฆฌ๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์„ ํƒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

    • ๋ฌผ๊ฑด์„ ๊ฐ€๋ฐฉ์— ๋„ฃ๋Š”๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ€๋ฐฉ์˜ ๋ฌด๊ฒŒ๋Š” ์ฆ๊ฐ€ํ•œ๋‹ค.
    • ๋ฌผ๊ฑด์„ ๊ฐ€๋ฐฉ์— ๋„ฃ์ง€ ์•Š๋Š”๋‹ค. ๊ฐ€๋ฐฉ์˜ ๋ฌด๊ฒŒ๋Š” ๊ทธ๋Œ€๋กœ.
  • ๊ทธ๋ฆฌ๊ณ  ์šฐ๋ฆฌ๊ฐ€ ๊ตฌํ•ด์•ผ ํ•  ๊ฒƒ์€, ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๊ฐ€์น˜์ด๋‹ค. ์›๋ž˜ ๊ฐ ์นธ๋งˆ๋‹ค์˜ ์ตœ๋Œ€ ๊ฐ€์น˜๋ฅผ ์ €์žฅํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ, ๋งค๋ฒˆ ๋งˆ๋‹ค ๊ฐ€๋ฐฉ์— ์–ผ๋งˆ์˜ ๋ฌด๊ฒŒ๊ฐ€ ๋“ค์–ด ๊ฐˆ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค.

๐Ÿ’ป ๋ฌธ์ œ ํ’€์ด

  • ๋ƒ…์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ๋Š” 2์ฐจ์› ๋ฐฐ์—ด์„ ์ด์šฉํ•œ๋‹ค. i๋ฒˆ์งธ ์•„์ดํ…œ์— ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฐฐ๋‚ญ ๋ฌด๊ฒŒ๊ฐ€ w์ผ ๋•Œ, ์ตœ๋Œ€ ๊ฐ€์น˜max_value๋ฅผ ์ €์žฅํ•œ๋‹ค.
  • ์ด๋ฅผ ํ‘œํ˜„ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
dp[i][w] = max_value

๊ทธ๋Ÿผ dp๋ฐฐ์—ด์—๋Š”, ์ตœ๋Œ€ ๋ฌด๊ฒŒ์™€ ์•„์ดํ…œ ๊ฐœ์ˆ˜๋งŒํผ ์„ ์–ธํ•˜๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์„ ์–ธํ•œ dp ๋ฐฐ์—ด์˜ ๋ฒ”์œ„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

int dp[101][100001];

๋ฌธ์ œ ์ž…๋ ฅ์—์„œ๋Š”
image
๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฃผ์–ด์กŒ๋‹ค. ์ด๋ฅผ ์ข€ ๋” ๋ณด๊ธฐ ์‰ฝ๊ฒŒ ๊ฐ€๋กœ๋กœ ๋ˆ•ํ˜€์ฃผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

์ฒซ ๋ฒˆ์งธ item ๋‘ ๋ฒˆ์งธ item ์„ธ ๋ฒˆ์งธ item ๋„ค ๋ฒˆ์งธ item
๋ฌด๊ฒŒ 6 4 3 5
๊ฐ€์น˜ 13 8 6 12

๊ทธ๋ฆฌ๊ณ  ์•„๊นŒ ์œ„์—์„œ ๋‚˜๋ˆˆ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ์ƒ๊ฐํ•ด ์ฃผ์ž.

  1. ๋งŒ์•ฝ ๋ฐฐ๋‚ญ์— ์ด๋ฏธ ๋‹ค ์ฐผ๋‹ค๋ฉด.
  • ๊ทธ๋Ÿผ ์šฐ๋ฆฌ๋Š” ์„ ํƒ์ง€๊ฐ€ ์—†๋‹ค. ๋ฌผ๊ฑด์„ ๋„ฃ์„ ์ˆ˜ ์—†๋‹ค. i ๋ฒˆ์งธ ์•„์ดํ…œ์„ ํ™•์ธํ•œ๋‹ค ํ–ˆ์„ ๋•Œ, ๊ทธ๋Ÿผ ์ด ๊ฐ’์€ ์ด์ „์˜ ๊ฐ’, ์ฆ‰ ์ด์ „์˜ ์ตœ๋Œ€ ๊ฐ€์น˜์™€ ๊ฐ™๋‹ค.
  • ์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
dp[i][j] = dp[i - 1][j];
  1. ๋งŒ์•ฝ ๋ฐฐ๋‚ญ์— ๋‹ค ์ฐจ์ง€ ์•Š์•˜๋‹ค๋ฉด
  • ์šฐ๋ฆฌ๋Š” ๋‘ ๊ฐ€์ง€ ์„ ํƒ์ง€๊ฐ€ ์žˆ๋‹ค.

    • i ๋ฒˆ์งธ ์•„์ดํ…œ์„ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ (๊ฐ€๋ฐฉ์— ๋„ฃ๋Š” ๊ฒฝ์šฐ)
      • ๊ทธ๋Ÿผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฐ€๋ฐฉ์˜ ๋ฌด๊ฒŒ๊ฐ€ ์ค„์–ด๋“ ๋‹ค. ๊ทธ๋Œ€์‹ , ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ€์น˜๋Š” ์ฆ๊ฐ€ํ•œ๋‹ค.
      • ๊ฐ€๋ฐฉ์˜ ๋ฌด๊ฒŒ๋Š” weight๋งŒํผ ์ค„์—ฌ์ฃผ๊ณ , ๊ฐ€์น˜๋Š” dp์— ๋”ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค
      • ์‹์œผ๋กœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
      •  dp[i - 1][j - weight[i]] + value[i];
    • i ๋ฒˆ์งธ ์•„์ดํ…œ์„ ์„ ํƒํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ (๊ฐ€๋ฐฉ์— ๋„ฃ์ง€ ์•Š๋Š” ๊ฒฝ์šฐ)
      • ์ด ๊ฒฝ์šฐ, "๋ฐฐ๋‚ญ์ด ์ด๋ฏธ ๋‹ค ์ฐฌ ๊ฒฝ์šฐ"์™€ ๊ฐ™์ด ์ƒ๊ฐํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค. ๊ฒฐ๊ตญ ์•„์ดํ…œ์„ ์„ ํƒํ•˜์ง€ ์•Š์€ ๊ฒƒ์ด๋ฏ€๋กœ, ์ด ๊ฐ’์€ ์ด์ „์˜ ๊ฐ’, ์ฆ‰ ์ด์ „์˜ ์ตœ๋Œ€ ๊ฐ€์น˜์™€ ๊ฐ™๋‹ค. ์‹์€ "๋ฐฐ๋‚ญ์ด ์ด๋ฏธ ๋‹ค ์ฐฌ ๊ฒฝ์šฐ"์™€ ๊ฐ™๋‹ค.
  • ์šฐ๋ฆฌ๋Š” ๊ฐ€๋ฐฉ์ด ๋‹ค ์ฐจ์ง€ ์•Š์•˜๋‹ค๋ฉด ๊ฐ€๋ฐฉ์— ๋„ฃ๊ฑฐ๋‚˜, ๊ฐ€๋ฐฉ์— ๋„ฃ์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋ฅผ ์„ ํƒํ•ด์ค˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ด ๋ฌธ์ œ๋Š” ์ตœ๋Œ€ ๊ฐ€์น˜๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•ด์ค€๋‹ค.

dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);

์œ„ ๊ฒฝ์šฐ๋“ค์„ ์ฝ”๋“œ๋กœ ์ •๋ฆฌํ•ด์ฃผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • ์ด๋ฅผ ๊ฐ๊ฐ ๋ชจ๋“  ๊ฒฝ์šฐ์— ๋Œ€ํ•ด ์ƒ๊ฐํ•œ๋‹ค. ๋ชจ๋“  ๊ฒฝ์šฐ๋Š”, item์˜ ์ˆœ์„œ๊ฐ€ i๋ฒˆ์งธ ์ผ ๋•Œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฐ€๋ฐฉ์˜ ๋ฌด๊ฒŒ๊ฐ€ w๋ผ๋ฉด์˜ ๊ฒฝ์šฐ์ด๋‹ค. ์ด๋Š” 1๋ฒˆ์งธ ์•„์ดํ…œ์˜ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ๋ฌด๊ฒŒ๊ฐ€ 1kg์ผ ๋•Œ ๋ถ€ํ„ฐ ๊ฐ€๋ฐฉ์˜ ์ตœ๋Œ€ ์šฉ๋Ÿ‰์ธ 7kg๊นŒ์ง€ ์ผ ๋•Œ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.
    for (int i = 1; i <= N; i++)
    {
        for (int j = 1; j <= K; j++)
        {
            if (j >= weight[i])
            {
                dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);
            }
            else
            {
                dp[i][j] = dp[i - 1][j];
            }
        }
    }

์šฐ๋ฆฌ๋Š” ๋งˆ์ง€๋ง‰ ์•„์ดํ…œ๊นŒ์ง€ ํฌํ•จํ•ด์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ ๋ฐฐ๋‚ญ ๋ฌด๊ฒŒ๊ฐ€ K์ผ ๋•Œ ๊นŒ์ง€ ์ผ ๋•Œ๋ฏ€๋กœ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅํ•ด์ค€๋‹ค.

cout << dp[N][K];

์ „์ฒด ์ฝ”๋“œ

#include <iostream>

using namespace std;

int N, K;
int dp[101][100001];
int weight[101];
int value[101];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> N >> K;

    for (int i = 1; i <= N; i++)
    {
        cin >> weight[i] >> value[i];
    }

    for (int i = 1; i <= N; i++)
    {
        for (int j = 1; j <= K; j++)
        {
            if (j >= weight[i])
            {
                dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);
            }
            else
            {
                dp[i][j] = dp[i - 1][j];
            }
        }
    }

    cout << dp[N][K];
}

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

  • ๋ฐฐ๋‚ญ ๋ฌธ์ œ ์ž์ฒด๋ฅผ ์ฒ˜์Œ ๊ฒฝํ—˜ํ•ด๋ดค๋‹ค. ์˜ˆ์ „ ์ด์ „ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์Šคํ„ฐ๋”” ํ•  ๋•Œ ์šฐ๋™ ํ˜ธ์”จ๊ฐ€ ์„ค๋ช… ํ•ด์คฌ์—ˆ๋Š”๋ฐ ๋‹ค ๊นŒ๋จน์–ด ๋ฒ„๋ ธ๋‹ค ์ฝ”ใ…‹ใ…Žใ…‹ใ…‹ใ…‹ใ…Ž
    ํ˜ธ์šฐ๋™ KnapSack
  • ๊ทธ๋Š” ์„ค๋ช…์˜ ์‹ ์ด์•ผ,,
  • ์„ธ ๋ฐฐ์—ด์„ main ํ•จ์ˆ˜์— ์„ ์–ธ ํ–ˆ๋”๋‹ˆ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ„ฐ์กŒ๋‹ค.
int dp[101][100001];
int weight[101];
int value[101];

์ด๊ฑธ main์•ˆ์— ๋„ฃ์—ˆ๋”๋‹ˆ input์ด ์•ˆ๋ฐ›์•„์ง€๊ณ  ๊บผ์ ธ์„œ ๋งŽ์ด ๋‹นํ™ฉํ–ˆ๋‹ค ใ…‹ใ…‹ใ…‹ ใ… ใ…  ๋ฉ”๋ชจ๋ฆฌ๋„ ๊ด€๋ฆฌํ•ด์•ผํ•œ๋‹ค..

@9kyo-hwang
Copy link

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    
    int N, K; 
    cin >> N >> K;
    
    vector<pair<int, int>> Objects(N);
    for(auto& [Weight, Value] : Objects)
    {
        cin >> Weight >> Value;
    }
    sort(Objects.begin(), Objects.end());
    
    vector<int> MaxValues(K + 1, 0);
    for(const auto&[Weight, Value] : Objects)
    {
        for(int MaxLoad = K; MaxLoad >= 0; --MaxLoad)
        {
            if(Weight > MaxLoad)
            {
                break;
            }
            
            MaxValues[MaxLoad] = max(MaxValues[MaxLoad], MaxValues[MaxLoad - Weight] + Value);
        }
    }
    
    cout << MaxValues[K];

    return 0;
}

แ„‘แ…งแ†ผแ„‡แ…ฅแ†ทแ„’แ…กแ†ซ-แ„‡แ…ขแ„‚แ…กแ†ผ

์ •๋ ฌ์„ ํ•ด์ค€ ์ด์œ ๋Š”, ๊ฐ€๋ฒผ์šด ๋ฌด๊ฒŒ์˜ ๋ฌผ๊ฑด๋ถ€ํ„ฐ ๊ฒ€์‚ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ฒ€์‚ฌํ•  ์ˆ˜๋ก ํ™•์ธํ•ด์•ผ ํ•˜๋Š” ๋ฒ”์œ„๊ฐ€ ์ขํ˜€์ง€๋ฉด์„œ ์†๋„ ์ƒ ์ด๋“์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.


์ €ํฌ ํŒ€์›๋„ ์ด ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์–ด์„œ ๋™์ผํ•œ ๋ฆฌ๋ทฐ๋ฅผ ๋‹ฌ์•˜์—ˆ๋Š”๋ฐ, ์ด๋ ‡๊ฒŒ 1์ฐจ์› ๋ฐฐ์—ด์„ ์ด์šฉํ•ด ํ‘ธ๋Š” ์ฝ”๋“œ๊ฐ€ ์žˆ๋”๋ผ๊ตฌ์š”.

Copy link
Member

@miniron-v miniron-v left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1์ฐจ์› DP๋กœ ํ’€๋‹ค๊ฐ€ ์ค‘๋ณต ๊ณ„์‚ฐ์—์„œ ๋ง‰ํ˜”๋Š”๋ฐ, ๊ตํ™ฉ ์”จ ์ฝ”๋ฉ˜ํŠธ(๋ฅผ ๊ฐ€์žฅํ•œ PR)๋ฅผ ๋ณด๊ณ  ํžŒํŠธ๋ฅผ ์–ป์–ด ๊นผ๋„ค์š”.

์ดˆ๊ธฐ ๋„์ „ ์ฝ”๋“œ

#include <iostream>
#include <vector>
#include <algorithm>

bool compare(std::pair<int, int> a, std::pair<int, int> b) {
	return a.first < b.first;
}

int main() {
	// ์ž…๋ ฅ
	int n, k;
	std::cin >> n >> k;

	std::vector<std::pair<int, int>> stuff(n);
	for (int i = 0; i < n; ++i) {
		std::cin >> stuff[i].first >> stuff[i].second;
	}

	std::sort(stuff.begin(), stuff.end(), compare);

	// dp
	std::vector<int> max_cost(k + 1, 0);

	for (int i = 1; i <= k; ++i) {
		for (auto& s : stuff) {
			if (i - s.first < 0) {
				break;
			}

			if (max_cost[i - s.first] + s.second > max_cost[i]) {
				max_cost[i] = max_cost[i - s.first] + s.second;
			}
		}
	}

	std::cout << max_cost[k];
}

๊ฐ ์นธ์— ๋Œ€ํ•ด ๊ณ„์‚ฐ์„ ์‹คํ–‰ํ•ด, ๊ฐ’์„ ๊ฐฑ์‹ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋žฌ๋”๋‹ˆ ๋ฌผ๊ฑด์ด ๋ฌดํ•œ๋Œ€์ธ ๊ฒฝ์šฐ ์ตœ๋Œ€์น˜๊ฐ€ ๋ฐ˜ํ™˜๋์Šต๋‹ˆ๋‹ค.

์„ฑ๊ณต ์ฝ”๋“œ

#include <iostream>
#include <vector>
#include <algorithm>

bool compare(std::pair<int, int> a, std::pair<int, int> b) {
	return a.first < b.first;
}

int main() {
	// ์ž…๋ ฅ
	int n, k;
	std::cin >> n >> k;

	std::vector<std::pair<int, int>> stuff(n);
	for (int i = 0; i < n; ++i) {
		std::cin >> stuff[i].first >> stuff[i].second;
	}

	std::sort(stuff.begin(), stuff.end(), compare);

	// dp
	std::vector<int> max_cost(k + 1, 0);

	for (auto& s : stuff) {
		for (int i = k; i >= 1; --i) {
			if (i - s.first < 0) {
				break;
			}

			if (max_cost[i - s.first] + s.second > max_cost[i]) {
				max_cost[i] = max_cost[i - s.first] + s.second;
			}
		}
	}

	std::cout << max_cost[k];
}

for๋ฌธ์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊ฟ”, ๊ฐ ๋ฌผ๊ฑด์— ๋Œ€ํ•ด ๊ณ„์‚ฐ, ๋’ค์ชฝ ์นธ๋ถ€ํ„ฐ ์ฑ„์›Œ๋‚˜๊ฐ”์Šต๋‹ˆ๋‹ค.
์ด๋Ÿฌ๋ฉด ๋ฌผ๊ฑด์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ •ํ•ด์ง„ ๊ฒฝ์šฐ์˜ ์ตœ๋Œ“๊ฐ’์ด ๋ฐ˜ํ™˜๋˜๋”๊ตฐ์š”.

๊ณ ์ฐฐ

์™œ ๊ทธ๋Ÿด๊นŒ ๊ณ ์ฐฐํ•ด๋ดค๋Š”๋ฐ, ๋จผ์ € ๊ฐ ๋ฌผ๊ฑด๋ณ„๋กœ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒฝ์šฐ, ์–ด๋–ค ๋ฌผ๊ฑด์ด ๋“ค์–ด๊ฐ€๊ฑฐ๋‚˜ ์•ˆ ๋“ค์–ด๊ฐ€๊ฑฐ๋‚˜, ๋‘ ๊ฐ€์ง€๊ฐ€ ์—ฐ์‚ฐ๋ฉ๋‹ˆ๋‹ค.
๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์œผ๋ฉด ๋“ค์–ด๊ฐ€๊ณ , ๋ชป ๋“ค์–ด๊ฐ€๋ฉด(ํ˜น์€ ๋ฌด๊ฒŒ๋Š” ๋˜๋Š”๋ฐ ๊ฐ€์น˜๊ฐ€ ์ค„์–ด๋“ค๋ฉด) ๋น ์ง€๋Š” ์‹์œผ๋กœ ์—ฐ์‚ฐ๋˜์ฃ . ๊ทธ๋ž˜์„œ ๋ฌผ๊ฑด ๊ฐœ์ˆ˜์— ๋งž๋Š” ์ตœ๋Œ“๊ฐ’์ด ๋ฐ˜ํ™˜๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๋ฐ˜๋Œ€๋กœ ๊ฐ ์นธ(๋ฌด๊ฒŒ)๋ณ„๋กœ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒฝ์šฐ, ๊ฐ ์นธ์— ๋ฌผ๊ฑด์„ ์ „๋ถ€ ๋„ฃ์–ด๋ณด๋ฉฐ ๊ณ„์‚ฐ์ด ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ค๋ณด๋‹ˆ 1๋ฒˆ ์นธ์—์„œ ๋ฌด๊ฒŒ๊ฐ€ 3์ธ ๋ฌผ์ฒด๊ฐ€ ๋“ค์–ด๊ฐ”์–ด๋„, 2๋ฒˆ ์นธ์—์„œ๋„ ๋ฌด๊ฒŒ๊ฐ€ 3์ธ ๋ฌผ์ฒด๋ฅผ ๊ฒ€์‚ฌํ•˜์ฃ . ๊ทธ๋ž˜์„œ ๋ฌผ๊ฑด์ด ๋ฌดํ•œ๋Œ€์ผ ๋•Œ ์ตœ๋Œ“๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ข€ ๋” ์ถ”์ƒํ™”ํ•˜๋ฉด ๋ฌธ์ œ ์œ ํ˜• ๋ณ„ ์ ‘๊ทผ๋ฒ•์œผ๋กœ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”.

  1. ๋ฌผ๊ฑด์ด ๋ฌดํ•œ๋Œ€๋กœ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ(์ค‘๋ณต์ด ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ)
    1-1. ๊ฐ ์นธ์„ ๊ธฐ์ค€์œผ๋กœ, ๋ชจ๋“  ๋ฌผ๊ฑด์„ ๋„ฃ์–ด๋ณด๋ฉฐ ์ง„ํ–‰
  2. ๋ฌผ๊ฑด์ด ์ œํ•œ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ(์ค‘๋ณต์ด ๋ถˆ๊ฐ€ํ•œ ๊ฒฝ์šฐ)
    2-1. ๊ฐ ๋ฌผ๊ฑด์„ ๊ธฐ์ค€์œผ๋กœ, ๋ชจ๋“  ์นธ์— ๋„ฃ์–ด๋ณด๋ฉฐ ์ง„ํ–‰

DP ๋ ˆ๋ฒจ ์—… ๋ฒ„์„ฏ ๋‚ญ๋‚ญํ•˜๊ฒŒ ๋จน๊ณ  ๊ฐ‘๋‹ˆ๋‹ค~

@Redish03 Redish03 changed the title 14 redish03 14-redish03 Feb 14, 2024
Copy link
Member

@bomik0221 bomik0221 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฌธ์ œ ํ’€์ด 2์ผ..?! ๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹คใ… ใ… 
๊ทธ๋Ÿฐ๋ฐ ๋ถ„๋ช… ์„ฑ์šฐ๋‹˜ PR์ธ๋ฐ ๋๊นŒ์ง€ ๋‹ค ์ฝ๊ณ ๋‚˜๋‹ˆ๊นŒ ์™œ ์ด ํŽ˜์ด์ง€์—๋งŒ PR์ด 3๊ฐœ์ธ๊ฑธ๊นŒ์š”...? ใ…‹ใ…‹ใ…‹
ํ•œ pr ์•ˆ์—์„œ ๋‹ค์–‘ํ•œ ํ’€์ด๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์–ด์„œ ์‹œ์•ผ๋ฅผ ๋„“ํžˆ๊ฒŒ ๋œ ๊ฒƒ ๊ฐ™์€ ๊ธฐ๋ถ„์ž…๋‹ˆ๋‹ค..!

@rivkms
Copy link

rivkms commented Feb 16, 2024

7ํŒ€์—์„œ ๊ตฌ๊ฒฝ์™”์Šต๋‹ˆ๋‹ค ๐Ÿ˜†

โœ”๏ธ ํ™•์‹คํžˆ 0,1-Knapsack ๋ฌธ์ œ๊ฐ€ 2์ฐจ์› ๋ฐฐ์—ด์„ ์“ฐ๋Š๋ƒ, 1์ฐจ์› ๋ฐฐ์—ด์„ ์“ฐ๋Š๋ƒ๋Š” ๋ฌธ์ œ์™€ ์Šคํƒ€์ผ์— ๋”ฐ๋ผ์„œ ๋งŽ์ด ๊ฐˆ๋ฆฌ๊ธด ํ•˜๋”๋ผ๊ณ ์š”.

  • ์ € ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” DP ๋งต ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„์„ ํ•˜๋‹ค๋ณด๋‹ˆ 2์ฐจ์› ๋ฐฐ์—ด์ด ํŽธํ–ˆ๊ณ  ๋˜ 1์ฐจ์› ๋ฐฐ์—ด์€ ๊ด€๊ณ„์‹์„ ์ ์„ ๋•Œ ํ–‡๊น”๋ คํ–ˆ์–ด์„œ 2์ฐจ์› ๋ฐฐ์—ด์„ ์„ ํ˜ธํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฐ˜๋ฉด์— 1์ฐจ์› ๋ฐฐ์—ด์„ ์„ ํ˜ธํ•˜์‹œ๋Š” ๋ถ„๋“ค๋„ ์žˆ๊ณ  ์˜คํžˆ๋ ค ๋ฉ”๋ชจ๋ฆฌ๋„ ๋” ์ ๊ฒŒ ์ฐจ์ง€ํ•˜๊ธฐ๋„ ํ•œ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๊ธด ํ•˜๊ฒ ์ฃ .

โœ”๏ธ ์ œ ๊ต์ˆ˜๋‹˜๊ป˜์„œ Knapsack์„ ๊ฐ•์˜ํ•ด์ฃผ์‹ค ๋•Œ๋Š” 2์ฐจ์› ๋ฐฐ์—ด๋กœ ์„ค๋ช…ํ•ด์ฃผ์…จ์œผ๋‚˜, 1์ฐจ์› ๋ฐฐ์—ด๋กœ ํ•ด๋„ ๋˜๋ƒ๋Š” ์งˆ๋ฌธ์— ๊ทธ ๋ฐฉ๋ฒ•๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ๋‹ตํ•ด์ฃผ์‹œ๊ณ  ์„ค๋ช…๋„ ์ถ”๊ฐ€๋กœ ํ•ด์ฃผ์…จ์Šต๋‹ˆ๋‹ค. โ€ผ๏ธ๋‘˜ ๋‹ค ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋ผ๋Š” ๋œป์ด๊ฒ ์ฃ โ€ผ๏ธ

๐Ÿ“ ์ด ๋ฌธ์ œ์—์„œ๋Š” ์–ด๋–ค ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋“ ์ง€ ์ƒ๊ด€์—†์ง€๋งŒ, ๋‹ค๋ฅธ DP๋ฌธ์ œ์—์„œ๋Š” ๋ฌธ์ œ ์กฐ๊ฑด๊ณผ ๊ด€๊ณ„์‹์„ ์–ด๋–ป๊ฒŒ ์งœ๋Š๋ƒ์— ๋”ฐ๋ผ ํŠน์ • ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์ด๋ฅผ ํ•ด์•ผํ•˜๋‹ˆ ๋‘˜ ๋‹ค ์•Œ์•„์•ผํ•˜๋”๋ผ๊ณ ์š”...

  • ์œ„์—์„œ ๋‘ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ž์„ธํ•˜๊ฒŒ ์„ค๋ช…ํ•ด์ฃผ์…”์„œ ๋งŽ์ด ๋ฐฐ์šธ ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค ๐Ÿ‘

์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค ๐Ÿ˜

@miniron-v
Copy link
Member

  • ์ € ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” DP ๋งต ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„์„ ํ•˜๋‹ค๋ณด๋‹ˆ 2์ฐจ์› ๋ฐฐ์—ด์ด ํŽธํ–ˆ๊ณ  ๋˜ 1์ฐจ์› ๋ฐฐ์—ด์€ ๊ด€๊ณ„์‹์„ ์ ์„ ๋•Œ ํ–‡๊น”๋ คํ–ˆ์–ด์„œ 2์ฐจ์› ๋ฐฐ์—ด์„ ์„ ํ˜ธํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฐ˜๋ฉด์— 1์ฐจ์› ๋ฐฐ์—ด์„ ์„ ํ˜ธํ•˜์‹œ๋Š” ๋ถ„๋“ค๋„ ์žˆ๊ณ  ์˜คํžˆ๋ ค ๋ฉ”๋ชจ๋ฆฌ๋„ ๋” ์ ๊ฒŒ ์ฐจ์ง€ํ•˜๊ธฐ๋„ ํ•œ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๊ธด ํ•˜๊ฒ ์ฃ .

โœ”๏ธ ์ œ ๊ต์ˆ˜๋‹˜๊ป˜์„œ Knapsack์„ ๊ฐ•์˜ํ•ด์ฃผ์‹ค ๋•Œ๋Š” 2์ฐจ์› ๋ฐฐ์—ด๋กœ ์„ค๋ช…ํ•ด์ฃผ์…จ์œผ๋‚˜, 1์ฐจ์› ๋ฐฐ์—ด๋กœ ํ•ด๋„ ๋˜๋ƒ๋Š” ์งˆ๋ฌธ์— ๊ทธ ๋ฐฉ๋ฒ•๋„ ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ๋‹ตํ•ด์ฃผ์‹œ๊ณ  ์„ค๋ช…๋„ ์ถ”๊ฐ€๋กœ ํ•ด์ฃผ์…จ์Šต๋‹ˆ๋‹ค. โ€ผ๏ธ๋‘˜ ๋‹ค ์ข‹์€ ๋ฐฉ๋ฒ•์ด๋ผ๋Š” ๋œป์ด๊ฒ ์ฃ โ€ผ๏ธ

๐Ÿ“ ์ด ๋ฌธ์ œ์—์„œ๋Š” ์–ด๋–ค ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋“ ์ง€ ์ƒ๊ด€์—†์ง€๋งŒ, ๋‹ค๋ฅธ DP๋ฌธ์ œ์—์„œ๋Š” ๋ฌธ์ œ ์กฐ๊ฑด๊ณผ ๊ด€๊ณ„์‹์„ ์–ด๋–ป๊ฒŒ ์งœ๋Š๋ƒ์— ๋”ฐ๋ผ ํŠน์ • ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์ด๋ฅผ ํ•ด์•ผํ•˜๋‹ˆ ๋‘˜ ๋‹ค ์•Œ์•„์•ผํ•˜๋”๋ผ๊ณ ์š”...

ํ™•์‹คํžˆ ์ „ 1์ฐจ์› ๋ฐฐ์—ด๋กœ ์“ฐ๋Š” ๊ฒŒ ๋” ์ง๊ด€์ ์ด์–ด์„œ 1์ฐจ์› ๋ฐฐ์—ด์„ ์„ ํ˜ธํ•˜๋Š”๋ฐ, ๋ฏผ์„ฑ๋‹˜ ์ฝ”๋ฉ˜ํŠธ๋ฅผ ๋ณด๋‹ˆ ๋‘˜ ๋‹ค ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์–ด์•ผ๊ฒ ๋‹ค๊ณ  ์ƒใ„ฑ๊ฐ์ด ๋“œ๋„ค์š”. 2์ฐจ์›์œผ๋กœ ์ƒ๊ฐํ•˜๊ณ  ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ํ•ด๋ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค!

@Redish03 Redish03 merged commit efeb6c6 into main Feb 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants