Submission #27087

# Submission time Handle Problem Language Result Execution time Memory
27087 2017-07-09T08:10:30 Z 김현수(#1118) Swap (BOI16_swap) C++14
100 / 100
106 ms 13960 KB
#include<bits/stdc++.h>
using namespace std;
const int N = 200005, inf = 1e9;
int n, a[N];

vector<int> dt[N];

bool upd (int I, int J, int V) {
	while(dt[I].size() <= J) dt[I].push_back(0);
	dt[I][J] = max(dt[I][J], V);
}

int get (int I, int V) {
	for(int i=0;i<dt[I].size();i++) {
		if(V < dt[I][i]) return i;
	}
	return inf;
}

void calc (int I) {
	if(2*I > n) {upd(I,0,inf); return;}
	calc(2*I);
	if(2*I+1 > n) {upd(I,0,a[2*I]); upd(I,1,inf); return;}
	calc(2*I+1);
	int A = min(a[2*I], a[2*I+1]), B = a[2*I]+a[2*I+1]-A;
	upd(I, 0, A);
	if(a[2*I] < a[2*I+1]) {
		for(auto &T : dt[2*I]) dt[I].push_back(T);
		return;
	}
	int flag = 0;
	for(int i=0,j=0;i<dt[2*I].size()&&j<dt[2*I+1].size();) {
		int P= dt[2*I][i], Q = dt[2*I+1][j], R = min(P, Q);
		if(!flag) {
			if(B < R) {flag = 1 + (i > j); upd(I, min(i, j)+1, B);}
			else upd(I, min(i, j)+1, R);
		}
		if(flag) upd(I, (flag-1?i:j)+1, R);
		P < Q ? i++ : j++;
	}
	for(int i=1;i<dt[I].size();i++) {
		dt[I][i] = max(dt[I][i], dt[I][i-1]);
	}
}

void solve (int I) {
	if(2*I > n) return;
	if(2*I+1 > n) {if(a[2*I] < a[I]) swap(a[2*I], a[I]); return;}
	if(a[I] < a[2*I] && a[I] < a[2*I+1]) return;
	if(a[2*I] < a[I] && a[2*I] < a[2*I+1]) {swap(a[2*I], a[I]); return;}
	int A = min(a[I], a[2*I]), B = a[I]+a[2*I]-A;
	a[I] = a[2*I+1];
	if(get(2*I, A) <= get(2*I+1, A)) {a[2*I] = A; a[2*I+1] = B;}
	else {a[2*I] = B; a[2*I+1] = A;}
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	calc(1);
	for(int i=1;i<=n;i++) {solve(i); printf("%d ",a[i]);}
	puts("");
}

Compilation message

swap.cpp: In function 'bool upd(int, int, int)':
swap.cpp:9:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  while(dt[I].size() <= J) dt[I].push_back(0);
                     ^
swap.cpp:11:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
swap.cpp: In function 'int get(int, int)':
swap.cpp:14:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<dt[I].size();i++) {
               ^
swap.cpp: In function 'void calc(int)':
swap.cpp:32:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0,j=0;i<dt[2*I].size()&&j<dt[2*I+1].size();) {
                   ^
swap.cpp:32:37: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0,j=0;i<dt[2*I].size()&&j<dt[2*I+1].size();) {
                                     ^
swap.cpp:41:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=1;i<dt[I].size();i++) {
               ^
swap.cpp: In function 'int main()':
swap.cpp:59:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
                ^
swap.cpp:60:41: 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]);
                                         ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7492 KB Output is correct
2 Correct 0 ms 7492 KB Output is correct
3 Correct 0 ms 7492 KB Output is correct
4 Correct 0 ms 7492 KB Output is correct
5 Correct 0 ms 7492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7492 KB Output is correct
2 Correct 0 ms 7492 KB Output is correct
3 Correct 0 ms 7492 KB Output is correct
4 Correct 0 ms 7492 KB Output is correct
5 Correct 0 ms 7492 KB Output is correct
6 Correct 3 ms 7492 KB Output is correct
7 Correct 0 ms 7492 KB Output is correct
8 Correct 3 ms 7492 KB Output is correct
9 Correct 0 ms 7492 KB Output is correct
10 Correct 3 ms 7492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7492 KB Output is correct
2 Correct 0 ms 7492 KB Output is correct
3 Correct 0 ms 7492 KB Output is correct
4 Correct 0 ms 7492 KB Output is correct
5 Correct 0 ms 7492 KB Output is correct
6 Correct 3 ms 7492 KB Output is correct
7 Correct 0 ms 7492 KB Output is correct
8 Correct 3 ms 7492 KB Output is correct
9 Correct 0 ms 7492 KB Output is correct
10 Correct 3 ms 7492 KB Output is correct
11 Correct 0 ms 7492 KB Output is correct
12 Correct 0 ms 7492 KB Output is correct
13 Correct 3 ms 7492 KB Output is correct
14 Correct 0 ms 7492 KB Output is correct
15 Correct 0 ms 7492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7492 KB Output is correct
2 Correct 0 ms 7492 KB Output is correct
3 Correct 0 ms 7492 KB Output is correct
4 Correct 0 ms 7492 KB Output is correct
5 Correct 0 ms 7492 KB Output is correct
6 Correct 3 ms 7492 KB Output is correct
7 Correct 0 ms 7492 KB Output is correct
8 Correct 3 ms 7492 KB Output is correct
9 Correct 0 ms 7492 KB Output is correct
10 Correct 3 ms 7492 KB Output is correct
11 Correct 0 ms 7492 KB Output is correct
12 Correct 0 ms 7492 KB Output is correct
13 Correct 3 ms 7492 KB Output is correct
14 Correct 0 ms 7492 KB Output is correct
15 Correct 0 ms 7492 KB Output is correct
16 Correct 29 ms 9076 KB Output is correct
17 Correct 29 ms 9076 KB Output is correct
18 Correct 23 ms 9076 KB Output is correct
19 Correct 19 ms 9076 KB Output is correct
20 Correct 26 ms 9076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7492 KB Output is correct
2 Correct 0 ms 7492 KB Output is correct
3 Correct 0 ms 7492 KB Output is correct
4 Correct 0 ms 7492 KB Output is correct
5 Correct 0 ms 7492 KB Output is correct
6 Correct 3 ms 7492 KB Output is correct
7 Correct 0 ms 7492 KB Output is correct
8 Correct 3 ms 7492 KB Output is correct
9 Correct 0 ms 7492 KB Output is correct
10 Correct 3 ms 7492 KB Output is correct
11 Correct 0 ms 7492 KB Output is correct
12 Correct 0 ms 7492 KB Output is correct
13 Correct 3 ms 7492 KB Output is correct
14 Correct 0 ms 7492 KB Output is correct
15 Correct 0 ms 7492 KB Output is correct
16 Correct 29 ms 9076 KB Output is correct
17 Correct 29 ms 9076 KB Output is correct
18 Correct 23 ms 9076 KB Output is correct
19 Correct 19 ms 9076 KB Output is correct
20 Correct 26 ms 9076 KB Output is correct
21 Correct 106 ms 13960 KB Output is correct
22 Correct 79 ms 13960 KB Output is correct
23 Correct 99 ms 13960 KB Output is correct
24 Correct 103 ms 13960 KB Output is correct
25 Correct 96 ms 13960 KB Output is correct