QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#411397#3170. Lunchtime Name Recallucup-team1716WA 3ms40992kbC++143.7kb2024-05-15 12:43:112024-05-15 12:43:11

Judging History

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

  • [2024-05-15 12:43:11]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:40992kb
  • [2024-05-15 12:43: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 = 6e5;

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];

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);
	
	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)state[n - i].size(); ++k) {
				states_pairs[++cnt_pairs] = mk(state[i][j], state[n - i][k]);
				mp_pairs[mk(state[i][j], state[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)state[n].size(); ++k) {
		states_pairs[++cnt_pairs] = mk(0, state[n][k]); // split everything (do not leave anything)
		mp_pairs[mk(0, state[n][k])] = cnt_pairs;
	}
	
	vector<int> v;
	v.push_back(n);
	
	dp[0][0][mp_pairs[mk(mp[v], 0)]] = 1;
	
	for (int i = 1; i <= m; ++i) {
		for (int k = 1; k <= cnt_pairs; ++k) {
			if (dp[i - 1][0][k]) {
				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];
					if (v1.size()) {
						int l = v1.size() - 1;
						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;
						}
						if (j >= v1[l]) {
							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(v1[l]);
							sort(nv2.begin(), nv2.end());
							dp[i][j - v1[l]][mp_pairs[mk(mp[nv1], mp[nv2])]] = 1;
						}
					}
				}
			}
		}
	}
	
	int ans = 0;
	
	for (int k = 1; k <= cnt_pairs; ++k) {
		if (dp[m][0][k]) {
			ans = max(ans, num1[states_pairs[k].fi] + num1[states_pairs[k].se]);
		}
	}
	
	cout << ans << endl;
	
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 15928kb

input:

4 2
2
2

output:

4

result:

ok single line: '4'

Test #2:

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

input:

16 3
6
8
8

output:

5

result:

ok single line: '5'

Test #3:

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

input:

5 2
2
2

output:

3

result:

ok single line: '3'

Test #4:

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

input:

16 3
8
8
8

output:

6

result:

ok single line: '6'

Test #5:

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

input:

20 7
1
1
1
1
6
8
8

output:

11

result:

ok single line: '11'

Test #6:

score: -100
Wrong Answer
time: 0ms
memory: 18840kb

input:

7 3
3
2
1

output:

3

result:

wrong answer 1st lines differ - expected: '4', found: '3'