-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
118 lines (98 loc) · 2.4 KB
/
main.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <bits/stdc++.h>
#include <CGAL/QP_models.h>
#include <CGAL/QP_functions.h>
#include <CGAL/Gmpz.h>
typedef long IT;
typedef CGAL::Gmpz ET;
typedef CGAL::Quadratic_program<IT> Program;
typedef CGAL::Quadratic_program_solution<ET> Solution;
void solve() {
int n, m; std::cin >> n >> m;
long s; std::cin >> s;
std::vector<std::pair<int, int>> nobles;
long nobles_x = 0, nobles_y = 0;
for (int i = 0; i < n; i++) {
int x, y; std::cin >> x >> y;
nobles.push_back({x, y});
nobles_x += x;
nobles_y += y;
}
std::vector<std::pair<int, int>> commoners;
long commoners_x = 0, commoners_y = 0;
for (int j = 0; j < m; j++) {
int x, y; std::cin >> x >> y;
commoners.push_back({x, y});
commoners_x += x;
commoners_y += y;
}
Program lp (CGAL::SMALLER, false, 0, false, 0);
int cnt = 0;
const int L = 0; lp.set_l(L, true, 0);
const int B = 1;
const int C = 2;
const int D = 3;
// Cersei
for (auto [x, y] : nobles) {
lp.set_a(B, cnt, y);
lp.set_a(C, cnt, 1);
lp.set_b(cnt, -x);
cnt++;
}
for (auto [x, y] : commoners) {
lp.set_a(B, cnt, -y);
lp.set_a(C, cnt, -1);
lp.set_b(cnt, x);
cnt++;
}
Solution sol = CGAL::solve_linear_program(lp, ET());
if (sol.is_infeasible()) {
std::cout << "Yuck!" << std::endl;
return;
}
// Tywin
if (s != -1) {
lp.set_a(B, cnt, commoners_y - nobles_y);
lp.set_a(C, cnt, m - n);
lp.set_b(cnt, s - (commoners_x - nobles_x));
cnt++;
sol = CGAL::solve_linear_program(lp, ET());
if (sol.is_infeasible()) {
std::cout << "Bankrupt!" << std::endl;
return;
}
}
// Jamie
for (auto [x, y] : nobles) {
lp.set_a(L, cnt, -1);
lp.set_a(B, cnt, -x);
lp.set_a(D, cnt, -1);
lp.set_b(cnt, -y);
cnt++;
lp.set_a(L, cnt, -1);
lp.set_a(B, cnt, x);
lp.set_a(D, cnt, 1);
lp.set_b(cnt, y);
cnt++;
}
for (auto [x, y] : commoners) {
lp.set_a(L, cnt, -1);
lp.set_a(B, cnt, -x);
lp.set_a(D, cnt, -1);
lp.set_b(cnt, -y);
cnt++;
lp.set_a(L, cnt, -1);
lp.set_a(B, cnt, x);
lp.set_a(D, cnt, 1);
lp.set_b(cnt, y);
cnt++;
}
lp.set_c(L, 1);
sol = CGAL::solve_linear_program(lp, ET());
long result = std::ceil(CGAL::to_double(sol.objective_value()));
std::cout << result << std::endl;
}
int main() {
std::ios_base::sync_with_stdio(false);
int t; std::cin >> t;
while (t--) solve();
}