답안 #372272

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
372272 2021-02-27T12:48:42 Z 8e7 Swap (BOI16_swap) C++14
100 / 100
801 ms 121192 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);
	ord.push_back(cur);
}

void merge(vector<int> &v1, vector<int> &v2, vector<int> & ret) {
	int cur = ret.size();
	ret.resize(ret.size() + v1.size() + v2.size());
	for (int i = 0;(1<<(i + 1)) - 1 <= v2.size();i++) {
		for (int j = (1<<i);j < (1<<(i + 1));j++) ret[cur++] = v1[j - 1];
		for (int j = (1<<i);j < (1<<(i + 1));j++) ret[cur++] = v2[j - 1];
		if ((1<<(i + 2)) - 1 > v2.size()) {
			for (int j = (1<<(i + 1)) - 1;j < v1.size();j++) ret[cur++] = v1[j];
			for (int j = (1<<(i + 1)) - 1;j < v2.size();j++) ret[cur++] = 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++) {
			solve(i, ind[i][x], ans[i][x]);
		}
		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:25:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |  for (int i = 0;(1<<(i + 1)) - 1 <= v2.size();i++) {
      |                 ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
swap.cpp:28:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |   if ((1<<(i + 2)) - 1 > v2.size()) {
      |       ~~~~~~~~~~~~~~~~~^~~~~~~~~~~
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 < v1.size();j++) ret[cur++] = v1[j];
      |                                  ~~^~~~~~~~~~~
swap.cpp:30:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |    for (int j = (1<<(i + 1)) - 1;j < v2.size();j++) ret[cur++] = v2[j];
      |                                  ~~^~~~~~~~~~~
swap.cpp: In function 'void getind(int, int)':
swap.cpp:43:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |  if (search(v, x) != ind[v].size()) return;
      |      ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
swap.cpp: In function 'void solve(int, int, std::vector<int>&)':
swap.cpp:79:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |    for (int i = 0;i < v1.size();i++) {
      |                   ~~^~~~~~~~~~~
swap.cpp: In function 'int main()':
swap.cpp:100:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |   for (int x = 0;x < ind[i].size();x++) {
      |                  ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 7 ms 9708 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 7 ms 9708 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 7 ms 9708 KB Output is correct
7 Correct 7 ms 9708 KB Output is correct
8 Correct 8 ms 9708 KB Output is correct
9 Correct 7 ms 9708 KB Output is correct
10 Correct 7 ms 9708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 7 ms 9708 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 7 ms 9708 KB Output is correct
7 Correct 7 ms 9708 KB Output is correct
8 Correct 8 ms 9708 KB Output is correct
9 Correct 7 ms 9708 KB Output is correct
10 Correct 7 ms 9708 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 9 ms 10092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 7 ms 9708 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 7 ms 9708 KB Output is correct
7 Correct 7 ms 9708 KB Output is correct
8 Correct 8 ms 9708 KB Output is correct
9 Correct 7 ms 9708 KB Output is correct
10 Correct 7 ms 9708 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 9 ms 10092 KB Output is correct
16 Correct 48 ms 15992 KB Output is correct
17 Correct 49 ms 16000 KB Output is correct
18 Correct 59 ms 16128 KB Output is correct
19 Correct 200 ms 33124 KB Output is correct
20 Correct 223 ms 33120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 7 ms 9708 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 7 ms 9708 KB Output is correct
7 Correct 7 ms 9708 KB Output is correct
8 Correct 8 ms 9708 KB Output is correct
9 Correct 7 ms 9708 KB Output is correct
10 Correct 7 ms 9708 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 9 ms 10092 KB Output is correct
16 Correct 48 ms 15992 KB Output is correct
17 Correct 49 ms 16000 KB Output is correct
18 Correct 59 ms 16128 KB Output is correct
19 Correct 200 ms 33124 KB Output is correct
20 Correct 223 ms 33120 KB Output is correct
21 Correct 223 ms 35048 KB Output is correct
22 Correct 185 ms 35304 KB Output is correct
23 Correct 196 ms 35432 KB Output is correct
24 Correct 801 ms 119912 KB Output is correct
25 Correct 796 ms 121192 KB Output is correct