//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 2e5 + 5;
const double eps = 1e-9;
int a[MAXN*4], ans[MAXN];
int n;
void upd() {
bool fl = 0;
for (int i = 1; i <= n; i++)
if (a[i] > ans[i]) return;
else if (a[i] < ans[i]) {
fl = 1;
break;
}
if (!fl) return;
for (int i = 1; i <= n; i++) ans[i] = a[i];
return;
}
bool check(vi &x, vi &y) {
for (int i = 1; i <= n; i++)
if (x[i] > y[i]) return 0;
else if (x[i] < y[i]) return 1;
return 0;
}
void solve(int k) {
if (a[2*k]==-1&&a[2*k+1]==-1)
return;
if (a[2*k+1]==-1) {
if (a[k] > a[2*k]) swap(a[k], a[2*k]);
return;
}
if (a[2*k] == -1) {
if (a[k] > a[2*k+1]) swap(a[k], a[2*k+1]);
return;
}
int mi = min(a[k], min(a[k*2],a[k*2+1]));
if (mi == a[k]) {
solve(k*2);
solve(k*2+1);
upd();
} else if (mi == a[k*2]) {
swap(a[k], a[k*2]);
solve(k*2);
solve(k*2+1);
upd();
} else {
swap(a[k],a[k*2+1]);
vi tmp, tmp2;
tmp.resize(n+1);
tmp2.resize(n+1);
for (int i = 1; i <= n; i++)
tmp[i] = a[i];
solve(k*2);
solve(k*2+1);
upd();
for (int i = 1; i <= n; i++) {
tmp2[i] = a[i];
a[i] = tmp[i];
}
swap(a[k*2],a[k*2+1]);
solve(k*2);
solve(k*2+1);
upd();
for (int i = 1; i <= n; i++)
tmp[i] = a[i];
if (check(tmp, tmp2)) for (int i = 1; i <= n; i++)
a[i] = tmp[i];
}
}
int main() {
ni(n);
memset(a, -1, sizeof a);
memset(ans, 0x3f, sizeof ans);
for (int i = 1; i <= n; i++)
ni(a[i]);
solve(1);
for (int i = 1; i <= n; i++)
printf("%d ", ans[i]);
enl;
}
Compilation message
swap.cpp: In function 'int main()':
swap.cpp:7:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define ni(n) scanf("%d", &(n))
~~~~~^~~~~~~~~~~~
swap.cpp:105:2: note: in expansion of macro 'ni'
ni(n);
^~
swap.cpp:7:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define ni(n) scanf("%d", &(n))
~~~~~^~~~~~~~~~~~
swap.cpp:109:3: note: in expansion of macro 'ni'
ni(a[i]);
^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
4224 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
4224 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
4224 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
4224 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
4224 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |