-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathBTCK.cpp
62 lines (52 loc) · 1.11 KB
/
BTCK.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
/*
brute force > recursive backtracking > pruned permutations
difficulty: easy
date: 14/Apr/2020
by: @brpapa
*/
#include <bits/stdc++.h>
using namespace std;
bitset<10> used; // used[v] = v já foi usado na permutação atual
int p[10]; // permutação atual
int A[10], K;
bool hasSolution;
// p[0:i-1] é válido?
bool check(int i, int v) {
int sum = 0;
for (int k = 0; k < i; k++) {
sum += p[k]*A[k];
if (sum > K) return false;
}
return sum + (v*A[i]) <= K;
}
// constroi p[i]
void bt(int i) {
if (hasSolution) return;
if (i == 10) {
hasSolution = true;
for (int i = 0; i < 9; i++)
cout << p[i] << " ";
cout << p[9] << endl;
return;
}
for (int v = 0; v < 10; v++) {
if (!used[v] && check(i, v)) {
p[i] = v;
used[v] = 1;
bt(i+1);
used[v] = 0;
}
}
}
int main() {
int T; cin >> T;
while (T--) {
for (int i = 0; i < 10; i++) cin >> A[i];
cin >> K;
hasSolution = false;
used.reset();
bt(0);
if (!hasSolution) cout << -1 << endl;
}
return 0;
}