Submission #372235

# Submission time Handle Problem Language Result Execution time Memory
372235 2021-02-27T10:01:20 Z 8e7 Swap (BOI16_swap) C++14
68 / 100
1000 ms 177184 KB
//Challenge: Accepted
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <unordered_map>
#define ll long long
#define maxn 200005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
int a[maxn], n;

void merge(vector<int> v1, vector<int> v2, vector<int> & ret) {
	for (int i = 0;(1<<(i + 1)) - 1 <= v2.size();i++) {
		for (int j = (1<<i);j < (1<<(i + 1));j++) ret.push_back(v1[j - 1]);
		for (int j = (1<<i);j < (1<<(i + 1));j++) ret.push_back(v2[j - 1]);
		if ((1<<(i + 2)) - 1 > v2.size()) {
			for (int j = (1<<(i + 1)) - 1;j < v1.size();j++) ret.push_back(v1[j]);
			for (int j = (1<<(i + 1)) - 1;j < v2.size();j++) ret.push_back(v2[j]);
			return;
		}
	}
}
unordered_map<int, vector<int> > ans[maxn];
vector<int> solve(int v, int x) {
	if (ans[v][x].size()) {
		return ans[v][x];
	}
	vector<int> ret;
	ret.push_back(x);
	bool lef = 2 * v <= n, rig = 2 * v + 1 <= n;
	if (!lef && !rig) {
		return ret;
	} else if (!rig) {
		ret.push_back(a[2 * v]);
		if (a[2 * v] < x) swap(ret[0], ret[1]);
		return ret;
	} else {
		int l = a[2 * v], r = a[2 * v + 1];
		if (x < l && x < r) {
			merge(solve(v * 2, l), solve(v * 2 + 1, r), ret);
		} else if (l < x && l < r) {
			ret[0] = l;
			merge(solve(v * 2, x), solve(v * 2 + 1, r), ret);
		} else {
			ret[0] = r;
			vector<int> v1, v2;
			merge(solve(v * 2, l), solve(v * 2 + 1, x), v1);
			merge(solve(v * 2, x), solve(v * 2 + 1, l), v2);
			for (int i = 0;i < v1.size();i++) {
				if (v1[i] < v2[i]) {
					ret.insert(ret.end(), v1.begin(), v1.end());
					break;
				} else if (v1[i] > v2[i]) {
					ret.insert(ret.end(), v2.begin(), v2.end());
					break;
				}
			}
		}
		ans[v][x] = ret;
		ans[v * 2][x].clear(), ans[v * 2 + 1][x].clear();
		return ret;
	}
}
int main() {
	io
	cin >> n;
	for (int i = 1;i <= n;i++) cin >> a[i];
	vector<int> out = solve(1, a[1]);
	for (int i:out) cout << i << " ";
	cout << "\n";
}
/*
5
3 4 2 5 1
 */

Compilation message

