This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << h; if(sizeof...(t)) cerr << ", ";
DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL
#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define tcT template<class T
#define nl "\n"
#define fi first
#define se second
using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
using db = long double;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;
void setIO(string NAME = "") {
cin.tie(0)->sync_with_stdio(0);
if(sz(NAME)) {
freopen((NAME + ".inp").c_str(),"r",stdin);
freopen((NAME + ".out").c_str(),"w",stdout);
}
}
tcT> bool ckmin(T&a, const T&b) {
return b < a ? a=b,1 : 0; }
tcT> bool ckmax(T&a, const T&b) {
return b > a ? a=b,1 : 0; }
const int MOD = 1e9 + 7;
const ll INF = 1e18;
const int LG = 18;
const int MX = 2e5+5;
int N;
int A[MX];
vi dp[MX][LG<<1];
bool need[MX][LG<<1];
void mrg(vi& res, const vi&a, const vi&b) {
for(int i = 0, j = 0, step = 1; i < sz(a) || j < sz(b); i += step, j += step, step <<= 1) {
for(int k = 0; k < step && i + k < sz(a); k++)
res.pb(a[i+k]);
for(int k = 0; k < step && j + k < sz(b); k++)
res.pb(b[j+k]);
}
}
void solve() {
cin >> N;
FOR(i,1,N+1) cin >> A[i];
need[1][0] = 1;
for(int i = 1; 2*i + 1 <= N; i++) {
F0R(j, LG<<1) if(need[i][j]) {
int b = (i >> (j>>1)) ^ (j&1);
if(A[i<<1|1] < A[i<<1] && A[i<<1|1] < A[b]) {
need[i<<1][0] = need[i<<1|1][1] = true;
if(j + 2 < (LG<<1)) need[i<<1|1][j+2] = need[i<<1][j+2] = true;
}
else {
need[i<<1][0] = need[i<<1|1][0] = 1;
if(j + 2 < (LG<<1)) need[i<<1][j+2] = 1;
}
}
}
ROF(i,1,N+1) F0R(j, LG<<1) if(need[i][j]) {
int b = (i >> (j>>1)) ^ (j&1);
if((i<<1|1) <= N) {
if(A[i<<1|1] < A[i<<1] && A[i<<1|1] < A[b]) {
vi opt1 = {A[i<<1|1]}, opt2 = opt1;
mrg(opt1, dp[i<<1][0], dp[i<<1|1][j+2]);
mrg(opt2, dp[i<<1][j+2], dp[i<<1|1][1]);
dp[i][j] = min(opt1, opt2);
}
else {
if(A[i<<1] < A[b] && A[i<<1] < A[i<<1|1]) {
dp[i][j] = {A[i<<1]};
mrg(dp[i][j], dp[i<<1][j+2], dp[i<<1|1][0]);
}
else {
dp[i][j] = {A[b]};
mrg(dp[i][j], dp[i<<1][0], dp[i<<1|1][0]);
}
}
}
else {
if((i<<1) <= N) {
dp[i][j] = {min(A[b], A[i<<1]), max(A[b], A[i<<1])};
}
else {
dp[i][j] = {A[b]};
}
}
if((i<<1|1) <= N) {
F0R(k, LG<<1) {
if(need[i<<1][k]) vi().swap(dp[i<<1][k]);
if(need[i<<1|1][k]) vi().swap(dp[i<<1|1][k]);
}
}
}
each(e, dp[1][0]) cout << e << " ";
cout << nl;
}
int main() {
setIO();
int t=1;
//cin>>t;
while(t-->0) {
solve();
}
return 0;
}
Compilation message (stderr)
swap.cpp: In function 'void setIO(std::string)':
swap.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen((NAME + ".inp").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
swap.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen((NAME + ".out").c_str(),"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... |