forked from Diusrex/UVA-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path103 Stacking Boxes.cpp
134 lines (104 loc) · 2.82 KB
/
103 Stacking Boxes.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
struct Box
{
Box(int numDimen)
: dimens(numDimen)
{}
int origionalPos;
vector<int> dimens;
};
const int UNKNOWN = -1;
int dimen, N;
int numberFit[35];
int bestToHaveInside[35];
bool MyIntComp(int i, int j)
{
return i > j;
}
bool MyComparison(const Box &l, const Box &r)
{
for (int i = 0; i < dimen; ++i)
if (l.dimens[i] != r.dimens[i])
return l.dimens[i] > r.dimens[i];
return l.origionalPos < r.origionalPos;
}
bool CanFit(const Box &smaller, const Box &larger)
{
for (int i = 0; i < dimen; ++i)
if (smaller.dimens[i] >= larger.dimens[i])
return false;
return true;
}
int NumberCanFit(int pos, const vector<Box> &allBoxes)
{
int &most = numberFit[pos];
if (most == UNKNOWN)
{
most = 0;
bestToHaveInside[pos] = -1;
const Box ¤t = allBoxes[pos];
for (int i = pos + 1; i < N; ++i)
{
if (CanFit(allBoxes[i], current))
{
int newVal = NumberCanFit(i, allBoxes);
if (newVal > most)
{
most = newVal;
bestToHaveInside[pos] = i;
}
}
}
++most;
}
return most;
}
int main()
{
vector<Box> allBoxes;
while (cin >> N >> dimen)
{
allBoxes.assign(N, Box(dimen));
for (int i = 0; i < N; ++i)
{
numberFit[i] = UNKNOWN;
allBoxes[i].origionalPos = i + 1;
for (int j = 0; j < dimen; ++j)
cin >> allBoxes[i].dimens[j];
sort(allBoxes[i].dimens.begin(), allBoxes[i].dimens.end(), MyIntComp);
}
sort(allBoxes.begin(), allBoxes.end(), MyComparison);
int largestScore = NumberCanFit(0, allBoxes);
int bestToStart = 0;
for (int i = 1; i < N; ++i)
{
int tempScore = NumberCanFit(i, allBoxes);
if (tempScore > largestScore)
{
largestScore = tempScore;
bestToStart = i;
}
}
cout << largestScore << '\n';
stack<int> reverseOrder;
int current = bestToStart;
while (current != -1)
{
reverseOrder.push(allBoxes[current].origionalPos);
current = bestToHaveInside[current];
}
current = reverseOrder.top();
reverseOrder.pop();
while (!reverseOrder.empty())
{
cout << current << ' ';
current = reverseOrder.top();
reverseOrder.pop();
}
cout << current << '\n';
}
}