QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#106813 | #3170. Lunchtime Name Recall | skittles1412 | ML | 917ms | 157880kb | C++17 | 2.5kb | 2023-05-19 12:39:37 | 2023-05-19 12:39:39 |
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))
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];
}
// 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: 2ms
memory: 3616kb
input:
4 2 2 2
output:
4
result:
ok single line: '4'
Test #2:
score: 0
Accepted
time: 2ms
memory: 3716kb
input:
16 3 6 8 8
output:
5
result:
ok single line: '5'
Test #3:
score: 0
Accepted
time: 2ms
memory: 3664kb
input:
5 2 2 2
output:
3
result:
ok single line: '3'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3692kb
input:
16 3 8 8 8
output:
6
result:
ok single line: '6'
Test #5:
score: 0
Accepted
time: 5ms
memory: 4368kb
input:
20 7 1 1 1 1 6 8 8
output:
11
result:
ok single line: '11'
Test #6:
score: 0
Accepted
time: 1ms
memory: 3700kb
input:
7 3 3 2 1
output:
4
result:
ok single line: '4'
Test #7:
score: 0
Accepted
time: 2ms
memory: 3664kb
input:
9 4 1 1 3 3
output:
5
result:
ok single line: '5'
Test #8:
score: 0
Accepted
time: 2ms
memory: 3640kb
input:
10 3 4 4 3
output:
6
result:
ok single line: '6'
Test #9:
score: 0
Accepted
time: 2ms
memory: 3644kb
input:
10 3 4 4 5
output:
6
result:
ok single line: '6'
Test #10:
score: 0
Accepted
time: 2ms
memory: 3644kb
input:
10 3 4 4 4
output:
7
result:
ok single line: '7'
Test #11:
score: 0
Accepted
time: 2ms
memory: 3712kb
input:
12 3 6 6 6
output:
6
result:
ok single line: '6'
Test #12:
score: 0
Accepted
time: 2ms
memory: 3708kb
input:
10 5 2 2 2 2 2
output:
7
result:
ok single line: '7'
Test #13:
score: 0
Accepted
time: 2ms
memory: 3736kb
input:
10 6 2 2 2 2 2 2
output:
10
result:
ok single line: '10'
Test #14:
score: 0
Accepted
time: 2ms
memory: 3664kb
input:
10 2 3 3
output:
2
result:
ok single line: '2'
Test #15:
score: 0
Accepted
time: 2ms
memory: 3620kb
input:
10 6 8 5 2 8 8 1
output:
10
result:
ok single line: '10'
Test #16:
score: 0
Accepted
time: 1ms
memory: 3712kb
input:
7 4 5 5 1 2
output:
5
result:
ok single line: '5'
Test #17:
score: 0
Accepted
time: 2ms
memory: 3592kb
input:
2 1 1
output:
2
result:
ok single line: '2'
Test #18:
score: 0
Accepted
time: 0ms
memory: 3660kb
input:
3 1 1
output:
1
result:
ok single line: '1'
Test #19:
score: 0
Accepted
time: 2ms
memory: 3640kb
input:
30 1 15
output:
0
result:
ok single line: '0'
Test #20:
score: 0
Accepted
time: 0ms
memory: 3684kb
input:
3 5 1 1 1 1 1
output:
3
result:
ok single line: '3'
Test #21:
score: 0
Accepted
time: 2ms
memory: 3668kb
input:
5 6 2 2 2 2 2 2
output:
5
result:
ok single line: '5'
Test #22:
score: 0
Accepted
time: 411ms
memory: 82820kb
input:
30 5 15 15 15 15 13
output:
28
result:
ok single line: '28'
Test #23:
score: 0
Accepted
time: 591ms
memory: 115676kb
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: 464ms
memory: 108004kb
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: 663ms
memory: 130116kb
input:
30 7 9 9 9 9 9 9 2
output:
28
result:
ok single line: '28'
Test #26:
score: 0
Accepted
time: 917ms
memory: 157880kb
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: 417ms
memory: 78196kb
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: 538ms
memory: 106708kb
input:
30 10 11 12 13 14 15 16 17 18 19 20
output:
30
result:
ok single line: '30'
Test #29:
score: -100
Memory Limit Exceeded
input:
30 10 20 21 22 23 24 25 26 27 28 29