-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrockers.cpp
executable file
·58 lines (53 loc) · 1.29 KB
/
rockers.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
/*
ID: paulius10
PROG: rockers
LANG: C++
*/
#include <fstream>
#include <numeric>
#include <stdlib.h>
using namespace std;
bool works(int used[], int lengths[], int N, int T, int M) {
int n_full_disks = 0;
int time_left = T;
for (int i = 0; i < N; i++) {
if (!used[i]) continue;
if (lengths[i] > T) return false;
if (lengths[i] <= time_left) time_left -= lengths[i];
else {
n_full_disks++;
time_left = T - lengths[i];
}
if (n_full_disks >= M) return false;
}
return true;
}
void truncated_dfs(int nextpos, int depth, int used[], int lengths[], int N,
int T, int M) {
if (works(used, lengths, N, T, M)) {
ofstream fout("rockers.out");
fout << accumulate(used, used + N, 0) << endl;
fout.close();
exit(0);
}
if (depth == 0) return;
for (int i = nextpos; i < N; i++) {
if (used[i]) {
used[i] = 0;
truncated_dfs(i + 1, depth - 1, used, lengths, N, T, M);
used[i] = 1;
}
}
}
int main() {
ifstream fin("rockers.in");
int N, T, M;
fin >> N >> T >> M;
int lengths[N];
for (int i = 0; i < N; i++) fin >> lengths[i];
fin.close();
int used[N];
for (int i = 0; i < N; i++) used[i] = 1;
for (int depth = 0; depth <= N; depth++) truncated_dfs(0, depth, used, lengths, N, T, M);
return 0;
}