Submission #56203

# Submission time Handle Problem Language Result Execution time Memory
56203 2018-07-10T08:49:42 Z 김세빈(#1581) Swap (BOI16_swap) C++11
100 / 100
456 ms 40936 KB
#include <bits/stdc++.h>

using namespace std;

vector <int> V[404040];
int A[202020], R[202020], D[202020];
int n;

void dfs(int p, int v)
{
	if(p > n) return;
	
	int i, j;
	
	for(i=0;i<V[p].size();i++){
		if(V[p][i] == v) break;
	}
	if(i < V[p].size()){
		if(V[p][i] > 0) V[p][i] = -V[p][i];
	}
	
	dfs(p<<1, v);
	dfs(p<<1|1, v);
}

void uncheck(int t, int p, int r, int o)
{
	if(t & (1 << (D[t] - D[r] - 1))){
		dfs(r<<1|1, p);
		if(o) dfs(r<<1, p);
	}
	else{
		dfs(r<<1, p);
		if(o) dfs(r<<1|1, p);
	}
}

int main()
{
	int i, j, m, a, b, c;
	
	scanf("%d", &n);
	
	for(i=1;i<=n;i++){
		scanf("%d", A+i);
	}
	
	for(i=2;i<=n;i++){
		D[i] = D[i>>1] + 1;
	}
	
	V[1].push_back(1);
	
	for(i=1;i<=n;i++){
		m = -1;
		for(j=0;j<V[i].size();j++){
//			printf("%d (%d)\n", V[i][j], A[V[i][j]]);
			if(V[i][j] > 0 && (m == -1 || A[V[i][j]] < A[m])) m = V[i][j];
		}
		a = A[m];
		
		b = (i<<1) > n? 1e9 : A[i<<1];
		c = (i<<1|1) > n? 1e9 : A[i<<1|1];
		
		if(a < b && a < c){
			printf("%d ", a);
			
			if(V[i].size() > 1){
				for(j=0;j<V[i].size();j++){
					if(V[i][j] == m) break;
					if(V[i][j] > 0) uncheck(i, V[i][j], R[m], 0);
				}
				uncheck(i, m, R[m], 1);
				for(j++;j<V[i].size();j++){
					if(V[i][j] > 0) uncheck(i, V[i][j], R[V[i][j]], 0);
				}
			}
			
			V[i<<1].push_back(i<<1);
			V[i<<1|1].push_back(i<<1|1);
		}
		else if(b < c){
			printf("%d ", b);
			
			V[i<<1] = V[i];
			V[i<<1|1].push_back(i<<1|1);
		}
		else{
			printf("%d ", c);
			
			if(V[i].size() == 1) R[m] = i;
			R[i<<1] = i;
			V[i].push_back(i<<1);
			
			V[i<<1] = V[i];
			V[i<<1|1] = V[i];
		}
		
//		printf("\n");
	}
	
	printf("\n");
	
	return 0;
}

Compilation message

swap.cpp: In function 'void dfs(int, int)':
swap.cpp:15:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0;i<V[p].size();i++){
          ~^~~~~~~~~~~~
swap.cpp:18:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if(i < V[p].size()){
     ~~^~~~~~~~~~~~~
swap.cpp:13:9: warning: unused variable 'j' [-Wunused-variable]
  int i, j;
         ^
swap.cpp: In function 'int main()':
swap.cpp:56:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(j=0;j<V[i].size();j++){
           ~^~~~~~~~~~~~
swap.cpp:69:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(j=0;j<V[i].size();j++){
             ~^~~~~~~~~~~~
swap.cpp:74:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(j++;j<V[i].size();j++){
             ~^~~~~~~~~~~~
swap.cpp:42:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
swap.cpp:45:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", A+i);
   ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9852 KB Output is correct
2 Correct 10 ms 9852 KB Output is correct
3 Correct 10 ms 9884 KB Output is correct
4 Correct 10 ms 9964 KB Output is correct
5 Correct 10 ms 10088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9852 KB Output is correct
2 Correct 10 ms 9852 KB Output is correct
3 Correct 10 ms 9884 KB Output is correct
4 Correct 10 ms 9964 KB Output is correct
5 Correct 10 ms 10088 KB Output is correct
6 Correct 10 ms 10088 KB Output is correct
7 Correct 14 ms 10088 KB Output is correct
8 Correct 11 ms 10160 KB Output is correct
9 Correct 10 ms 10160 KB Output is correct
10 Correct 13 ms 10160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9852 KB Output is correct
2 Correct 10 ms 9852 KB Output is correct
3 Correct 10 ms 9884 KB Output is correct
4 Correct 10 ms 9964 KB Output is correct
5 Correct 10 ms 10088 KB Output is correct
6 Correct 10 ms 10088 KB Output is correct
7 Correct 14 ms 10088 KB Output is correct
8 Correct 11 ms 10160 KB Output is correct
9 Correct 10 ms 10160 KB Output is correct
10 Correct 13 ms 10160 KB Output is correct
11 Correct 11 ms 10160 KB Output is correct
12 Correct 11 ms 10160 KB Output is correct
13 Correct 12 ms 10160 KB Output is correct
14 Correct 11 ms 10160 KB Output is correct
15 Correct 14 ms 10160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9852 KB Output is correct
2 Correct 10 ms 9852 KB Output is correct
3 Correct 10 ms 9884 KB Output is correct
4 Correct 10 ms 9964 KB Output is correct
5 Correct 10 ms 10088 KB Output is correct
6 Correct 10 ms 10088 KB Output is correct
7 Correct 14 ms 10088 KB Output is correct
8 Correct 11 ms 10160 KB Output is correct
9 Correct 10 ms 10160 KB Output is correct
10 Correct 13 ms 10160 KB Output is correct
11 Correct 11 ms 10160 KB Output is correct
12 Correct 11 ms 10160 KB Output is correct
13 Correct 12 ms 10160 KB Output is correct
14 Correct 11 ms 10160 KB Output is correct
15 Correct 14 ms 10160 KB Output is correct
16 Correct 51 ms 14072 KB Output is correct
17 Correct 53 ms 14240 KB Output is correct
18 Correct 45 ms 14304 KB Output is correct
19 Correct 123 ms 17456 KB Output is correct
20 Correct 83 ms 17456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 9852 KB Output is correct
2 Correct 10 ms 9852 KB Output is correct
3 Correct 10 ms 9884 KB Output is correct
4 Correct 10 ms 9964 KB Output is correct
5 Correct 10 ms 10088 KB Output is correct
6 Correct 10 ms 10088 KB Output is correct
7 Correct 14 ms 10088 KB Output is correct
8 Correct 11 ms 10160 KB Output is correct
9 Correct 10 ms 10160 KB Output is correct
10 Correct 13 ms 10160 KB Output is correct
11 Correct 11 ms 10160 KB Output is correct
12 Correct 11 ms 10160 KB Output is correct
13 Correct 12 ms 10160 KB Output is correct
14 Correct 11 ms 10160 KB Output is correct
15 Correct 14 ms 10160 KB Output is correct
16 Correct 51 ms 14072 KB Output is correct
17 Correct 53 ms 14240 KB Output is correct
18 Correct 45 ms 14304 KB Output is correct
19 Correct 123 ms 17456 KB Output is correct
20 Correct 83 ms 17456 KB Output is correct
21 Correct 148 ms 26512 KB Output is correct
22 Correct 164 ms 26512 KB Output is correct
23 Correct 158 ms 26512 KB Output is correct
24 Correct 448 ms 40876 KB Output is correct
25 Correct 456 ms 40936 KB Output is correct