0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-24 03:31:59 +00:00

#1199. 90岁的张哥哥

https://sjzezoj.com/submission/46232
This commit is contained in:
Baoshuo Ren 2021-12-27 19:11:20 +08:00
parent 23c83d10aa
commit 88b60390ec
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

View File

@ -1,34 +1,120 @@
#pragma GCC optimize("Ofast")
#include <algorithm>
#include <iostream>
#include <limits>
#include <stack>
using std::cin;
using std::cout;
using std::endl;
// Limits
const int N = 300005;
int n, c, l, r, a[N];
// Variables
int a[N];
bool vis[N];
// Segment Tree
void build(int, int, int);
int query(int, int, int);
int main() {
std::ios::sync_with_stdio(false);
int n, c;
cin >> n >> c;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
l = 1, r = c;
for (int i = 1; i <= n; i++) {
int p = std::min_element(a + l, a + r + 1) - a;
cout << a[p] << ' ';
vis[p] = true;
a[p] = std::numeric_limits<int>::max();
while (p && vis[p]) p--;
l = std::max(p, 1);
r = std::min(r + 1, n);
a[0] = std::numeric_limits<int>::max();
build(1, 1, n);
int pos = query(1, 1, c);
std::stack<int> st;
for (int i = 1; i < pos; i++) {
st.push(i);
}
cout << a[pos] << ' ';
for (int i = c + 1; i <= n; i++) {
int p = query(1, pos + 1, i);
if (!st.empty() && a[st.top()] <= a[p]) {
cout << a[st.top()] << ' ';
st.pop();
} else {
for (int j = pos + 1; j < p; j++) {
st.push(j);
}
pos = p;
cout << a[p] << ' ';
}
}
for (int i = 1; i < c; i++) {
int p = pos + 1 <= n ? query(1, pos + 1, n) : 0;
if (!st.empty() && a[st.top()] <= a[p]) {
cout << a[st.top()] << ' ';
st.pop();
} else {
for (int j = pos + 1; j < p; j++) {
st.push(j);
}
pos = p;
cout << a[p] << ' ';
}
}
cout << endl;
return 0;
}
// === Segment Tree ===
struct node {
int l, r, id;
node()
: l(0), r(0), id(0) {}
node(int _l, int _r)
: l(_l), r(_r), id(0) {}
} tr[N << 2];
inline void pushup(int u) {
tr[u].id = a[tr[u << 1].id] <= a[tr[u << 1 | 1].id] ? tr[u << 1].id : tr[u << 1 | 1].id;
}
void build(int u, int l, int r) {
tr[u] = node(l, r);
if (l == r) {
tr[u].id = l;
return;
}
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
/**
* [l, r] a ****
* @param u
* @param l
* @param r
* @return a ****
*/
int query(int u, int l, int r) {
if (l <= tr[u].l && tr[u].r <= r) return tr[u].id;
int mid = tr[u].l + tr[u].r >> 1,
pos = 0;
if (l <= mid) {
int t = query(u << 1, l, r);
if (a[t] < a[pos]) pos = t;
}
if (r > mid) {
int t = query(u << 1 | 1, l, r);
if (a[t] < a[pos]) pos = t;
}
return pos;
}