Submission #372270

# Submission time Handle Problem Language Result Execution time Memory
372270 2021-02-27T12:45:23 Z 8e7 Swap (BOI16_swap) C++14
68 / 100
1000 ms 121448 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;
vector<int> ord;
void getorder(int cur) {
	if (cur * 2 + 1 <= n) getorder(cur * 2 + 1);
	if (cur * 2 <= n) getorder(cur * 2);
	//cout << cur << endl;
	ord.push_back(cur);
}

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;
		}
	}
}
vector<int> ind[maxn];
vector< vector<int> > ans[maxn];

inline int search(int v, int x) {
	return find(ind[v].begin(), ind[v].end(), x) - ind[v].begin();
}

void getind(int v, int x) {
	if (search(v, x) != ind[v].size()) return;
	bool lef = 2 * v <= n, rig = 2 * v + 1 <= n;
	if (lef && !rig) {
		getind(v * 2, x);
		getind(v * 2, a[v * 2]);
	} else if (lef && rig) {
		int l = a[2 * v], r = a[2 * v + 1];
		if (x < l && x < r) {
			getind(v * 2, l), getind(v * 2 + 1, r);
		} else if (l < x && l < r) {
			getind(v * 2, x), getind(v * 2 + 1, r);
		} else {
			getind(v * 2, l), getind(v * 2 + 1, x);
			getind(v * 2, x), getind(v * 2 + 1, l);
		}
	}
	ind[v].push_back(x);
}
void solve(int v, int x, vector<int> &ret) {
	ret.push_back(x);
	bool lef = 2 * v <= n, rig = 2 * v + 1 <= n;
	if (lef && !rig) {
		ret.push_back(a[2 * v]);
		if (a[2 * v] < x) swap(ret[0], ret[1]);
	} else if (lef && rig) {
		int l = a[2 * v], r = a[2 * v + 1], lind = v * 2, rind = v * 2 + 1;
		if (x < l && x < r) {
			merge(ans[lind][search(lind, l)], ans[rind][search(rind, r)], ret);
		} else if (l < x && l < r) {
			ret[0] = l;
			merge(ans[lind][search(lind, x)], ans[rind][search(rind, r)], ret);
		} else {
			ret[0] = r;
			vector<int> v1, v2;
			merge(ans[lind][search(lind, l)], ans[rind][search(rind, x)], v1);
			merge(ans[lind][search(lind, x)], ans[rind][search(rind, 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;
				}
			}
		}
	}
}
int main() {
	io
	cin >> n;
	for (int i = 1;i <= n;i++) cin >> a[i];
	getorder(1);
	getind(1, a[1]);
	for (int cnt = 0;cnt < n;cnt++) {
		int i = ord[cnt];
		ans[i].resize(ind[i].size());
		for (int x = 0;x < ind[i].size();x++) {
			//cout << ind[i][x] << ": ";
			solve(i, ind[i][x], ans[i][x]);
			//for (auto j:tmp) cout << j << " ";
			//cout << endl;
			//ans[i].push_back(tmp);
		}
		//cout << endl;
		if (i * 2 <= n) ans[i * 2].clear();
		if (i * 2 + 1 <= n) ans[i * 2 + 1].clear();
	}
	for (int i:ans[1][0]) 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:24:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |  for (int i = 0;(1<<(i + 1)) - 1 <= v2.size();i++) {
      |                 ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
swap.cpp:27:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |   if ((1<<(i + 2)) - 1 > v2.size()) {
      |       ~~~~~~~~~~~~~~~~~^~~~~~~~~~~
swap.cpp:28:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |    for (int j = (1<<(i + 1)) - 1;j < v1.size();j++) ret.push_back(v1[j]);
      |                                  ~~^~~~~~~~~~~
swap.cpp:29:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |    for (int j = (1<<(i + 1)) - 1;j < v2.size();j++) ret.push_back(v2[j]);
      |                                  ~~^~~~~~~~~~~
swap.cpp: In function 'void getind(int, int)':
swap.cpp:42:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |  if (search(v, x) != ind[v].size()) return;
      |      ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
swap.cpp: In function 'void solve(int, int, std::vector<int>&)':
swap.cpp:78:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |    for (int i = 0;i < v1.size();i++) {
      |                   ~~^~~~~~~~~~~
swap.cpp: In function 'int main()':
swap.cpp:99:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |   for (int x = 0;x < ind[i].size();x++) {
      |                  ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 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 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 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 9728 KB Output is correct
6 Correct 8 ms 9708 KB Output is correct
7 Correct 7 ms 9756 KB Output is correct
8 Correct 7 ms 9708 KB Output is correct
9 Correct 7 ms 9708 KB Output is correct
10 Correct 7 ms 9636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 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 9728 KB Output is correct
6 Correct 8 ms 9708 KB Output is correct
7 Correct 7 ms 9756 KB Output is correct
8 Correct 7 ms 9708 KB Output is correct
9 Correct 7 ms 9708 KB Output is correct
10 Correct 7 ms 9636 KB Output is correct
11 Correct 8 ms 9836 KB Output is correct
12 Correct 8 ms 9836 KB Output is correct
13 Correct 8 ms 9836 KB Output is correct
14 Correct 9 ms 10092 KB Output is correct
15 Correct 10 ms 10092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 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 9728 KB Output is correct
6 Correct 8 ms 9708 KB Output is correct
7 Correct 7 ms 9756 KB Output is correct
8 Correct 7 ms 9708 KB Output is correct
9 Correct 7 ms 9708 KB Output is correct
10 Correct 7 ms 9636 KB Output is correct
11 Correct 8 ms 9836 KB Output is correct
12 Correct 8 ms 9836 KB Output is correct
13 Correct 8 ms 9836 KB Output is correct
14 Correct 9 ms 10092 KB Output is correct
15 Correct 10 ms 10092 KB Output is correct
16 Correct 55 ms 16092 KB Output is correct
17 Correct 56 ms 15980 KB Output is correct
18 Correct 55 ms 16112 KB Output is correct
19 Correct 228 ms 32876 KB Output is correct
20 Correct 233 ms 32864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 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 9728 KB Output is correct
6 Correct 8 ms 9708 KB Output is correct
7 Correct 7 ms 9756 KB Output is correct
8 Correct 7 ms 9708 KB Output is correct
9 Correct 7 ms 9708 KB Output is correct
10 Correct 7 ms 9636 KB Output is correct
11 Correct 8 ms 9836 KB Output is correct
12 Correct 8 ms 9836 KB Output is correct
13 Correct 8 ms 9836 KB Output is correct
14 Correct 9 ms 10092 KB Output is correct
15 Correct 10 ms 10092 KB Output is correct
16 Correct 55 ms 16092 KB Output is correct
17 Correct 56 ms 15980 KB Output is correct
18 Correct 55 ms 16112 KB Output is correct
19 Correct 228 ms 32876 KB Output is correct
20 Correct 233 ms 32864 KB Output is correct
21 Correct 219 ms 34800 KB Output is correct
22 Correct 231 ms 35528 KB Output is correct
23 Correct 233 ms 35688 KB Output is correct
24 Execution timed out 1082 ms 121448 KB Time limit exceeded
25 Halted 0 ms 0 KB -