답안 #480742

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
480742 2021-10-18T02:37:26 Z Rainbowbunny 중앙값 배열 (balkan11_medians) C++17
100 / 100
117 ms 18428 KB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 200005;

int n;
vector <int> Ans;
int A[MAXN], ST[4 * MAXN], Lazy[4 * MAXN];
bool Used[MAXN];
vector <int> Can[MAXN];

void Push(int node, int l, int r)
{
    if(Lazy[node])
    {
        ST[node] = Lazy[node];
        if(l != r)
        {
            Lazy[node << 1] = Lazy[node];
            Lazy[node << 1 | 1] = Lazy[node];
        }
        Lazy[node] = 0;
    }
}

void Update(int node, int l, int r, int L, int R, int val)
{
    Push(node, l, r);
    if(l > R or r < L or l > r)
    {
        return;
    }
    if(L <= l and r <= R)
    {
        Lazy[node] = val;
        Push(node, l, r);
        return;
    }
    int mid = (l + r) >> 1;
    Update(node << 1, l, mid, L, R, val);
    Update(node << 1 | 1, mid + 1, r, L, R, val);
    ST[node] = max(ST[node << 1], ST[node << 1 | 1]);
}

void Build(int node, int l, int r)
{
    Push(node, l, r);
    if(l == r)
    {
        Can[ST[node]].push_back(l);
        return;
    }
    int mid = (l + r) >> 1;
    Build(node << 1, l, mid);
    Build(node << 1 | 1, mid + 1, r);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> A[i];
    }
    for(int i = 1; i < n; i++)
    {
        if(A[i] != A[i + 1])
        {
            if(A[i] < A[i + 1])
            {
                Update(1, 1, 2 * n - 1, A[i] + 1, A[i + 1] - 1, 2 * i + 2);
            }
            else
            {
                Update(1, 1, 2 * n - 1, A[i + 1] + 1, A[i] - 1, 2 * i + 2);
            }
        }
    }
    Build(1, 1, 2 * n - 1);
    set <int> S;
    for(auto x : Can[0])
    {
        S.insert(x);
    }
    assert(S.count(A[1]));
    Used[A[1]] = true;
    S.erase(A[1]);
    Ans.push_back(A[1]);
    for(int i = 2; i <= n; i++)
    {
        for(auto x : Can[2 * i])
        {
            S.insert(x);
        }
        if(A[i] == A[i - 1])
        {
            assert(S.size() >= 2);
            int id = *S.begin();
            assert(id < A[i]);
            Used[id] = true;
            Ans.push_back(id);
            S.erase(*S.begin());
            id = *prev(S.end());
            assert(id > A[i]);
            S.erase(id);
            Used[id] = true;
            Ans.push_back(id);
        }
        else
        {
            if(A[i] > A[i - 1])
            {
                int tmp = *prev(S.end());
                assert(tmp > A[i]);
                Ans.push_back(tmp);
                Used[tmp] = true;
                S.erase(tmp);
                if(Used[A[i]])
                {
                    tmp = *prev(S.end());
                }
                else
                {
                    tmp = A[i];
                    assert(S.count(A[i]));
                }
                Used[tmp] = true;
                Ans.push_back(tmp);
                S.erase(tmp);
            }
            else if(A[i] < A[i - 1])
            {
                int tmp = *S.begin();
                assert(tmp < A[i]);
                Ans.push_back(tmp);
                Used[tmp] = true;
                S.erase(tmp);
                if(Used[A[i]])
                {
                    tmp = *S.begin();
                }
                else
                {
                    tmp = A[i];
                    assert(S.count(A[i]));
                }
                Used[tmp] = true;
                Ans.push_back(tmp);
                S.erase(tmp); 
            }
        }
    }
    for(auto x : Ans)
    {
        cout << x << ' ';
    }
    cout << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4952 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 5068 KB Output is correct
12 Correct 3 ms 5068 KB Output is correct
13 Correct 4 ms 5068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 5324 KB Output is correct
2 Correct 6 ms 5580 KB Output is correct
3 Correct 10 ms 6220 KB Output is correct
4 Correct 20 ms 7236 KB Output is correct
5 Correct 38 ms 9280 KB Output is correct
6 Correct 69 ms 14244 KB Output is correct
7 Correct 117 ms 18428 KB Output is correct