-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSPOJ21395.cc
71 lines (66 loc) · 1.76 KB
/
SPOJ21395.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPOJ 21395: Ohani And Binary Search Tree
// http://www.spoj.com/problems/OHANIBTR/
//
// Solution: optimal insertion sort, complete bst
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <vector>
#include <algorithm>
using namespace std;
#define fst first
#define snd second
#define all(c) ((c).begin()), ((c).end())
template <class T>
int longest_increasing_subsequence(const vector<T> &x) {
int n = x.size();
vector<int> length(n), tail;
for (int k = 0; k < n; ++k) {
length[k] = distance(tail.begin(), upper_bound(all(tail), k,
[&](int i, int j) { return x[i] < x[j]; }
));
if (length[k] == tail.size()) tail.push_back(k);
else tail[length[k]] = k;
}
return *max_element(all(length)) + 1;
}
vector<int> completeBST(int n) {
vector<int> p(n, -1);
function<void (int,int,int)> rec = [&](int i, int j, int q) {
if (j <= i) return;
if (j == i + 1) {
p[i] = q;
} else {
int n = j - i, k = 1;
while (2*k <= n) k *= 2;
if (k/2 - 1 <= (n-k)) k = k - 1;
else k = n - k/2;
p[i+k] = q;
rec(i, i+k, i+k);
rec(i+k+1, j, i+k);
}
};
rec(0, n, -1);
return p;
}
int main() {
int ncase; scanf("%d", &ncase);
for (int icase = 0; icase < ncase; ++icase) {
printf("Case %d:\n", icase+1);
int n; scanf("%d", &n);
vector<int> x(n);
for (int i = 0; i < n; ++i)
scanf("%d", &x[i]);
printf("Minimum Move: %d\n", n - longest_increasing_subsequence(x));
sort(all(x));
vector<int> p = completeBST(n);
for (int i = 0; i < n; ++i) {
if (i > 0) printf(" ");
if (p[i] < 0) printf("-1");
else printf("%d", x[p[i]]);
}
printf("\n");
}
}