답안 #868311

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
868311 2023-10-31T04:02:23 Z anha3k25cvp 중앙값 배열 (balkan11_medians) C++14
100 / 100
89 ms 23876 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define dl double
#define st first
#define nd second
#define II pair <int, int>

using namespace std;

const int N = 5 + 1e5;
const int inf = 7 + 1e9;

struct Segment {
    int n;
    vector <II> tree;
    Segment (int _n = 0) : n(_n) {
        tree.assign(4 * n + 1, {inf, inf});
    }
    void update(int l, int r, int node, int u, int val) {
        if (r < u || u < l)
            return;
        if (l == r) {
            tree[node] = {val, l};
            return;
        }
        int mid = (l + r) / 2;
        if (u <= mid)
            update(l, mid, 2 * node, u, val);
        else
            update(mid + 1, r, 2 * node + 1, u, val);
        tree[node] = min(tree[2 * node], tree[2 * node + 1]);
    }
    II get(int l, int r, int node, int u, int v) {
        if (r < u || v < l)
            return {inf, 0};
        if (u <= l && r <= v)
            return tree[node];
        int mid = (l + r) / 2;
        return min(get(l, mid, 2 * node, u, v), get(mid + 1, r, 2 * node + 1, u, v));
    }
};

int main() {
#define TASKNAME ""
    ios_base :: sync_with_stdio (0);
    cin.tie (0);
    if ( fopen( TASKNAME".inp", "r" ) ) {
        freopen (TASKNAME".inp", "r", stdin);
        freopen (TASKNAME".out", "w", stdout);
    }
    int n;
    cin >> n;
    vector <int> b(n + 1, 0), a(2 * n, 0), vis(2 * n, 0), ma(2 * n, 2 * n - 1);
    vector <vector <int>> cnt(2, vector <int> (2 * n, 0));
    vector <vector <II>> open(2 * n);
    Segment f(2 * n - 1);
    for (int i = 1; i <= n; i ++) {
        cin >> b[i];
        if (!vis[b[i]]) {
            ma[b[i]] = 2 * i - 1;
            vis[b[i]] = i;
        }
        if (i == 1)
            a[i] = b[i];
        else {
            if (b[i] == b[i - 1]) {
                cnt[0][b[i - 1]] ++;
                cnt[1][b[i - 1]] ++;
            }
            else if (b[i] < b[i - 1]) {
                cnt[0][b[i - 1]] += 2;
                if (b[i] + 1 < b[i - 1])
                    open[b[i] + 1].push_back({i, b[i - 1] - 1});
            }
            else {
                cnt[1][b[i - 1]] += 2;
                if (b[i - 1] + 1 < b[i])
                    open[b[i - 1] + 1].push_back({i, b[i] - 1});
            }
        }
    }
    vector <vector <int>> ins(2 * n);
    priority_queue <II> q;
    for (int i = 1; i < 2 * n; i ++) {
        for (auto z : open[i])
            q.push(z);
        while (!q.empty() && q.top().nd < i)
            q.pop();
        if (!q.empty())
            ins[q.top().st + 1].push_back(i);
        else
            ins[1].push_back(i);
    }
    vector <int> c1, c2(1, 0);
    for (int i = 2; i + 1 < 2 * n; i ++) {
        if (cnt[0][i] == i - 1)
            c1.push_back(i);
        if (cnt[1][i] == 2 * n - i)
            c2.push_back(i);
    }
    c1.push_back(2 * n);
    vis.assign(2 * n, 0);
    for (int u : ins[1])
        f.update(1, 2 * n - 1, 1, u, ma[u]);
    f.update(1, 2 * n - 1, 1, b[1], 2 * n);
    vis[b[1]] = 1;
    for (int i = 2; i <= n; i ++) {
        for (int u : ins[i])
            f.update(1, 2 * n - 1, 1, u, ma[u]);
        if (b[i] == b[i - 1]) {
            int u = lower_bound(c1.begin(), c1.end(), b[i - 1]) - c1.begin(), v = lower_bound(c2.begin(), c2.end(), b[i - 1]) - c2.begin();
            if (c2[v] == b[i - 1])
                v --;
            int l = c2[v] + 1, r = c1[u] - 1;
            if (r >= b[i - 1])
                r = b[i - 1] - 1;
            a[2 * i - 1] = f.get(1, 2 * n - 1, 1, l, r).nd;
            f.update(1, 2 * n - 1, 1, a[2 * i - 1], 2 * n);
            u = lower_bound(c1.begin(), c1.end(), b[i - 1]) - c1.begin(); v = lower_bound(c2.begin(), c2.end(), b[i - 1]) - c2.begin();
            if (c1[u] == b[i - 1])
                u ++;
            l = c2[v] + 1; r = c1[u] - 1;
            if (l <= b[i - 1])
                l = b[i - 1] + 1;
            a[2 * i - 2] = f.get(1, 2 * n - 1, 1, l, r).nd;
            f.update(1, 2 * n - 1, 1, a[2 * i - 2], 2 * n);
        }
        else if (b[i] < b[i - 1]) {
            int u = lower_bound(c1.begin(), c1.end(), b[i]) - c1.begin(), v = lower_bound(c2.begin(), c2.end(), b[i]) - c2.begin();
            if (c2[v] == b[i])
                v --;
            int l = c2[v] + 1, r = c1[u] - 1;
            if (r >= b[i])
                r = b[i] - 1;
            if (vis[b[i]])
                a[2 * i - 1] = f.get(1, 2 * n - 1, 1, l, r).nd;
            else
                a[2 * i - 1] = b[i];
            f.update(1, 2 * n - 1, 1, a[2 * i - 1], 2 * n);
            a[2 * i - 2] = f.get(1, 2 * n - 1, 1, l, r).nd;
            f.update(1, 2 * n - 1, 1, a[2 * i - 2], 2 * n);
        }
        else {
            int u = lower_bound(c1.begin(), c1.end(), b[i]) - c1.begin(), v = lower_bound(c2.begin(), c2.end(), b[i]) - c2.begin();
            if (c1[u] == b[i])
                u ++;
            int l = c2[v] + 1, r = c1[u] - 1;
            if (l <= b[i])
                l = b[i] + 1;
            if (vis[b[i]])
                a[2 * i - 1] = f.get(1, 2 * n - 1, 1, l, r).nd;
            else
                a[2 * i - 1] = b[i];
            f.update(1, 2 * n - 1, 1, a[2 * i - 1], 2 * n);
            a[2 * i - 2] = f.get(1, 2 * n - 1, 1, l, r).nd;
            f.update(1, 2 * n - 1, 1, a[2 * i - 2], 2 * n);
        }
        vis[a[2 * i - 1]] = vis[a[2 * i - 2]] = 1;
    }
    for (int i = 1; i < 2 * n; i ++)
        cout << a[i] << ' ';
    return 0;
}

Compilation message

medians.cpp: In function 'int main()':
medians.cpp:49:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |         freopen (TASKNAME".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
medians.cpp:50:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |         freopen (TASKNAME".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 352 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 860 KB Output is correct
2 Correct 4 ms 1372 KB Output is correct
3 Correct 7 ms 2140 KB Output is correct
4 Correct 14 ms 3932 KB Output is correct
5 Correct 31 ms 7804 KB Output is correct
6 Correct 56 ms 15284 KB Output is correct
7 Correct 89 ms 23876 KB Output is correct