-
Notifications
You must be signed in to change notification settings - Fork 1
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
1-rivkms #5
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DP tableμ ν΅ν μ€λͺ μ΄ μκ°μ μΌλ‘ μ΄ν΄νκΈ° μμ£Ό μ’μμ΅λλ€. π
λ¬Έμ λ₯Ό μ²μ μ§λ©΄νμ λ νμλ²μ΄ μ§κ΄μ μΌλ‘ λ μ¬λλλ° μ°Έκ³ κΈμ μ½μ΄λ³΄λ λ¬Όνμ΄ λΆν κ°λ₯ν λ νμλ²μ μ¬μ©νλ€λ μ μ μκ² λμμ΅λλ€.
μ΄λ² νμ΄λ₯Ό ν΅ν΄ DP μκ³ λ¦¬μ¦μ 곡λΆν μ μμμ΅λλ€. μκ³ νμ ¨μ΅λλ€βββ
int main(){ | ||
int n, k; | ||
cin >> n >> k; | ||
vector<pair<int, int>> wv(n+1, pair<int, int>(0,0)); | ||
vector<vector<int>> value (n+1, vector<int>(k+1, 0)); | ||
for(int i = 1; i<=n; i++){ | ||
cin >> wv[i].first >> wv[i].second; | ||
} | ||
|
||
for(int i = 1; i<=n; i++){ | ||
for(int j = 1; j<=k;j++){ | ||
if(wv[i].first > j ){ | ||
value[i][j] = value[i-1][j]; | ||
} | ||
else{ | ||
value[i][j] = max(value[i-1][j], value[i-1][j-wv[i].first]+wv[i].second); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ κ° c++μμ μ½λꡬνμ΄ μ΅μνμ§ μμ μ¬μ€λ³΄μλ©΄
μμ λΈλμμ μ
λ ₯κ°μ λ°κ³ value
λ°°μ΄μ μμ±ν λ€,
μλ forλ¬Έμμ κ΄κ³μμ μ΄μ©νμ¬ value
λ°°μ΄μ μ±μλκ°λ€κ³ μ΄ν΄νλ©΄ λ κΉμ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ΅ valueκ° DP Tableμ΄λΌκ³ 보μλ©΄ λκ³ DP Tableμ κ΄κ³μμ μ΄μ©ν΄μ μ±μλκ°λ€κ³ μκ°νμλ©΄ λ©λλ€ π
PR μ λ΄€μ΅λλ€! μ μ§μ¬μ§ μ νμκ³Ό μ€λͺ μμ, μΌλ§λ μ΄μ¬ν 곡λΆνμ ¨λμ§κ° μ λλ¬λλ€μ. μ λ μ ν¬ ν(5ν) 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 (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];
} 2μ°¨μ λ°°μ΄μ νμ μμλλ‘ λ΄€μ λ, νμ μλμͺ½ νμ΄ μμͺ½ νλ³΄λ€ κ°μ΄ ν° μ¬μ€μ λ°κ²¬ν μ μμ£ . κ²λ€κ° νμ λ°λ‘ μ νλ§ μ κ·Όνλ, 리ν©ν λ§ μ€ ν μ€ λ°°μ΄μ΄λ©΄ μΆ©λΆνκ² λ€λΌλ μκ°μ΄ κΈλ°© λ€ κ² κ°μμ. (λ§μ§λ§ νμ μ μΈνλ©΄ κΈ°μ΅ν νμκ° μμΌλκΉμ.) 첫 PRμΈλ°λ μμΈνκ³ λͺ νν μ€λͺ κ°μ¬λ립λλ€. κ³ μνμ ¨μ΄μ! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ’μ μμ΄λμ΄μ κ·Έκ²μ μ½λλ‘ κ΅¬νν κ²λ ν μ‘μ κ³³ μ‘°μ°¨ μλ€κ³ μκ°ν©λλ€. μκ³ νμ ¨μ΅λλ€!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
κ·Έλ¦Όμ΄ μμΌλκΉ μκ³ λ¦¬μ¦ μ΄ν΄κ° νμ€ν μλλ€μ. λλΆμ μ’μ μμ΄λμ΄ νλ μ»κ³ κ°λλ€!
π λ¬Έμ λ§ν¬
νλ²ν λ°°λ
βοΈ μμλ μκ°
1h
β¨ μλ μ½λ
π·λ¬Έμ μ μ κ³Όμ
λλ¦ μ²« μκ°μ΄λκΉ κ·Έλμ λ§μ΄ 곡λΆνμλ DP, κ·Έμ€μμλ knapsack λ¬Έμ λ₯Ό κ°μ§κ³ μμ΅λλ€.
λ¬Έμ μ΄ν΄
λ¬Έμ μμμ μ λ ₯μ λ€μκ³Ό κ°μ΅λλ€.
μ 4κ°μ§ μ λ ₯μ μ΄μ©νμ¬ λ°°λμ λ£μ μ μλ μ΅λμ κ°μΉλ₯Ό μ°Ύλ λ¬Έμ μ λλ€.
λ¬Έμ νμ΄
μΌλ¨ DPλ₯Ό νκΈ° μνμ¬ μ΄λ»κ² νλ©΄ μ΅λμ κ°μΉλ₯Ό κ°μ§λμ§ μ‘°κΈ λ μ§κ΄μ μΌλ‘ μκΈ° μν΄ DP Tableμ κ·Έλ €λ΄ μλ€.
1οΈβ£ μΌλ°μ μΈ κ²½μ° :
μμ νμμ (3,7)μ κ°μ΄ μ΄λ»κ² λμ€κ² λμλμ§ μκ°ν΄λ³ΌκΉμ?
μ°λ¦¬λ (2,7) κ³Ό (2,4)+3λ²μ§Έ 물건μ κ°μΉ λ₯Ό λΉκ΅νμ¬ κ·Έ μ€ ν° κ²μ μ°Ύκ² λ©λλ€.
κ·Έ κ²°κ³Ό 13 vs 8+6=14λ‘μ 14κ° λ ν¬λ (3,7)μλ 14 κ° λ€μ΄κ°κ² λ©λλ€.
2οΈβ£ νΉλ³ν κ²½μ° :
μμ νμμ (4,3)μ κ°μ λ³ΌκΉμ?
4λ²μ§Έ 물건μ 무κ²μΈ 5λ³΄λ€ λ² λμ ν¬κΈ°κ° μμ ν΄λΉ 물건μ λ°°λμ λ£μ§ λͺ»ν©λλ€.
κ·Έλμ (3,3) μ κ°μ΄ λ°λ‘ λ΄λ €μ€κ² λκ² μ£ .
βοΈμ΄ νλ₯Ό ValueλΌκ³ λͺ λͺ ν λ, κ²°κ³Όμ μΌλ‘, λ€μμ κ΄κ³μμ μ μ μ μμ΅λλ€.
μ κ΄κ³μμ μ ꡬν΄λλ€λ©΄, μ΄μ μ½λλ‘ μ§κΈ°λ§ νλ©΄ λ©λλ€!!
μ½λ μμ±
λ₯Ό μ½λλ‘ μ°λ©΄ λ€μκ³Ό κ°κ² μ£
π μλ‘κ² μκ²λ λ΄μ©
π’ λ§μ½ 물건μ μ€λ³΅ν μ μλ€λ©΄ κ΄κ³μμΌλ‘ νλ©΄ λκ² μ£ ?
μ κ·Έλ°μ§λ νλ² μκ°ν΄λ³΄μκΈΈ
π’ μ΄ λ°©λ² μΈμλ λ€μν λ°©λ²μ΄ μλ κ² κ°μ΅λλ€.
μ΄ κΈμμ μ μ€λͺ ν΄λ κ² κ°μ 첨λΆν΄λ΄ λλ€.
[μκ³ λ¦¬μ¦ - μ΄λ‘ ] 0-1 KnapSack Problem and Fractional
νλ² μ°Έκ³ ν΄λ³΄μλ©΄ μ’μ κ² κ°μ΅λλ€.
π μ μ½λλ₯Ό μ‘°κΈ λ κ°μ ν΄λ³΄μλ©΄,
1οΈβ£ μμ νμμ 맨 μ²μ 0μΈ κ°λ€ μμ κΈ° -> 물건μ 무κ²μ μ΅μκ°μ κΈ°μ€μΌλ‘ μλ₯΄κΈ°
2οΈβ£ 2μ°¨μ λ°°μ΄μ μ¬μ©νμ§ μκ³ 1μ°¨μ λ°°μ΄λ‘ νκΈ°
λ±λ±μ΄ μμ κ² κ°λ€μ