Submission #317087

# Submission time Handle Problem Language Result Execution time Memory
317087 2020-10-29T03:02:41 Z 8e7 DEL13 (info1cup18_del13) C++14
30 / 100
123 ms 1632 KB
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define maxn 200005
using namespace std;
bool dp[1<<18];
int from[1<<18], nxt[1<<18];
void build(int n) {
	dp[(1<<n) - 1] = true;
	for (int i = (1<<n) - 2;i >= 0;i--) {
		vector<int> pos;
		for (int j = 0;j < n;j++) {
			//cout << ((i & (1<<j)) ? 1 : 0);
			if ((i & (1<<j)) == 0) {
				pos.push_back(j);
			}
		}
		for (int j = 0;j < pos.size() - 1;j++) {
			int cnt = 0, ind = 0;
			for (int x = pos[j];x < n;x++) {
				cnt += ((i & (1<<x)) ? (ind = x, 1) : 0);
				if (cnt > 1) continue;
				if ((i & (1<<x)) == 0 && cnt == 1 && dp[i + (1<<(pos[j])) + (1<<x)]) {
					dp[i] = true;
					from[i] = ind;
					nxt[i] = i + (1<<(pos[j])) + (1<<x);
					break;
				}
			}
			if (dp[i]) {
				break;
			}
		}
		//cout << " " << (dp[i] ? 1 : 0) << endl;
	}
}
int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);
	int t;
	cin >> t;
	int cnt = 0;
	while (t--) {
		int n, q;
		cin >> n >> q;
		if (cnt == 0) {
			build(n);
		}
		int a = 0;
		for (int i = 0;i < q;i++) {
			int x;
			cin >> x;
			a += (1<<(x - 1));
		}
		//cout << a << endl;

		if (dp[a]) {
			vector<int> ans;
			while (a != (1<<n) - 1) {
				ans.push_back(from[a]);
				a = nxt[a];
			}
			cout << ans.size() << "\n";
			reverse(ans.begin(), ans.end());
			for (auto i:ans) cout << i + 1 << " ";
			cout << "\n";
		} else {
			cout << -1 << "\n";
		}

		cnt++;
	}
}
/*
4
7 3
1 5 7
7 3
1 2 7
 */

Compilation message

del13.cpp: In function 'void build(int)':
del13.cpp:21:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |   for (int j = 0;j < pos.size() - 1;j++) {
      |                  ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 67 ms 1144 KB Output is correct
4 Correct 123 ms 1632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 640 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Runtime error 1 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 67 ms 1144 KB Output is correct
4 Correct 123 ms 1632 KB Output is correct
5 Runtime error 1 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
6 Runtime error 1 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
7 Runtime error 1 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 67 ms 1144 KB Output is correct
4 Correct 123 ms 1632 KB Output is correct
5 Runtime error 1 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
6 Runtime error 1 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
7 Runtime error 1 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
8 Runtime error 1 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
9 Runtime error 1 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
10 Runtime error 1 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)
11 Runtime error 1 ms 512 KB Execution killed with signal 11 (could be triggered by violating memory limits)