Submission #372245

# Submission time Handle Problem Language Result Execution time Memory
372245 2021-02-27T10:09:38 Z 8e7 Swap (BOI16_swap) C++14
68 / 100
491 ms 262148 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;
		}
	}
}
int siz[maxn];
vector<int> ind[maxn];
vector< vector<int> > ans[maxn];
vector<int> solve(int v, int x) {
	for (int i = 0;i < siz[v];i++) {
		if (ind[v][i] == x) return ans[v][i];
	}
	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;
				}
			}
		}
		ind[v][siz[v]] = x;
		ans[v][siz[v]++] = ret;
		return ret;
	}
}
int main() {
	io
	cin >> n;
	for (int i = 1;i <= n;i++) cin >> a[i];
	for (int i = 1;i <= n;i++) {
		ind[i].resize(40);
		ans[i].resize(40);
	}
	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:55:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |    for (int i = 0;i < v1.size();i++) {
      |                   ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9708 KB Output is correct
2 Correct 8 ms 9708 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9708 KB Output is correct
2 Correct 8 ms 9708 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 9 ms 9836 KB Output is correct
7 Correct 9 ms 9836 KB Output is correct
8 Correct 8 ms 9836 KB Output is correct
9 Correct 7 ms 9836 KB Output is correct
10 Correct 9 ms 9836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9708 KB Output is correct
2 Correct 8 ms 9708 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 9 ms 9836 KB Output is correct
7 Correct 9 ms 9836 KB Output is correct
8 Correct 8 ms 9836 KB Output is correct
9 Correct 7 ms 9836 KB Output is correct
10 Correct 9 ms 9836 KB Output is correct
11 Correct 9 ms 10988 KB Output is correct
12 Correct 9 ms 10988 KB Output is correct
13 Correct 9 ms 10988 KB Output is correct
14 Correct 11 ms 11116 KB Output is correct
15 Correct 11 ms 11116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9708 KB Output is correct
2 Correct 8 ms 9708 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 9 ms 9836 KB Output is correct
7 Correct 9 ms 9836 KB Output is correct
8 Correct 8 ms 9836 KB Output is correct
9 Correct 7 ms 9836 KB Output is correct
10 Correct 9 ms 9836 KB Output is correct
11 Correct 9 ms 10988 KB Output is correct
12 Correct 9 ms 10988 KB Output is correct
13 Correct 9 ms 10988 KB Output is correct
14 Correct 11 ms 11116 KB Output is correct
15 Correct 11 ms 11116 KB Output is correct
16 Correct 111 ms 73560 KB Output is correct
17 Correct 112 ms 73184 KB Output is correct
18 Correct 136 ms 73704 KB Output is correct
19 Correct 491 ms 94548 KB Output is correct
20 Correct 341 ms 94296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9708 KB Output is correct
2 Correct 8 ms 9708 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 9 ms 9836 KB Output is correct
7 Correct 9 ms 9836 KB Output is correct
8 Correct 8 ms 9836 KB Output is correct
9 Correct 7 ms 9836 KB Output is correct
10 Correct 9 ms 9836 KB Output is correct
11 Correct 9 ms 10988 KB Output is correct
12 Correct 9 ms 10988 KB Output is correct
13 Correct 9 ms 10988 KB Output is correct
14 Correct 11 ms 11116 KB Output is correct
15 Correct 11 ms 11116 KB Output is correct
16 Correct 111 ms 73560 KB Output is correct
17 Correct 112 ms 73184 KB Output is correct
18 Correct 136 ms 73704 KB Output is correct
19 Correct 491 ms 94548 KB Output is correct
20 Correct 341 ms 94296 KB Output is correct
21 Runtime error 317 ms 262148 KB Execution killed with signal 9
22 Halted 0 ms 0 KB -