-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackrec.cpp
executable file
·118 lines (111 loc) · 3.83 KB
/
packrec.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
/*
ID: paulius10
PROG: packrec
LANG: C++
*/
#include <algorithm>
#include <iostream>
#include <fstream>
#include <set>
#include <vector>
using namespace std;
vector<vector<int> > deep_copy_without(vector<vector<int> > a, int i) {
vector<vector<int> > b;
for (int j = 0; j < a.size(); j++) {
if (j == i) continue;
vector<int> t;
for (vector<int>::iterator it2 = a[j].begin(); it2 != a[j].end(); it2++)
t.push_back(*it2);
b.push_back(t);
}
return b;
}
vector<vector<vector<int> > > rotations(vector<vector<int> > packages, int i = 0) {
vector<vector<vector<int> > > v;
if (i == 4) {
v.push_back(packages);
return v;
}
for (int j = 0; j < 2; j++) {
int t = packages[i][0];
packages[i][0] = packages[i][1];
packages[i][1] = t;
vector<vector<vector<int> > > results = rotations(packages, i + 1);
for (vector<vector<vector<int> > >::iterator it = results.begin(); it != results.end(); it++)
v.push_back(*it);
}
return v;
}
vector<vector<vector<int> > > permutations(vector<vector<int> > packages, vector<vector<int> > p, bool used[], int i = 0) {
vector<vector<vector<int> > > v;
if (i == 4) {
v.push_back(p);
return v;
}
for (int j = 0; j < packages.size(); j++) {
if (used[j]) continue;
p.push_back(packages[j]);
used[j] = true;
vector<vector<vector<int> > > results = permutations(packages, p, used, i + 1);
for (vector<vector<vector<int> > >::iterator it2 = results.begin(); it2 != results.end(); it2++)
v.push_back(*it2);
p.pop_back();
used[j] = false;
}
return v;
}
void update(int p, int S, int& minS, set<int>& ps) {
if (minS == -1 || S < minS) {
minS = S;
ps.clear();
ps.insert(p);
} else if (S == minS) ps.insert(p);
}
int main() {
ifstream fin("packrec.in");
ofstream fout("packrec.out");
vector<vector<int> > packages;
for (int i = 0; i < 4; i++) {
vector<int> v;
int a, b;
fin >> a >> b;
v.push_back(a);
v.push_back(b);
packages.push_back(v);
}
vector<vector<vector<int> > > states;
vector<vector<vector<int> > > temp = rotations(packages);
for (vector<vector<vector<int> > >::iterator it = temp.begin(); it != temp.end(); it++) {
vector<vector<int> > t;
bool used[] = {false, false, false, false};
vector<vector<vector<int> > > p = permutations(*it, t, used);
for (vector<vector<vector<int> > >::iterator it2 = p.begin(); it2 != p.end(); it2++)
states.push_back(*it2);
}
int minS = -1;
set<int> ps;
for (vector<vector<vector<int> > >::iterator it = states.begin(); it != states.end(); it++) {
int x = (*it)[0][0] + (*it)[1][0] + (*it)[2][0] + (*it)[3][0];
int y = max((*it)[0][1], max((*it)[1][1], max((*it)[2][1], (*it)[3][1])));
update(min(x, y), x * y, minS, ps);
x = max((*it)[0][0] + (*it)[1][0] + (*it)[2][0], (*it)[3][0]);
y = max((*it)[0][1], max((*it)[1][1], (*it)[2][1])) + (*it)[3][1];
update(min(x, y), x * y, minS, ps);
x = max((*it)[0][0] + (*it)[1][0], (*it)[3][0]) + (*it)[2][0];
y = max(max((*it)[0][1], (*it)[1][1]) + (*it)[3][1] ,(*it)[2][1]);
update(min(x, y), x * y, minS, ps);
x = (*it)[0][0] + max((*it)[1][0], (*it)[2][0]) + (*it)[3][0];
y = max((*it)[0][1], max((*it)[1][1] + (*it)[2][1], (*it)[3][1]));
update(min(x, y), x * y, minS, ps);
x = max((*it)[0][0], (*it)[1][0]) + (*it)[2][0] + (*it)[3][0];
y = max((*it)[0][1] + (*it)[1][1], max((*it)[2][1], (*it)[3][1]));
update(min(x, y), x * y, minS, ps);
x = max((*it)[0][0] + (*it)[1][0], max((*it)[0][0] + (*it)[2][0], (*it)[3][0] + (*it)[2][0]));
y = max((*it)[0][1] + (*it)[3][1], max((*it)[1][1] + (*it)[3][1], (*it)[1][1] + (*it)[2][1]));
update(min(x, y), x * y, minS, ps);
}
fout << minS << endl;
for (set<int>::iterator it = ps.begin(); it != ps.end(); it++)
fout << *it << " " << minS / (*it) << endl;
return 0;
}