답안 #372240

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
372240 2021-02-27T10:06:33 Z 8e7 Swap (BOI16_swap) C++14
68 / 100
1000 ms 167884 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;
		}
	}
}
vector<int> ind[maxn];
vector< vector<int> > ans[maxn];
vector<int> solve(int v, int x) {
	for (int i = 0;i < ind[v].size();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].push_back(x);
		ans[v].push_back(ret);
		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:30:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |  for (int i = 0;i < ind[v].size();i++) {
      |                 ~~^~~~~~~~~~~~~~~
swap.cpp:54:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |    for (int i = 0;i < v1.size();i++) {
      |                   ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 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
# 결과 실행 시간 메모리 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 7 ms 9708 KB Output is correct
7 Correct 14 ms 9708 KB Output is correct
8 Correct 8 ms 9708 KB Output is correct
9 Correct 8 ms 9708 KB Output is correct
10 Correct 7 ms 9708 KB Output is correct
# 결과 실행 시간 메모리 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 7 ms 9708 KB Output is correct
7 Correct 14 ms 9708 KB Output is correct
8 Correct 8 ms 9708 KB Output is correct
9 Correct 8 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 9 ms 9964 KB Output is correct
13 Correct 8 ms 9836 KB Output is correct
14 Correct 11 ms 10092 KB Output is correct
15 Correct 12 ms 10220 KB Output is correct
# 결과 실행 시간 메모리 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 7 ms 9708 KB Output is correct
7 Correct 14 ms 9708 KB Output is correct
8 Correct 8 ms 9708 KB Output is correct
9 Correct 8 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 9 ms 9964 KB Output is correct
13 Correct 8 ms 9836 KB Output is correct
14 Correct 11 ms 10092 KB Output is correct
15 Correct 12 ms 10220 KB Output is correct
16 Correct 69 ms 19960 KB Output is correct
17 Correct 63 ms 19436 KB Output is correct
18 Correct 62 ms 19932 KB Output is correct
19 Correct 324 ms 49620 KB Output is correct
20 Correct 331 ms 49372 KB Output is correct
# 결과 실행 시간 메모리 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 7 ms 9708 KB Output is correct
7 Correct 14 ms 9708 KB Output is correct
8 Correct 8 ms 9708 KB Output is correct
9 Correct 8 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 9 ms 9964 KB Output is correct
13 Correct 8 ms 9836 KB Output is correct
14 Correct 11 ms 10092 KB Output is correct
15 Correct 12 ms 10220 KB Output is correct
16 Correct 69 ms 19960 KB Output is correct
17 Correct 63 ms 19436 KB Output is correct
18 Correct 62 ms 19932 KB Output is correct
19 Correct 324 ms 49620 KB Output is correct
20 Correct 331 ms 49372 KB Output is correct
21 Correct 273 ms 53604 KB Output is correct
22 Correct 268 ms 53764 KB Output is correct
23 Correct 277 ms 54584 KB Output is correct
24 Execution timed out 1100 ms 167884 KB Time limit exceeded
25 Halted 0 ms 0 KB -