๋ฌธ์ ๋งํฌ
ํ์ด
์ผ๋จ ๋ด๊ฐ ์๊ฐํด๋ธ ์์ด๋์ด๋ Cost ๋ฅผ ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ ๋ค์ ๋ฐฑํธ๋ํน์ผ๋ก ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ํด๊ฐ๋ฉด์
๋ฉ๋ชจ๋ฆฌ์ ํฉ์ด M์ ๋์ผ๋ฉด ๊ทธ๋์ Cost๋ฅผ ๊ธฐ์กด๊ณผ ๋น๊ตํด์ ์ต์๊ฐ์ ์ ์ฅํ๋ ๋ฐฉ์.
๋จผ์ ์ฑ์ ๋ํด์ ์ฐ๋ฆฌ๊ฐ ์ทจํ ์ ์๋ ๊ฒฝ์ฐ์ ์๋
1. ๋นํ์ฑํ ํ๋ค
2. ๊ทธ๋๋ก ๋๋ค
๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ด ์๋ค. ์ด๋ฅผ ๋ฐํ์ผ๋ก ๋ฐฑํธ๋ํน์ ์์ฑํ๋ฉด ์๋์ ์ฝ๋๊ฐ ๋๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <climits>
#include <algorithm>
#include <tuple>
#include <cmath>
using namespace std;
typedef long long ll;
int N, M;
const int MAX_N = 10000000 + 5;
vector<int> Cost;
vector<int> Mem;
vector<pair<int, int>> App;
int ans = INT_MAX;
void backtracking(int depth, int mem_sum, int cost_sum) {
if (mem_sum >= M) {
ans = min(cost_sum, ans);
return;
}
if (depth == N)
return;
int now_cost = App[depth].first;
int now_mem = App[depth].second;
backtracking(depth + 1, mem_sum + now_mem, cost_sum + now_cost);
backtracking(depth + 1, mem_sum, cost_sum);
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
int mem, cost;
App.push_back({ 0,0 });
for (int i = 0; i < N; i++) {
cin >> mem;
Mem.push_back(mem);
}
for (int i = 0; i < N; i++) {
cin >> cost;
Cost.push_back(cost);
}
for (int i = 0; i < N; i++) {
App.push_back({ Cost[i], Mem[i] });
}
sort(App.begin(), App.end());
backtracking(0, 0, 0);
cout << ans << "\n";
return 0;
}
|
cs |
ํ์ง๋ง ์ด๋ ๊ฒ ์ฌ์ธ๋ฆฌ๊ฐ ์๋ค. ์ด๋ฆผ๋์์ด ์๊ฐ์ด๊ณผ.
๋๋ ๋ ์์ ์ ์ฅ๋ฐฉ์์ ์ด์ฉํ๋ ค ํ๋ค dp๋ผ๋ 2์ฐจ์ ๋ฐฐ์ด์ ๋ง๋ค์ด dp[depth][cost] ์ ๋นํ์ฑํ ์ํจ memory๋ฅผ ์ ์ฅํ๋ค.
๊ทธ๋ ๊ฒ ํด์ ๊ฐ์ depth์ ๊ฐ์ cost๋ฅผ ์ง๋ถํ๋ค๋ฉด ๋นํ์ฑํํ ๋ฉ๋ชจ๋ฆฌ๊ฐ ์ ์ ์๋ก ๋นํจ์จ์ ์ธ ๊ฒ์ด๋ค.
๊ทธ๋์ if(dp[depth][cost] >= ํ์ฌ๊น์ง memory ํฉ) ์ด๋ฉด return; ์ ํ๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <climits>
#include <algorithm>
#include <tuple>
#include <cmath>
using namespace std;
typedef long long ll;
int N, M;
const int MAX_N = 10000000 + 5;
vector<int> Cost;
vector<int> Mem;
vector<pair<int, int>> App;
int ans = INT_MAX;
int dp[101][10005];
void backtracking(int depth, int mem_sum, int cost_sum) {
if (mem_sum >= M) {
ans = min(cost_sum, ans);
return;
}
if (depth == N || dp[depth][cost_sum] >= mem_sum) {
return;
}
int now_cost = App[depth].first;
int now_mem = App[depth].second;
dp[depth][cost_sum] = mem_sum;
backtracking(depth + 1, mem_sum + now_mem, cost_sum + now_cost);
backtracking(depth + 1, mem_sum, cost_sum);
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
int mem, cost;
for (int i = 0; i < N; i++) {
cin >> mem;
Mem.push_back(mem);
}
for (int i = 0; i < N; i++) {
cin >> cost;
Cost.push_back(cost);
}
for (int i = 0; i < N; i++) {
App.push_back({ Cost[i], Mem[i] });
}
sort(App.begin(), App.end());
memset(dp, -1, sizeof(dp));
backtracking(0, 0, 0);
cout << ans << "\n";
return 0;
}
|
cs |
'๋ฐฑ์ค > Gold' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 9370 : ๋ฏธํ์ธ ๋์ฐฉ์ง (0) | 2022.01.24 |
---|---|
[BOJ] 1707 : ์ด๋ถ ๊ทธ๋ํ (0) | 2022.01.21 |
[BOJ] 10942 : ํฐ๋ฆฐ๋๋กฌ? (0) | 2022.01.20 |
[BOJ] 2629 : ์ํ์ ์ธ (0) | 2022.01.19 |
[BOJ] 2661: ์ข์ ์์ด (0) | 2022.01.19 |