forked from chr4ss1/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPARTY.cpp
More file actions
76 lines (59 loc) · 1.74 KB
/
PARTY.cpp
File metadata and controls
76 lines (59 loc) · 1.74 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <utility>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
int main()
{
while(true)
{
int budget, parties;
scanf("%d %d", &budget, &parties);
if(budget == 0 && parties == 0)
break;
int bx[101][501][2] = {0};
int partyCount = parties;
vector<pair<int, int>> vec;
int index = 0;
while(parties--)
{
int fun, fee;
scanf("%d %d", &fee, &fun);
bx[index][fee][0] = fun;
bx[index][fee][1] = fee;
vec.push_back(make_pair<int, int>(fun, fee));
index++;
}
// start building from zero.
for(int party = 0; party < partyCount; party++)
{
int partyFun = vec[party].first;
int partyFee = vec[party].second;
int partyIndex = party + 1;
for(int currentBudget = 1; currentBudget <= budget; currentBudget++)
{
int previousFunWithoutParty = bx[partyIndex-1][currentBudget][0];
int previousBudgetWithoutParty = bx[partyIndex-1][currentBudget][1];
// assume explictly that this is best choice
// eg the same choice that was previously, but using parties 0..partyIndex-1
bx[partyIndex][currentBudget][0] = previousFunWithoutParty;
bx[partyIndex][currentBudget][1] = previousBudgetWithoutParty;
// can we possibly do better?
if(currentBudget - partyFee >= 0)
{
int funWithParty = bx[partyIndex - 1][currentBudget - partyFee][0] + partyFun;
int feeWithParty = bx[partyIndex - 1][currentBudget - partyFee][1] + partyFee;
if(previousFunWithoutParty < funWithParty)
{
bx[partyIndex][currentBudget][0] = funWithParty;
bx[partyIndex][currentBudget][1] = feeWithParty;
}
}
}
}
printf("%d %d\r\n", bx[vec.size()][budget][1], bx[vec.size()][budget][0]);
}
return 0;
}