QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#411373 | #3170. Lunchtime Name Recall | ucup-team1716 | TL | 6975ms | 70284kb | C++14 | 3.7kb | 2024-05-15 12:18:46 | 2024-05-15 12:18:47 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mk make_pair
const int MAXS = 28628;
const int MAXPairs = 300000;
int n, m;
int cnt;
int num[35], num2[35];
map<vector<int>, int> mp;
vector<int> vecs[MAXS + 5];
int sum[MAXS + 5];
vector<int> state[35], state2[35];
void dfs(vector<int> v, int cursum) {
assert(v.size());
assert(!mp[v]);
mp[v] = ++cnt;
vecs[cnt] = v;
sum[cnt] = cursum;
state[cursum].push_back(cnt);
if (v.size() % 2 == 0)
state2[cursum].push_back(cnt);
for (int i = v.back(); cursum + i <= n; ++i) {
v.push_back(i);
dfs(v, cursum + i);
v.pop_back();
}
}
int a[15];
int cnt_pairs;
pair<int, int> states_pairs[MAXPairs + 5];
map<pair<int, int>, int> mp_pairs;
bool dp[31][16][MAXPairs + 5];
void print(vector<int> v) {
cout << "[";
for (int i = 0; i < (int)v.size(); ++i) {
cout << v[i] << ", ";
}
cout << "]" << endl;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
cin >> a[i];
a[i] = min(a[i], n - a[i]); // to reduce constant
}
// n = 30;
for (int i = 1; i <= n; ++i) {
vector<int> v;
v.push_back(i);
dfs(v, i);
}
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < (int)state[i].size(); ++j) {
for (int k = 0; k < (int)state2[n - i].size(); ++k) {
states_pairs[++cnt_pairs] = mk(state[i][j], state2[n - i][k]);
mp_pairs[mk(state[i][j], state2[n - i][k])] = cnt_pairs;
}
}
}
for (int j = 0; j < (int)state[n].size(); ++j) {
states_pairs[++cnt_pairs] = mk(state[n][j], 0); // do not split anything
mp_pairs[mk(state[n][j], 0)] = cnt_pairs;
}
for (int k = 0; k < (int)state2[n].size(); ++k) {
states_pairs[++cnt_pairs] = mk(0, state2[n][k]); // split everything (do not leave anything)
mp_pairs[mk(0, state2[n][k])] = cnt_pairs;
}
// when n = 30, cnt_pairs is 294652; roughly 3*10^5
vector<int> v;
v.push_back(n);
// cout << mp[v] << endl; // 28628, when n = 30
dp[0][0][mp_pairs[mk(mp[v], 0)]] = 1;
for (int i = 1; i <= m; ++i) {
for (int j = 0; j <= a[i - 1]; ++j) {
for (int k = 1; k <= cnt_pairs; ++k) {
if (dp[i - 1][j][k] && sum[states_pairs[k].fi] >= j) {
vector<int> v1 = vecs[states_pairs[k].fi], v2 = vecs[states_pairs[k].se];
for (int t = 0; t < (int)v2.size(); ++t) {
v1.push_back(v2[t]);
}
sort(v1.begin(), v1.end());
assert(mp[v1] != 0);
dp[i][a[i]][mp_pairs[mk(mp[v1], 0)]] = 1;
}
}
}
for (int j = a[i]; j >= 1; --j) { // number of 1s remaining
for (int k = 1; k <= cnt_pairs; ++k) { // current state: (unsplitted, splitted)
if (dp[i][j][k]) {
vector<int> v1 = vecs[states_pairs[k].fi], v2 = vecs[states_pairs[k].se];
for (int l = 0; l < (int)v1.size(); ++l) { // the element to be splitted
for (int x = 1; x < v1[l] && x <= j; ++x) { // use x 1s to split that element
vector<int> nv1, nv2 = v2;
for (int t = 0; t < (int)v1.size(); ++t)
if (t != l)
nv1.push_back(v1[t]);
nv2.push_back(x);
nv2.push_back(v1[l] - x);
sort(nv2.begin(), nv2.end());
// assert(mp[nv2] != 0);
dp[i][j - x][mp_pairs[mk(mp[nv1], mp[nv2])]] = 1;
}
}
}
}
}
}
int ans = 0;
for (int j = 0; j <= a[m]; ++j) {
for (int k = 1; k <= cnt_pairs; ++k) {
if (dp[m][j][k] && sum[states_pairs[k].fi] >= j) {
vector<int> v1 = vecs[states_pairs[k].fi], v2 = vecs[states_pairs[k].se];
int num1s = 0;
for (int k = 0; k < (int)v1.size(); ++k)
num1s += (v1[k] == 1);
for (int k = 0; k < (int)v2.size(); ++k)
num1s += (v2[k] == 1);
ans = max(ans, num1s);
}
}
}
cout << ans << endl;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 12244kb
input:
4 2 2 2
output:
4
result:
ok single line: '4'
Test #2:
score: 0
Accepted
time: 4ms
memory: 18708kb
input:
16 3 6 8 8
output:
5
result:
ok single line: '5'
Test #3:
score: 0
Accepted
time: 1ms
memory: 12492kb
input:
5 2 2 2
output:
3
result:
ok single line: '3'
Test #4:
score: 0
Accepted
time: 3ms
memory: 20420kb
input:
16 3 8 8 8
output:
6
result:
ok single line: '6'
Test #5:
score: 0
Accepted
time: 14ms
memory: 31948kb
input:
20 7 1 1 1 1 6 8 8
output:
11
result:
ok single line: '11'
Test #6:
score: 0
Accepted
time: 0ms
memory: 14716kb
input:
7 3 3 2 1
output:
4
result:
ok single line: '4'
Test #7:
score: 0
Accepted
time: 0ms
memory: 14496kb
input:
9 4 1 1 3 3
output:
5
result:
ok single line: '5'
Test #8:
score: 0
Accepted
time: 2ms
memory: 14656kb
input:
10 3 4 4 3
output:
6
result:
ok single line: '6'
Test #9:
score: 0
Accepted
time: 2ms
memory: 16620kb
input:
10 3 4 4 5
output:
6
result:
ok single line: '6'
Test #10:
score: 0
Accepted
time: 0ms
memory: 16624kb
input:
10 3 4 4 4
output:
7
result:
ok single line: '7'
Test #11:
score: 0
Accepted
time: 0ms
memory: 18908kb
input:
12 3 6 6 6
output:
6
result:
ok single line: '6'
Test #12:
score: 0
Accepted
time: 0ms
memory: 20692kb
input:
10 5 2 2 2 2 2
output:
7
result:
ok single line: '7'
Test #13:
score: 0
Accepted
time: 0ms
memory: 22676kb
input:
10 6 2 2 2 2 2 2
output:
10
result:
ok single line: '10'
Test #14:
score: 0
Accepted
time: 2ms
memory: 12724kb
input:
10 2 3 3
output:
2
result:
ok single line: '2'
Test #15:
score: 0
Accepted
time: 2ms
memory: 24812kb
input:
10 6 8 5 2 8 8 1
output:
10
result:
ok single line: '10'
Test #16:
score: 0
Accepted
time: 0ms
memory: 16556kb
input:
7 4 5 5 1 2
output:
5
result:
ok single line: '5'
Test #17:
score: 0
Accepted
time: 1ms
memory: 8308kb
input:
2 1 1
output:
2
result:
ok single line: '2'
Test #18:
score: 0
Accepted
time: 1ms
memory: 10368kb
input:
3 1 1
output:
1
result:
ok single line: '1'
Test #19:
score: 0
Accepted
time: 73ms
memory: 36560kb
input:
30 1 15
output:
0
result:
ok single line: '0'
Test #20:
score: 0
Accepted
time: 0ms
memory: 20580kb
input:
3 5 1 1 1 1 1
output:
3
result:
ok single line: '3'
Test #21:
score: 0
Accepted
time: 0ms
memory: 22704kb
input:
5 6 2 2 2 2 2 2
output:
5
result:
ok single line: '5'
Test #22:
score: 0
Accepted
time: 4209ms
memory: 53460kb
input:
30 5 15 15 15 15 13
output:
28
result:
ok single line: '28'
Test #23:
score: 0
Accepted
time: 5488ms
memory: 67308kb
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: 4058ms
memory: 65052kb
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: 5489ms
memory: 60768kb
input:
30 7 9 9 9 9 9 9 2
output:
28
result:
ok single line: '28'
Test #26:
score: 0
Accepted
time: 6875ms
memory: 66284kb
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: 6975ms
memory: 70284kb
input:
30 10 1 2 3 4 5 6 7 8 9 10
output:
30
result:
ok single line: '30'
Test #28:
score: -100
Time Limit Exceeded
input:
30 10 11 12 13 14 15 16 17 18 19 20