QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#411388#3170. Lunchtime Name Recallucup-team1716TL 6925ms71064kbC++143.7kb2024-05-15 12:36:112024-05-15 12:36:12

Judging History

你现在查看的是最新测评结果

  • [2024-05-15 12:36:12]
  • 评测
  • 测评结果:TL
  • 用时:6925ms
  • 内存:71064kb
  • [2024-05-15 12:36:11]
  • 提交

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], num1[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;
	for (int i = 0; i < (int)v.size(); ++i) {
		num1[cnt] += (v[i] == 1);
	}
	
	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) {
					if (num1[states_pairs[k].fi] + num1[states_pairs[k].se] == n) {
						cout << n << endl;
						return 0;
					}
					
					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) {
				ans = max(ans, num1[states_pairs[k].fi] + num1[states_pairs[k].se]);
			}
		}
	}
	
	cout << ans << endl;
	
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 10616kb

input:

4 2
2
2

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 0ms
memory: 19112kb

input:

16 3
6
8
8

output:

5

result:

ok single line: '5'

Test #3:

score: 0
Accepted
time: 0ms
memory: 10408kb

input:

5 2
2
2

output:

3

result:

ok single line: '3'

Test #4:

score: 0
Accepted
time: 0ms
memory: 21132kb

input:

16 3
8
8
8

output:

6

result:

ok single line: '6'

Test #5:

score: 0
Accepted
time: 11ms
memory: 32136kb

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: 14512kb

input:

7 3
3
2
1

output:

4

result:

ok single line: '4'

Test #7:

score: 0
Accepted
time: 0ms
memory: 16784kb

input:

9 4
1
1
3
3

output:

5

result:

ok single line: '5'

Test #8:

score: 0
Accepted
time: 2ms
memory: 15932kb

input:

10 3
4
4
3

output:

6

result:

ok single line: '6'

Test #9:

score: 0
Accepted
time: 0ms
memory: 18288kb

input:

10 3
4
4
5

output:

6

result:

ok single line: '6'

Test #10:

score: 0
Accepted
time: 3ms
memory: 16532kb

input:

10 3
4
4
4

output:

7

result:

ok single line: '7'

Test #11:

score: 0
Accepted
time: 0ms
memory: 20488kb

input:

12 3
6
6
6

output:

6

result:

ok single line: '6'

Test #12:

score: 0
Accepted
time: 3ms
memory: 18880kb

input:

10 5
2
2
2
2
2

output:

7

result:

ok single line: '7'

Test #13:

score: 0
Accepted
time: 0ms
memory: 24200kb

input:

10 6
2
2
2
2
2
2

output:

10

result:

ok single line: '10'

Test #14:

score: 0
Accepted
time: 2ms
memory: 12408kb

input:

10 2
3
3

output:

2

result:

ok single line: '2'

Test #15:

score: 0
Accepted
time: 0ms
memory: 22724kb

input:

10 6
8
5
2
8
8
1

output:

10

result:

ok single line: '10'

Test #16:

score: 0
Accepted
time: 2ms
memory: 14424kb

input:

7 4
5
5
1
2

output:

5

result:

ok single line: '5'

Test #17:

score: 0
Accepted
time: 1ms
memory: 10296kb

input:

2 1
1

output:

2

result:

ok single line: '2'

Test #18:

score: 0
Accepted
time: 1ms
memory: 12008kb

input:

3 1
1

output:

1

result:

ok single line: '1'

Test #19:

score: 0
Accepted
time: 66ms
memory: 38816kb

input:

30 1
15

output:

0

result:

ok single line: '0'

Test #20:

score: 0
Accepted
time: 2ms
memory: 12432kb

input:

3 5
1
1
1
1
1

output:

3

result:

ok single line: '3'

Test #21:

score: 0
Accepted
time: 0ms
memory: 14452kb

input:

5 6
2
2
2
2
2
2

output:

5

result:

ok single line: '5'

Test #22:

score: 0
Accepted
time: 4171ms
memory: 56928kb

input:

30 5
15
15
15
15
13

output:

28

result:

ok single line: '28'

Test #23:

score: 0
Accepted
time: 4566ms
memory: 55616kb

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: 4108ms
memory: 65228kb

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: 5497ms
memory: 59180kb

input:

30 7
9
9
9
9
9
9
2

output:

28

result:

ok single line: '28'

Test #26:

score: 0
Accepted
time: 6822ms
memory: 70028kb

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: 6925ms
memory: 71064kb

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

output:


result: