#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define LongDepTrai "swap"
#define ll long long
#define ull unsigned long long
#define ld long double
#define ii pair<int,int>
#define iii pair<int,ii>
#define iv pair<ii,ii>
#define pll pair<ll,ll>
#define vi vector<int>
#define vii vector<ii>
#define vll vector<ll>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) int((x).size())
#define order_set(T) tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
inline ll add(ll a, ll b, ll mod){ a += b; if(a >= mod) a -= mod; return a; }
inline ll sub(ll a, ll b, ll mod){ a -= b; if(a < 0) a += mod; return a; }
inline ll mul(ll a, ll b, ll mod){ return ( (ll)a * b ) % mod; }
static mt19937_64 rng((unsigned)chrono::steady_clock::now().time_since_epoch().count());
const int N=2e5+9;
int n,a[N];
vi best,tmp;
void Try(int i){
if(i>n/2){
for(int j=n/2+1;j<=n;j++) tmp.pb(a[j]);
best=min(best,tmp);
for(int j=n/2+1;j<=n;j++) tmp.pop_back();
return;
}
if(a[i]<min(a[i*2],a[i*2+1])) {
tmp.pb(a[i]);
Try(i+1);
tmp.pop_back();
}
else
if(a[i*2]<min(a[i],a[i*2+1])){
swap(a[i],a[i*2]);
tmp.pb(a[i]);
Try(i+1);
tmp.pop_back();
swap(a[i],a[i*2]);
}
else
if(a[i*2+1]<min(a[i],a[i*2])){
swap(a[i],a[i*2]);
swap(a[i],a[i*2+1]);
tmp.pb(a[i]);
Try(i+1);
tmp.pop_back();
swap(a[i],a[i*2+1]);
swap(a[i],a[i*2]);
swap(a[i],a[i*2+1]);
tmp.pb(a[i]);
Try(i+1);
tmp.pop_back();
swap(a[i],a[i*2+1]);
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if(fopen(LongDepTrai".inp","r")){
freopen(LongDepTrai".inp","r",stdin);
freopen(LongDepTrai".out","w",stdout);
}
best.pb(1000000000);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
Try(1);
for(int x:best) cout<<x<<" ";
return 0;
}
Compilation message (stderr)
swap.cpp: In function 'int main()':
swap.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | freopen(LongDepTrai".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
swap.cpp:77:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | freopen(LongDepTrai".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |