forked from daniCh8/eth-algolab-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlight_at_the_museum.cpp
183 lines (160 loc) · 4.88 KB
/
light_at_the_museum.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Super ugly and long solution - it's a mix of different tries to solve the problem. It works, but it's super ugly. DP mixed to Split & List.
**/
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <climits>
using namespace std;
const int maxint = INT_MAX;
void brute_force(int number_of_switches, int m, int index, vector<int> lights_in_the_room, vector<vector<int>> &switches, vector<pair<vector<int>, int>> &subset) {
if(index < 0) return;
vector<int> newLights(m);
for(int i = 0; i < m; i++)
newLights[i] = lights_in_the_room[i] + switches[index][i];
pair<vector<int>, int> tmp(newLights, number_of_switches+1);
subset.push_back(tmp);
brute_force(number_of_switches, m, index-1, lights_in_the_room, switches, subset);
brute_force(number_of_switches+1, m, index-1, newLights, switches, subset);
}
bool mysort1(pair<vector<int>, int> i, pair<vector<int>, int> j) {
for(int index = 0; index < (int)(i.first.size()); index++) {
if(i.first[index] < j.first[index]) return true;
else if(i.first[index] > j.first[index]) return false;
}
return i.second < j.second;
}
bool mysort2(pair<vector<int>, int> i, pair<vector<int>, int> j) {
for(int index = 0; index < (int)(i.first.size()); index++) {
if(i.first[index] > j.first[index]) return true;
else if(i.first[index] < j.first[index]) return false;
}
return i.second < j.second;
}
int compare(vector<pair<vector<int>, int>> &subset1, vector<pair<vector<int>, int>> &subset2, vector<int> &target, int m, int found) {
int index1 = 0, index2 = 0;
while(index1 < (int)subset1.size() && index2 < (int)subset2.size()) {
bool mix = true;
for(int i = 0; i < m; i++) {
int comparison = target[i] - (subset1[index1].first)[i] - (subset2[index2].first)[i];
if(comparison > 0) {
mix = false;
i = m;
index1++;
} else if(comparison < 0) {
mix = false;
i = m;
index2++;
}
}
if(mix) {
int tmp = subset1[index1].second + subset2[index2].second;
if(tmp < found)
found = tmp;
int index1_backup = index1;
bool check1 = true;
index1++;
while(check1 && index1 < subset1.size()) {
for(int i = 0; i < m; i++) {
int comparison = target[i] - (subset1[index1].first)[i] - (subset2[index2].first)[i];
if(comparison != 0) {
check1 = false;
i = m;
}
}
if(check1) {
tmp = subset1[index1].second + subset2[index2].second;
if(tmp < found)
found = tmp;
}
index1++;
}
index1 = index1_backup;
index2++;
}
}
return found;
}
int findmin1(vector<pair<vector<int>, int>> &subset, vector<int> &target, int m) {
int index = 0;
while(index < (int)subset.size()) {
bool mix = true;
for(int i = 0; i < m; i++) {
int comparison = target[i] - (subset[index].first)[i];
if(comparison > 0) {
mix = false;
i = m;
index++;
} else if(comparison < 0) {
return maxint;
}
}
if(mix) return subset[index].second;
}
return maxint;
}
int findmin2(vector<pair<vector<int>, int>> &subset, vector<int> &target, int m) {
int index = 0;
while(index < (int)subset.size()) {
bool mix = true;
for(int i = 0; i < m; i++) {
int comparison = target[i] - (subset[index].first)[i];
if(comparison < 0) {
mix = false;
i = m;
index++;
} else if(comparison > 0) {
return maxint;
}
}
if(mix) return subset[index].second;
}
return maxint;
}
int main(int argc, char const *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int num_of_tests;
cin >> num_of_tests;
while(num_of_tests > 0) {
int n, m;
cin >> n >> m;
vector<int> target(m);
for(int i = 0; i < m; i++)
cin >> target[i];
int half = n/2, remaining = n-half;
vector<int> lights_in_the_room(m, 0);
vector<vector<int>> switches1(half, vector<int>(m, 0)), switches2(remaining, vector<int>(m, 0));
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
int tmp = 0;
for(int k = 0; k < 2; k++) {
if(k == 0) {
int first;
cin >> first;
target[j] -= first;
tmp -= first;
}
else {
int second;
cin >> second;
tmp += second;
if(i < half) switches1[i][j] = tmp;
else switches2[i-half][j] = tmp;
}
}
}
}
vector<pair<vector<int>, int>> subset1, subset2;
brute_force(0, m, half-1, lights_in_the_room, switches1, subset1);
brute_force(0, m, remaining-1, lights_in_the_room, switches2, subset2);
sort(subset1.begin(), subset1.end(), mysort1);
sort(subset2.begin(), subset2.end(), mysort2);
int result = min(compare(subset1, subset2, target, m, maxint), findmin1(subset1, target, m));
result = min(result, findmin2(subset2, target, m));
if(result == maxint) cout << "impossible\n";
else cout << result << "\n";
num_of_tests--;
}
}