MY MEMO

[BAEKJOON] 4781 사탕 가게 본문

ALGORITHM/BAEKJOON

[BAEKJOON] 4781 사탕 가게

l_j_yeon 2017. 11. 13. 19:45
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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int cCnt, cKcal, intT, intM, intK;
    float tMoney, cMoney;
    vector<int>dp;
    vector<pair<int, int>>candy;
 
    while (1)
    {
        scanf("%d %f", &cCnt, &tMoney);
        if (cCnt == 0) break;
 
        intT = (int)(tMoney * 100 + 0.5);
        dp = vector<int>(intT + 1, 0);
        candy.clear();
 
        for (int j = 0; j < cCnt; j++)
        {
            scanf("%d %f", &cKcal, &cMoney);
            candy.push_back(make_pair((int)(cMoney * 100 + 0.5), cKcal));
        }
        sort(candy.begin(), candy.end());
        for (int j = 0; j < cCnt; j++)
        {
            intM = candy[j].first;
            intK = candy[j].second;
 
            for (int j = intM; j <= intT; j++)
                dp[j] = max(dp[j], dp[j - intM] + intK);
        }
        cout << dp[intT] << endl;
    }
    return 0;
}

vector.empty()가 vector.clear()라고 착각하고..

11번이나 컴파일을 시도했다..


그런데 굳이 sort를 하지 않아도 가능했다

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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int cCnt, cKcal, price, Total;
    float tMoney, cMoney;
    vector<int>dp;
 
    while (1)
    {
        scanf("%d %f", &cCnt, &tMoney);
        if (cCnt == 0) break;
 
        Total = (int)(tMoney * 100 + 0.5);
        dp = vector<int>(Total + 1, 0);
 
        for (int j = 0; j < cCnt; j++)
        {
            scanf("%d %f", &cKcal, &cMoney);
            price = (int)(cMoney * 100 + 0.5);
 
            for (int j = price; j <= Total; j++)
                dp[j] = max(dp[j], dp[j - price] + cKcal);
        }
        printf("%d\n", dp[Total]);
    }
    return 0;
}



'ALGORITHM > BAEKJOON' 카테고리의 다른 글

[BAEKJOON] 1695 팰린드롬 만들기  (0) 2017.11.14
[BAEKJOON] 9184 신나는 함수 실행  (0) 2017.11.14
[BAEKJOON] 2629 양팔 저울  (0) 2017.11.13
[BAEKJOON] 2410 2의 멱수의 합  (0) 2017.11.08
[BAEKJOON] 1563 개근상  (0) 2017.11.08
Comments