#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 = 400000;
int n;
int a[mxn];
unordered_map<int, vector<int> > f[mxn];
//vector<int> f[mxn][mxn];
vector<int> combine(int a, int x, int b, int y){
int p = 0;
int l = 1;
vector<int> ret;
while(p < (int)f[a][x].size()){
fr(k, 0, l){
ret.pb(f[a][x][p+k]);
}
fr(k, 0, l){
ret.pb(f[b][y][p+k]);
}
p += l;
l *= 2;
}
return ret;
}
vector<int> lex(vector<int> v1, vector<int> v2){
fr(i, 0, (int)v1.size()){
if(v1[i] < v2[i]) return v1;
else if(v1[i] > v2[i]) return v2;
}
return v1;
}
bool searchval(int k, int val){
if(k > n) return false;
if(a[k] == val) return true;
else return searchval(k*2, val)|searchval(k*2+1, val);
}
void solve(){
cin >> n;
fr(i, 1, n+1){
cin >> a[i];
}
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 --){
vector<int> cand;
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(a[i] != j && searchval(i, j)) continue;
if(i*2 > n){
f[i][j] = {j};
}
else{
int mi = min(j, min(a[i*2], a[i*2+1]));
if(j == mi){
f[i][j] = combine(i*2, a[i*2], i*2+1, a[i*2+1]);
}
else if(a[i*2] == mi){
f[i][j] = combine(i*2, j, i*2+1, a[i*2+1]);
}
else{
vector<int> g1 = combine(i*2, a[i*2], i*2+1, j);
vector<int> g2 = combine(i*2, j, i*2+1, a[i*2]);
f[i][j] = lex(g1, g2);
}
f[i][j].insert(f[i][j].begin(), mi);
}
}
}
fr(i, 0, orgn){
cout<<f[1][a[1]][i]<<' ';
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
22220 KB |
Output is correct |
2 |
Correct |
15 ms |
22248 KB |
Output is correct |
3 |
Correct |
14 ms |
22220 KB |
Output is correct |
4 |
Correct |
14 ms |
22252 KB |
Output is correct |
5 |
Correct |
15 ms |
22184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
22220 KB |
Output is correct |
2 |
Correct |
15 ms |
22248 KB |
Output is correct |
3 |
Correct |
14 ms |
22220 KB |
Output is correct |
4 |
Correct |
14 ms |
22252 KB |
Output is correct |
5 |
Correct |
15 ms |
22184 KB |
Output is correct |
6 |
Correct |
13 ms |
22248 KB |
Output is correct |
7 |
Correct |
15 ms |
22248 KB |
Output is correct |
8 |
Correct |
16 ms |
22248 KB |
Output is correct |
9 |
Correct |
15 ms |
22248 KB |
Output is correct |
10 |
Correct |
14 ms |
22248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
22220 KB |
Output is correct |
2 |
Correct |
15 ms |
22248 KB |
Output is correct |
3 |
Correct |
14 ms |
22220 KB |
Output is correct |
4 |
Correct |
14 ms |
22252 KB |
Output is correct |
5 |
Correct |
15 ms |
22184 KB |
Output is correct |
6 |
Correct |
13 ms |
22248 KB |
Output is correct |
7 |
Correct |
15 ms |
22248 KB |
Output is correct |
8 |
Correct |
16 ms |
22248 KB |
Output is correct |
9 |
Correct |
15 ms |
22248 KB |
Output is correct |
10 |
Correct |
14 ms |
22248 KB |
Output is correct |
11 |
Correct |
21 ms |
24012 KB |
Output is correct |
12 |
Correct |
21 ms |
24168 KB |
Output is correct |
13 |
Correct |
21 ms |
24012 KB |
Output is correct |
14 |
Correct |
22 ms |
24176 KB |
Output is correct |
15 |
Correct |
22 ms |
24196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
22220 KB |
Output is correct |
2 |
Correct |
15 ms |
22248 KB |
Output is correct |
3 |
Correct |
14 ms |
22220 KB |
Output is correct |
4 |
Correct |
14 ms |
22252 KB |
Output is correct |
5 |
Correct |
15 ms |
22184 KB |
Output is correct |
6 |
Correct |
13 ms |
22248 KB |
Output is correct |
7 |
Correct |
15 ms |
22248 KB |
Output is correct |
8 |
Correct |
16 ms |
22248 KB |
Output is correct |
9 |
Correct |
15 ms |
22248 KB |
Output is correct |
10 |
Correct |
14 ms |
22248 KB |
Output is correct |
11 |
Correct |
21 ms |
24012 KB |
Output is correct |
12 |
Correct |
21 ms |
24168 KB |
Output is correct |
13 |
Correct |
21 ms |
24012 KB |
Output is correct |
14 |
Correct |
22 ms |
24176 KB |
Output is correct |
15 |
Correct |
22 ms |
24196 KB |
Output is correct |
16 |
Correct |
1000 ms |
251772 KB |
Output is correct |
17 |
Correct |
998 ms |
250864 KB |
Output is correct |
18 |
Execution timed out |
1010 ms |
253372 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
22220 KB |
Output is correct |
2 |
Correct |
15 ms |
22248 KB |
Output is correct |
3 |
Correct |
14 ms |
22220 KB |
Output is correct |
4 |
Correct |
14 ms |
22252 KB |
Output is correct |
5 |
Correct |
15 ms |
22184 KB |
Output is correct |
6 |
Correct |
13 ms |
22248 KB |
Output is correct |
7 |
Correct |
15 ms |
22248 KB |
Output is correct |
8 |
Correct |
16 ms |
22248 KB |
Output is correct |
9 |
Correct |
15 ms |
22248 KB |
Output is correct |
10 |
Correct |
14 ms |
22248 KB |
Output is correct |
11 |
Correct |
21 ms |
24012 KB |
Output is correct |
12 |
Correct |
21 ms |
24168 KB |
Output is correct |
13 |
Correct |
21 ms |
24012 KB |
Output is correct |
14 |
Correct |
22 ms |
24176 KB |
Output is correct |
15 |
Correct |
22 ms |
24196 KB |
Output is correct |
16 |
Correct |
1000 ms |
251772 KB |
Output is correct |
17 |
Correct |
998 ms |
250864 KB |
Output is correct |
18 |
Execution timed out |
1010 ms |
253372 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |