답안 #258588

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
258588 2020-08-06T07:45:33 Z 송준혁(#5060) Swap (BOI16_swap) C++17
100 / 100
104 ms 30840 KB
#include <bits/stdc++.h>
#define pb push_back
#define lb lower_bound
#define fi first
#define se second
#define mup(a,x) a=min(a,x)
#define Mup(a,x) a=max(a,x)
#define INF 1234567890
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;

int N;
int A[202020];
map<int,int> D[202020];

int f(int u, int x){
	if (D[u].find(x) != D[u].end()) return D[u][x];
	if (2*u > N) return D[u][x] = u;
	if (2*u == N){
		if (A[2*u] < x) return D[u][x] = f(2*u, x);
		return D[u][x] = u;
	}
	if (A[2*u] > x && A[2*u+1] > x) return D[u][x] = u;
	if (A[2*u] < x && A[2*u] < A[2*u+1]) return D[u][x] = f(2*u, x);
	if (A[2*u] < x){
		if (f(2*u, A[2*u]) < f(2*u+1, A[2*u])) return D[u][x] = f(2*u+1, x);
		return D[u][x] = f(2*u, x);
	}
	if (f(2*u, x) < f(2*u+1, x)) return D[u][x] = f(2*u, x);
	return D[u][x] = f(2*u+1, x);
}

void g(int u){
	if (2*u > N) return;
	if (2*u == N){
		if (A[2*u] < A[u]) swap(A[u], A[2*u]);
		return;
	}
	if (A[2*u] < A[u] && A[2*u] < A[2*u+1]) swap(A[u], A[2*u]);
	else if (A[u] > A[2*u+1] && A[2*u] > A[2*u+1]){
		if (A[2*u] < A[u]){
			if (f(2*u, A[2*u]) < f(2*u+1, A[2*u])) swap(A[u], A[2*u+1]);
			else swap(A[2*u], A[2*u+1]), swap(A[u], A[2*u]);
		}
		else{
			if (f(2*u, A[u]) < f(2*u+1, A[u])) swap(A[2*u], A[2*u+1]), swap(A[u], A[2*u]);
			else swap(A[u], A[2*u+1]);
		}
	}
	g(2*u), g(2*u+1);
}

int main(){
	scanf("%d", &N);
	for (int i=1; i<=N; i++) scanf("%d", &A[i]);
	g(1);
	for (int i=1; i<=N; i++) printf("%d ", A[i]);
	printf("\n");
	return 0;
}

Compilation message

swap.cpp: In function 'int main()':
swap.cpp:55:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
swap.cpp:56:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i=1; i<=N; i++) scanf("%d", &A[i]);
                           ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9856 KB Output is correct
2 Correct 6 ms 9856 KB Output is correct
3 Correct 6 ms 9856 KB Output is correct
4 Correct 6 ms 9856 KB Output is correct
5 Correct 5 ms 9856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9856 KB Output is correct
2 Correct 6 ms 9856 KB Output is correct
3 Correct 6 ms 9856 KB Output is correct
4 Correct 6 ms 9856 KB Output is correct
5 Correct 5 ms 9856 KB Output is correct
6 Correct 5 ms 9856 KB Output is correct
7 Correct 5 ms 9856 KB Output is correct
8 Correct 7 ms 9856 KB Output is correct
9 Correct 6 ms 9856 KB Output is correct
10 Correct 6 ms 9856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9856 KB Output is correct
2 Correct 6 ms 9856 KB Output is correct
3 Correct 6 ms 9856 KB Output is correct
4 Correct 6 ms 9856 KB Output is correct
5 Correct 5 ms 9856 KB Output is correct
6 Correct 5 ms 9856 KB Output is correct
7 Correct 5 ms 9856 KB Output is correct
8 Correct 7 ms 9856 KB Output is correct
9 Correct 6 ms 9856 KB Output is correct
10 Correct 6 ms 9856 KB Output is correct
11 Correct 8 ms 9984 KB Output is correct
12 Correct 8 ms 9856 KB Output is correct
13 Correct 6 ms 9856 KB Output is correct
14 Correct 6 ms 9856 KB Output is correct
15 Correct 6 ms 9856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9856 KB Output is correct
2 Correct 6 ms 9856 KB Output is correct
3 Correct 6 ms 9856 KB Output is correct
4 Correct 6 ms 9856 KB Output is correct
5 Correct 5 ms 9856 KB Output is correct
6 Correct 5 ms 9856 KB Output is correct
7 Correct 5 ms 9856 KB Output is correct
8 Correct 7 ms 9856 KB Output is correct
9 Correct 6 ms 9856 KB Output is correct
10 Correct 6 ms 9856 KB Output is correct
11 Correct 8 ms 9984 KB Output is correct
12 Correct 8 ms 9856 KB Output is correct
13 Correct 6 ms 9856 KB Output is correct
14 Correct 6 ms 9856 KB Output is correct
15 Correct 6 ms 9856 KB Output is correct
16 Correct 34 ms 12024 KB Output is correct
17 Correct 22 ms 12152 KB Output is correct
18 Correct 23 ms 12140 KB Output is correct
19 Correct 30 ms 15104 KB Output is correct
20 Correct 30 ms 15224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9856 KB Output is correct
2 Correct 6 ms 9856 KB Output is correct
3 Correct 6 ms 9856 KB Output is correct
4 Correct 6 ms 9856 KB Output is correct
5 Correct 5 ms 9856 KB Output is correct
6 Correct 5 ms 9856 KB Output is correct
7 Correct 5 ms 9856 KB Output is correct
8 Correct 7 ms 9856 KB Output is correct
9 Correct 6 ms 9856 KB Output is correct
10 Correct 6 ms 9856 KB Output is correct
11 Correct 8 ms 9984 KB Output is correct
12 Correct 8 ms 9856 KB Output is correct
13 Correct 6 ms 9856 KB Output is correct
14 Correct 6 ms 9856 KB Output is correct
15 Correct 6 ms 9856 KB Output is correct
16 Correct 34 ms 12024 KB Output is correct
17 Correct 22 ms 12152 KB Output is correct
18 Correct 23 ms 12140 KB Output is correct
19 Correct 30 ms 15104 KB Output is correct
20 Correct 30 ms 15224 KB Output is correct
21 Correct 74 ms 19064 KB Output is correct
22 Correct 74 ms 19068 KB Output is correct
23 Correct 87 ms 19044 KB Output is correct
24 Correct 103 ms 30840 KB Output is correct
25 Correct 104 ms 30840 KB Output is correct