답안 #414343

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
414343 2021-05-30T10:59:36 Z Blagojce Swap (BOI16_swap) C++11
0 / 100
10 ms 14668 KB
#include <bits/stdc++.h>
#include <cstdio>
#pragma GCC optimize("O3")
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
 
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const ll inf = 1e18;
const ll mod = 1000000007;
const ld eps = 1e-13;
const ld pi  = 3.14159265359;
 
mt19937 _rand(time(NULL));
clock_t timer = clock();
const int mxn = 262144 + 10;
 
inline int readint(){
	int ret=0;
	for(char c;;){
		c=getchar();
		if('0'<=c and c<='9')
			ret=ret*10+(c&15);
		else
			return ret;
	}
}
vector<char> digits;
inline void printint(int x){
	do{
		digits.push_back(x%10|48);
		x/=10;
	}while(x);
	while(not digits.empty())
		putchar(digits.back()), digits.pop_back();
}
 
 
int n;
int a[mxn];
 
 
unordered_map<int, vector<int> > f[mxn];
int p, l;
int mi;

void combine(vector<int> &v1, vector<int> &v2, vector<int> &r){
	
	
	p = 0;
	l = 1;
	
	while(p < (int)v1.size()){
		fr(k, 0, l){
			r.pb(v1[p+k]);
		}
		fr(k, 0, l){
			r.pb(v2[p+k]);
		}
		p += l;
		l *= 2;
	}
}
 

 
vector<int> g1;
vector<int> g2;
vector<int> cand;

void rec(int k){
	
	cand.pb(a[k]);
	if(k != 1){
		if(k%2) cand.pb(a[k-1]);
		else cand.pb(a[k+1]);
	}
	
	
	if(k*2 > n){
		for(auto u : cand){
			f[k][u] = {u};
		}
		cand.pop_back();
		if(k != 1){
			cand.pop_back();
		}
		return;
	}
	
	rec(k*2);
	rec(k*2+1);
	
	
	for(auto j : cand){
		
		mi = min(j, min(a[k*2], a[k*2+1]));
		
		if(j == mi){
			f[k][j] = {mi};
			combine(f[k*2][a[k*2]], f[k*2+1][a[k*2+1]], f[k][j]);
		}
		else if(a[k*2] == mi){
			f[k][j] = {mi};
			combine(f[k*2][j], f[k*2+1][a[k*2+1]], f[k][j]);
		}
		else{
			
			
			g1.clear();
			g1.pb(mi);
			
			combine(f[k*2][a[k*2]], f[k*2+1][j], g1);
			g2.clear();
			g2.pb(mi);
			combine(f[k*2][j], f[k*2+1][a[k*2]], g2);
			f[k][j] = min(g1, g2);
		}
	}
	cand.pop_back();
	if(k != 1){
		cand.pop_back();
	}
	f[2*k].clear();
	f[2*k+1].clear();
}

 
void solve(){
	n = readint();
	fr(i, 1, n+1){
		a[i] = readint();
	}
	int orgn = n;
	int m = n;
	while(m+1 - ((m+1)&-(m+1)) != 0){	
		++m;
	}
	
	fr(i, n+1, m+1){
		a[i] = i;
	}
	n = m;
	/*for(int i = n; i >= 1; i --){
		cand.clear();	
		for(int j = i; j >= 1; j /= 2){
			cand.pb(a[j]);
			if(j != 1){
				if(j % 2){
					cand.pb(a[j-1]);
				}
				else{
					cand.pb(a[j+1]);
				}
			}
		}
		
		
		for(auto j : cand){
			if(i*2 > n){
				f[i][j] = {j};
			}
			else{
				mi = min(j, min(a[i*2], a[i*2+1]));
				if(j == mi){
					f[i][j] = {mi};
					combine(f[i*2][a[i*2]], f[i*2+1][a[i*2+1]], f[i][j]);
				}
				else if(a[i*2] == mi){
					f[i][j] = {mi};
					combine(f[i*2][j], f[i*2+1][a[i*2+1]], f[i][j]);
				}
				else{
					g1.clear();
					g1.pb(mi);
					combine(f[i*2][a[i*2]], f[i*2+1][j], g1);
					g2.clear();
					g2.pb(mi);
					combine(f[i*2][j], f[i*2+1][a[i*2]], g2);
					f[i][j] = min(g1, g2);
				}
				//f[i][j].insert(f[i][j].begin(), mi);	
			}
		}
		if(i*2 <= n){
			f[i*2].clear();
			f[i*2+1].clear();
		}
		
		
	}*/
	rec(1);
	
	vector<int> ans = f[1][a[1]];
	fr(i, 0, orgn){
		printint(ans[i]);
		putchar(' ');
	}	
}
 
int main(){
	digits.reserve(10);
	freopen("t.in.25", "r", stdin);
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	solve();
}

Compilation message

swap.cpp: In function 'int main()':
swap.cpp:209:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  209 |  freopen("t.in.25", "r", stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 14668 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 14668 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 14668 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 14668 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 10 ms 14668 KB Output isn't correct
2 Halted 0 ms 0 KB -