swap.cpp: In function 'void merge(std::vector<int>, std::vector<int>, std::vector<int>&)':
swap.cpp:17:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |  for (int i = 0;(1<<(i + 1)) - 1 <= v2.size();i++) {
      |                 ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
swap.cpp:20:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |   if ((1<<(i + 2)) - 1 > v2.size()) {
      |       ~~~~~~~~~~~~~~~~~^~~~~~~~~~~
swap.cpp:21:36: 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 = (1<<(i + 1)) - 1;j < v1.size();j++) ret.push_back(v1[j]);
      |                                  ~~^~~~~~~~~~~
swap.cpp:22:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |    for (int j = (1<<(i + 1)) - 1;j < v2.size();j++) ret.push_back(v2[j]);
      |                                  ~~^~~~~~~~~~~
swap.cpp: In function 'std::vector<int> solve(int, int)':
swap.cpp:53:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |    for (int i = 0;i < v1.size();i++) {
      |                   ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 11244 KB Output is correct
2 Correct 8 ms 11244 KB Output is correct
3 Correct 8 ms 11244 KB Output is correct
4 Correct 9 ms 11244 KB Output is correct
5 Correct 10 ms 11392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 11244 KB Output is correct
2 Correct 8 ms 11244 KB Output is correct
3 Correct 8 ms 11244 KB Output is correct
4 Correct 9 ms 11244 KB Output is correct
5 Correct 10 ms 11392 KB Output is correct
6 Correct 8 ms 11244 KB Output is correct
7 Correct 8 ms 11244 KB Output is correct
8 Correct 10 ms 11372 KB Output is correct
9 Correct 9 ms 11372 KB Output is correct
10 Correct 10 ms 11244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 11244 KB Output is correct
2 Correct 8 ms 11244 KB Output is correct
3 Correct 8 ms 11244 KB Output is correct
4 Correct 9 ms 11244 KB Output is correct
5 Correct 10 ms 11392 KB Output is correct
6 Correct 8 ms 11244 KB Output is correct
7 Correct 8 ms 11244 KB Output is correct
8 Correct 10 ms 11372 KB Output is correct
9 Correct 9 ms 11372 KB Output is correct
10 Correct 10 ms 11244 KB Output is correct
11 Correct 9 ms 11628 KB Output is correct
12 Correct 9 ms 11628 KB Output is correct
13 Correct 9 ms 11616 KB Output is correct
14 Correct 12 ms 12012 KB Output is correct
15 Correct 14 ms 12012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 11244 KB Output is correct
2 Correct 8 ms 11244 KB Output is correct
3 Correct 8 ms 11244 KB Output is correct
4 Correct 9 ms 11244 KB Output is correct
5 Correct 10 ms 11392 KB Output is correct
6 Correct 8 ms 11244 KB Output is correct
7 Correct 8 ms 11244 KB Output is correct
8 Correct 10 ms 11372 KB Output is correct
9 Correct 9 ms 11372 KB Output is correct
10 Correct 10 ms 11244 KB Output is correct
11 Correct 9 ms 11628 KB Output is correct
12 Correct 9 ms 11628 KB Output is correct
13 Correct 9 ms 11616 KB Output is correct
14 Correct 12 ms 12012 KB Output is correct
15 Correct 14 ms 12012 KB Output is correct
16 Correct 105 ms 28512 KB Output is correct
17 Correct 122 ms 28008 KB Output is correct
18 Correct 105 ms 28524 KB Output is correct
19 Correct 571 ms 82388 KB Output is correct
20 Correct 561 ms 81624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 11244 KB Output is correct
2 Correct 8 ms 11244 KB Output is correct
3 Correct 8 ms 11244 KB Output is correct
4 Correct 9 ms 11244 KB Output is correct
5 Correct 10 ms 11392 KB Output is correct
6 Correct 8 ms 11244 KB Output is correct
7 Correct 8 ms 11244 KB Output is correct
8 Correct 10 ms 11372 KB Output is correct
9 Correct 9 ms 11372 KB Output is correct
10 Correct 10 ms 11244 KB Output is correct
11 Correct 9 ms 11628 KB Output is correct
12 Correct 9 ms 11628 KB Output is correct
13 Correct 9 ms 11616 KB Output is correct
14 Correct 12 ms 12012 KB Output is correct
15 Correct 14 ms 12012 KB Output is correct
16 Correct 105 ms 28512 KB Output is correct
17 Correct 122 ms 28008 KB Output is correct
18 Correct 105 ms 28524 KB Output is correct
19 Correct 571 ms 82388 KB Output is correct
20 Correct 561 ms 81624 KB Output is correct
21 Correct 426 ms 83396 KB Output is correct
22 Correct 440 ms 85552 KB Output is correct
23 Correct 446 ms 84556 KB Output is correct
24 Execution timed out 1067 ms 177184 KB Time limit exceeded
25 Halted 0 ms 0 KB -