QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#106814 | #3170. Lunchtime Name Recall | skittles1412 | AC ✓ | 1147ms | 167396kb | C++17 | 2.8kb | 2023-05-19 12:41:50 | 2023-05-19 12:41:50 |
Judging History
answer
#include "bits/extc++.h"
using namespace std;
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t;
((cerr << " | " << u), ...);
cerr << endl;
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr \
if (false) \
cerr
#endif
using u8 = uint8_t;
using u64 = uint64_t;
#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))
template <typename Cb>
struct KeyCmp {
Cb cb;
KeyCmp(Cb cb) : cb(cb) {}
template <typename T>
bool operator()(const T& a, const T& b) const {
return cb(a) < cb(b);
}
};
struct Hasher {
u64 x;
Hasher& operator()(u64 v) {
x ^= x >> 29;
x ^= x << 37;
x ^= x >> 17;
x ^= x << 23;
x ^= v;
return *this;
}
};
constexpr int maxn = 15, maxm = 35;
int n, m, arr[maxn];
u64 hashes[maxm];
__gnu_pbds::gp_hash_table<u64, int> memo[maxn][maxm];
template <typename T>
u64 hashv(const T& arr) {
u64 ans = 0;
for (auto& a : arr) {
ans += hashes[a];
}
return ans;
}
int dp(int ind, int left, basic_string<u8> gen, basic_string<u8> comps) {
if (ind == n) {
int ans = int(count(begin(comps), end(comps), 1));
if (ans == m) {
cout << ans << endl;
exit(0);
}
return ans;
} else if (!sz(comps)) {
if (left) {
return 0;
}
return dp(ind + 1, arr[ind + 1], {}, gen);
}
u64 key = Hasher()(hashv(gen))(hashv(comps)).x;
auto it = memo[ind][left].find(key);
if (it != memo[ind][left].end()) {
return it->second;
}
int ans = 0, cx = comps.back();
comps.pop_back();
for (int i = 0; i <= min(cx, left); i++) {
auto ngen = gen;
auto push = [&](int u) -> void {
if (u) {
ngen.push_back(u);
}
};
push(i);
push(cx - i);
ans = max(ans, dp(ind, left - i, ngen, comps));
}
return memo[ind][left][key] = ans;
}
void solve() {
mt19937_64 cowng(chrono::steady_clock::now().time_since_epoch().count());
for (auto& a : hashes) {
a = cowng();
}
cin >> m >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr, arr + n, KeyCmp([&](int x) -> int {
return min(x, m - x);
}));
// shuffle(arr, arr + n, cowng);
cout << dp(0, arr[0], {}, {u8(m)}) << endl;
}
int main() {
cin.tie(nullptr);
cin.exceptions(ios::failbit);
ios_base::sync_with_stdio(false);
solve();
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 3668kb
input:
4 2 2 2
output:
4
result:
ok single line: '4'
Test #2:
score: 0
Accepted
time: 2ms
memory: 3732kb
input:
16 3 6 8 8
output:
5
result:
ok single line: '5'
Test #3:
score: 0
Accepted
time: 2ms
memory: 3700kb
input:
5 2 2 2
output:
3
result:
ok single line: '3'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3680kb
input:
16 3 8 8 8
output:
6
result:
ok single line: '6'
Test #5:
score: 0
Accepted
time: 5ms
memory: 4372kb
input:
20 7 1 1 1 1 6 8 8
output:
11
result:
ok single line: '11'
Test #6:
score: 0
Accepted
time: 2ms
memory: 3668kb
input:
7 3 3 2 1
output:
4
result:
ok single line: '4'
Test #7:
score: 0
Accepted
time: 2ms
memory: 3680kb
input:
9 4 1 1 3 3
output:
5
result:
ok single line: '5'
Test #8:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
10 3 4 4 3
output:
6
result:
ok single line: '6'
Test #9:
score: 0
Accepted
time: 2ms
memory: 3660kb
input:
10 3 4 4 5
output:
6
result:
ok single line: '6'
Test #10:
score: 0
Accepted
time: 2ms
memory: 3648kb
input:
10 3 4 4 4
output:
7
result:
ok single line: '7'
Test #11:
score: 0
Accepted
time: 2ms
memory: 3672kb
input:
12 3 6 6 6
output:
6
result:
ok single line: '6'
Test #12:
score: 0
Accepted
time: 2ms
memory: 3648kb
input:
10 5 2 2 2 2 2
output:
7
result:
ok single line: '7'
Test #13:
score: 0
Accepted
time: 2ms
memory: 3704kb
input:
10 6 2 2 2 2 2 2
output:
10
result:
ok single line: '10'
Test #14:
score: 0
Accepted
time: 1ms
memory: 3704kb
input:
10 2 3 3
output:
2
result:
ok single line: '2'
Test #15:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
10 6 8 5 2 8 8 1
output:
10
result:
ok single line: '10'
Test #16:
score: 0
Accepted
time: 0ms
memory: 3676kb
input:
7 4 5 5 1 2
output:
5
result:
ok single line: '5'
Test #17:
score: 0
Accepted
time: 2ms
memory: 3636kb
input:
2 1 1
output:
2
result:
ok single line: '2'
Test #18:
score: 0
Accepted
time: 2ms
memory: 3664kb
input:
3 1 1
output:
1
result:
ok single line: '1'
Test #19:
score: 0
Accepted
time: 2ms
memory: 3704kb
input:
30 1 15
output:
0
result:
ok single line: '0'
Test #20:
score: 0
Accepted
time: 1ms
memory: 3596kb
input:
3 5 1 1 1 1 1
output:
3
result:
ok single line: '3'
Test #21:
score: 0
Accepted
time: 2ms
memory: 3676kb
input:
5 6 2 2 2 2 2 2
output:
5
result:
ok single line: '5'
Test #22:
score: 0
Accepted
time: 666ms
memory: 118672kb
input:
30 5 15 15 15 15 13
output:
28
result:
ok single line: '28'
Test #23:
score: 0
Accepted
time: 288ms
memory: 62808kb
input:
30 10 15 15 15 15 15 1 1 1 1 1
output:
30
result:
ok single line: '30'
Test #24:
score: 0
Accepted
time: 810ms
memory: 129448kb
input:
30 10 15 10 10 10 10 1 1 1 1 1
output:
28
result:
ok single line: '28'
Test #25:
score: 0
Accepted
time: 716ms
memory: 144404kb
input:
30 7 9 9 9 9 9 9 2
output:
28
result:
ok single line: '28'
Test #26:
score: 0
Accepted
time: 898ms
memory: 157816kb
input:
30 10 2 2 2 3 3 3 6 11 14 15
output:
28
result:
ok single line: '28'
Test #27:
score: 0
Accepted
time: 415ms
memory: 78068kb
input:
30 10 1 2 3 4 5 6 7 8 9 10
output:
30
result:
ok single line: '30'
Test #28:
score: 0
Accepted
time: 358ms
memory: 83476kb
input:
30 10 11 12 13 14 15 16 17 18 19 20
output:
30
result:
ok single line: '30'
Test #29:
score: 0
Accepted
time: 1147ms
memory: 167396kb
input:
30 10 20 21 22 23 24 25 26 27 28 29
output:
30
result:
ok single line: '30'
Test #30:
score: 0
Accepted
time: 372ms
memory: 89580kb
input:
30 10 4 4 4 5 5 5 5 5 5 5
output:
28
result:
ok single line: '28'
Test #31:
score: 0
Accepted
time: 649ms
memory: 113592kb
input:
30 10 2 2 2 3 3 3 8 8 9 11
output:
28
result:
ok single line: '28'
Test #32:
score: 0
Accepted
time: 661ms
memory: 110776kb
input:
30 10 2 2 2 3 3 3 9 9 9 9
output:
28
result:
ok single line: '28'
Test #33:
score: 0
Accepted
time: 855ms
memory: 135764kb
input:
30 10 12 12 12 12 3 2 2 2 2 2
output:
28
result:
ok single line: '28'
Test #34:
score: 0
Accepted
time: 633ms
memory: 109284kb
input:
30 10 10 9 9 9 3 3 2 2 2 2
output:
28
result:
ok single line: '28'
Test #35:
score: 0
Accepted
time: 690ms
memory: 118892kb
input:
30 10 10 9 9 8 5 2 2 2 2 2
output:
28
result:
ok single line: '28'
Test #36:
score: 0
Accepted
time: 627ms
memory: 133056kb
input:
30 10 9 9 9 8 8 3 2 2 1 1
output:
28
result:
ok single line: '28'
Test #37:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
30 10 1 1 1 1 1 1 1 1 1 1
output:
10
result:
ok single line: '10'
Test #38:
score: 0
Accepted
time: 0ms
memory: 3988kb
input:
30 10 2 2 2 2 2 2 2 2 2 2
output:
15
result:
ok single line: '15'
Test #39:
score: 0
Accepted
time: 11ms
memory: 8304kb
input:
30 10 3 3 3 3 3 3 3 3 3 3
output:
20
result:
ok single line: '20'
Test #40:
score: 0
Accepted
time: 122ms
memory: 34396kb
input:
30 10 4 4 4 4 4 4 4 4 4 4
output:
25
result:
ok single line: '25'
Test #41:
score: 0
Accepted
time: 338ms
memory: 79580kb
input:
30 10 5 5 5 5 5 5 5 5 5 5
output:
30
result:
ok single line: '30'
Test #42:
score: 0
Accepted
time: 2ms
memory: 3620kb
input:
30 3 2 2 2
output:
4
result:
ok single line: '4'
Test #43:
score: 0
Accepted
time: 2ms
memory: 3724kb
input:
30 6 2 2 2 2 2 2
output:
9
result:
ok single line: '9'
Test #44:
score: 0
Accepted
time: 3ms
memory: 3804kb
input:
30 9 2 2 2 2 2 2 2 2 2
output:
13
result:
ok single line: '13'
Test #45:
score: 0
Accepted
time: 398ms
memory: 86948kb
input:
30 10 2 3 4 5 5 5 5 5 6 6
output:
28
result:
ok single line: '28'
Test #46:
score: 0
Accepted
time: 408ms
memory: 84128kb
input:
30 10 2 4 5 5 5 5 5 5 5 6
output:
28
result:
ok single line: '28'
Test #47:
score: 0
Accepted
time: 316ms
memory: 59456kb
input:
30 10 15 15 15 15 29 29 29 29 29 29
output:
20
result:
ok single line: '20'
Test #48:
score: 0
Accepted
time: 500ms
memory: 89108kb
input:
30 5 15 15 15 15 15
output:
30
result:
ok single line: '30'
Test #49:
score: 0
Accepted
time: 501ms
memory: 119488kb
input:
30 10 3 6 6 7 4 3 5 3 6 4
output:
28
result:
ok single line: '28'
Test #50:
score: 0
Accepted
time: 505ms
memory: 110076kb
input:
30 10 5 2 6 7 4 7 3 4 2 6
output:
28
result:
ok single line: '28'
Test #51:
score: 0
Accepted
time: 136ms
memory: 33508kb
input:
30 10 20 6 20 19 11 18 21 24 18 12
output:
30
result:
ok single line: '30'
Test #52:
score: 0
Accepted
time: 352ms
memory: 79652kb
input:
30 10 7 22 8 12 12 23 10 11 24 21
output:
30
result:
ok single line: '30'
Test #53:
score: 0
Accepted
time: 265ms
memory: 58028kb
input:
30 10 19 21 23 17 8 21 15 20 6 22
output:
30
result:
ok single line: '30'
Test #54:
score: 0
Accepted
time: 295ms
memory: 70180kb
input:
30 10 16 5 19 16 10 14 10 12 14 4
output:
30
result:
ok single line: '30'
Test #55:
score: 0
Accepted
time: 228ms
memory: 52772kb
input:
30 10 5 9 7 10 6 21 15 24 17 21
output:
30
result:
ok single line: '30'
Test #56:
score: 0
Accepted
time: 282ms
memory: 76048kb
input:
30 10 2 4 7 3 2 6 7 2 5 2
output:
25
result:
ok single line: '25'
Test #57:
score: 0
Accepted
time: 534ms
memory: 99616kb
input:
30 10 8 6 4 2 2 3 4 8 6 5
output:
30
result:
ok single line: '30'
Test #58:
score: 0
Accepted
time: 509ms
memory: 110268kb
input:
30 10 6 4 6 4 5 2 3 3 5 8
output:
28
result:
ok single line: '28'
Test #59:
score: 0
Accepted
time: 554ms
memory: 103784kb
input:
30 10 4 12 8 13 8 1 1 1 1 1
output:
24
result:
ok single line: '24'
Test #60:
score: 0
Accepted
time: 685ms
memory: 126932kb
input:
30 10 7 13 6 9 14 1 1 1 1 1
output:
25
result:
ok single line: '25'
Test #61:
score: 0
Accepted
time: 666ms
memory: 120964kb
input:
30 10 5 11 8 14 9 1 1 1 1 1
output:
25
result:
ok single line: '25'
Test #62:
score: 0
Accepted
time: 290ms
memory: 51456kb
input:
30 10 4 12 5 12 1 3 1 2 1 3
output:
22
result:
ok single line: '22'
Test #63:
score: 0
Accepted
time: 376ms
memory: 62832kb
input:
30 10 6 13 12 10 1 2 1 2 1 1
output:
21
result:
ok single line: '21'
Test #64:
score: 0
Accepted
time: 434ms
memory: 85816kb
input:
30 10 7 5 14 5 1 3 2 3 2 2
output:
24
result:
ok single line: '24'
Test #65:
score: 0
Accepted
time: 706ms
memory: 115560kb
input:
30 5 12 14 15 12 15
output:
27
result:
ok single line: '27'
Test #66:
score: 0
Accepted
time: 772ms
memory: 131144kb
input:
30 5 14 13 15 11 11
output:
26
result:
ok single line: '26'
Test #67:
score: 0
Accepted
time: 680ms
memory: 111816kb
input:
30 5 13 12 14 11 12
output:
26
result:
ok single line: '26'
Test #68:
score: 0
Accepted
time: 862ms
memory: 142336kb
input:
30 6 11 10 9 9 10 9
output:
28
result:
ok single line: '28'
Test #69:
score: 0
Accepted
time: 800ms
memory: 141472kb
input:
30 6 10 7 8 11 9 9
output:
27
result:
ok single line: '27'
Test #70:
score: 0
Accepted
time: 766ms
memory: 127048kb
input:
30 6 7 9 8 10 10 8
output:
26
result:
ok single line: '26'
Test #71:
score: 0
Accepted
time: 821ms
memory: 140856kb
input:
30 7 6 10 7 9 6 8 9
output:
30
result:
ok single line: '30'
Test #72:
score: 0
Accepted
time: 793ms
memory: 141184kb
input:
30 7 7 6 7 10 7 7 10
output:
30
result:
ok single line: '30'
Test #73:
score: 0
Accepted
time: 677ms
memory: 126064kb
input:
30 7 7 8 6 8 8 7 6
output:
28
result:
ok single line: '28'
Test #74:
score: 0
Accepted
time: 572ms
memory: 107824kb
input:
30 10 2 2 4 5 8 5 4 11 3 3
output:
28
result:
ok single line: '28'
Test #75:
score: 0
Accepted
time: 530ms
memory: 101612kb
input:
30 9 5 2 3 3 8 10 6 3 6
output:
27
result:
ok single line: '27'
Test #76:
score: 0
Accepted
time: 211ms
memory: 42516kb
input:
30 9 5 2 3 3 8 8 4 3 3
output:
24
result:
ok single line: '24'
Test #77:
score: 0
Accepted
time: 517ms
memory: 112740kb
input:
30 10 3 2 6 4 1 8 4 8 4 6
output:
28
result:
ok single line: '28'
Test #78:
score: 0
Accepted
time: 483ms
memory: 98436kb
input:
30 9 3 6 6 11 2 7 4 2 6
output:
27
result:
ok single line: '27'
Test #79:
score: 0
Accepted
time: 535ms
memory: 114324kb
input:
30 10 3 8 6 5 2 7 4 2 2 7
output:
28
result:
ok single line: '28'
Test #80:
score: 0
Accepted
time: 579ms
memory: 115276kb
input:
30 9 3 6 4 2 7 8 5 8 4
output:
28
result:
ok single line: '28'
Test #81:
score: 0
Accepted
time: 525ms
memory: 109736kb
input:
30 9 8 7 6 4 7 2 5 6 2
output:
28
result:
ok single line: '28'
Test #82:
score: 0
Accepted
time: 574ms
memory: 112648kb
input:
30 10 3 5 2 7 7 1 6 2 6 7
output:
28
result:
ok single line: '28'
Test #83:
score: 0
Accepted
time: 379ms
memory: 63300kb
input:
30 9 2 3 2 3 4 8 15 3 5
output:
24
result:
ok single line: '24'