-
Notifications
You must be signed in to change notification settings - Fork 3
/
J.cpp
104 lines (88 loc) · 1.99 KB
/
J.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 <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
using ull = unsigned long long;
using ll = long long;
using namespace std;
using SeqHash = pair<ull, int>;
const int maxn = 5650, mod = 998244353, mul = 31;
int n, m, a[12];
int f[12][maxn];
int seqN;
inline SeqHash GetHash(vector<int> &seq) {
sort(seq.begin(), seq.end(), greater<int>());
while (seq.back() == 0) seq.pop_back();
SeqHash ret;
for (int val: seq) {
ret.first = ret.first * mul + val;
ret.second = ((ll)ret.second * mul + val) % mod;
}
return ret;
}
inline int GetSeqFromInt(vector<int> &seq) {
static map<SeqHash, int> seqToInt;
auto hashVal = GetHash(seq);
auto it = seqToInt.find(hashVal);
if (it == seqToInt.end()) {
seqToInt[hashVal] = ++seqN;
return seqN;
}
return it->second;
}
int dp(int day, vector<int> S);
int helper(int day, int burger, vector<int> &S, vector<int> &add, int maxAdd = n) {
if (burger < 0) {
return 0;
}
if (S.empty()) {
return burger == 0 ? dp(day + 1, add) : 0;
}
int v = S.back();
S.pop_back();
int lim = min(maxAdd, v);
bool keepMax = S.empty() ? false: S.back() == v;
int ret = 0;
for (int i = 0; i <= lim; i++) {
add.push_back(i);
add.push_back(v - i);
ret = max(ret, helper(day, burger - i, S, add, keepMax ? i : n));
add.pop_back();
add.pop_back();
if (ret == n) break;
}
S.push_back(v);
return ret;
}
int dp(int day, vector<int> S) {
int state = GetSeqFromInt(S);
int &ret = f[day][state];
if (ret != -1) {
return ret;
}
ret = 0;
if (day > m) {
for (int i: S) {
ret += i == 1;
}
return ret;
}
vector<int> nS;
ret = helper(day, a[day], S, nS);
return ret;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> a[i];
a[i] = min(a[i], n - a[i]);
}
sort(a + 1, a + 1 + m, greater<int>());
vector<int> init {n};
memset(f, -1, sizeof(f));
cout << dp(1, init);
return 0;
}