-
Notifications
You must be signed in to change notification settings - Fork 4
/
light-at-the-museum.cpp
104 lines (91 loc) · 2.05 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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
struct mySum {
int time;
vector<int> sum;
bool operator< (const mySum &mySum_inst) const {
return (sum < mySum_inst.sum) || ((sum == mySum_inst.sum) && (time < mySum_inst.time));
}
};
void museum() {
int nS, nR;
cin >> nS >> nR;
vector<int> target(nR);
for (int i = 0; i < nR; i++) {
cin >> target[i];
}
vector< vector<int> > control(nS, vector<int>(nR));
for (int i = 0; i < nS; i++) {
for (int j = 0; j < nR; j++) {
int a, b;
cin >> a >> b;
target[j] -= a;
control[i][j] = b - a;
}
}
int leftNum = nS / 2;
int rightNum = nS - leftNum;
vector<mySum> leftSum((1 << leftNum));
vector<mySum> rightSum((1 << rightNum));
for (int s = 0; s < (1 << leftNum); s++) {
leftSum[s].time = 0;
leftSum[s].sum.clear();
leftSum[s].sum.resize(nR, 0);
for (int i = 0; i < leftNum; i++) {
if (s & (1 << i)) {
leftSum[s].time++;
for (int j = 0; j < nR; j++) {
leftSum[s].sum[j] += control[i][j];
}
}
}
}
for (int s = 0; s < (1 << rightNum); s++) {
rightSum[s].time = 0;
rightSum[s].sum.clear();
rightSum[s].sum.resize(nR, 0);
for (int i = 0; i < rightNum; i++) {
if (s & (1 << i)) {
rightSum[s].time++;
for (int j = 0; j < nR; j++) {
rightSum[s].sum[j] += control[i + leftNum][j];
}
}
}
}
sort(leftSum.begin(), leftSum.end());
sort(rightSum.begin(), rightSum.end());
int i = 0, j = (1 << rightNum) - 1, minTime = nS + 1;
vector<int> result;
while (i < (1 << leftNum) && j >= 0) {
vector<int> tempSum(nR, 0);
for (int k = 0; k < nR; k++) {
tempSum[k] = leftSum[i].sum[k] + rightSum[j].sum[k];
}
if (tempSum < target) {
i++;
} else if (tempSum > target) {
j--;
} else {
minTime = min(minTime, leftSum[i].time + rightSum[j].time);
j--;
}
}
if (minTime > nS) {
cout << "impossible" << endl;
} else {
cout << minTime << endl;
}
}
int main(void) {
std::ios_base::sync_with_stdio(false);
int T;
cin >> T;
for (int t = 0; t < T; t++) {
museum();
}
return 0;
}