Submission #1271190

#TimeUsernameProblemLanguageResultExecution timeMemory
1271190hoangtien69Stone Arranging 2 (JOI23_ho_t1)C++20
100 / 100
171 ms45916 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int N;
    cin >> N;
    vector<ll> A(N+1);
    for(int i = 1; i <= N; i++)
    {
        cin >> A[i];
    }
    map<int, pair<int,ll>> segments;
    unordered_map<ll, set<int>> colorSegments;
    segments.clear();
    colorSegments.clear();
    for(int i = 1; i <= N; i++)
    {
        ll col = A[i];
        if(i == 1)
        {

            segments[1] = {1, col};
            colorSegments[col].insert(1);
        }
        else
        {
            if(colorSegments[col].empty())
            {
                segments[i] = {i, col};
                colorSegments[col].insert(i);
            }
            else
            {
                auto itColor = colorSegments[col].end();
                --itColor;
                int lastStart = *itColor;
                int lastEnd = segments[lastStart].first;
                int j = lastEnd;
                if(j == i-1)
                {
                    segments[lastStart].first = i;
                }
                else
                {
                    auto it = segments.lower_bound(j+1);
                    vector<int> toDelete;
                    while(it != segments.end() && it->first <= i-1)
                    {
                        toDelete.push_back(it->first);
                        ++it;
                    }
                    for(int start : toDelete)
                    {
                        ll c2 = segments[start].second;
                        segments.erase(start);
                        colorSegments[c2].erase(start);
                    }
                    segments[j+1] = {i, col};
                    colorSegments[col].insert(j+1);
                }
            }
        }
    }
    vector<ll> ans(N+1);
    for(auto &kv : segments)
    {
        int s = kv.first;
        int e = kv.second.first;
        ll c = kv.second.second;
        for(int k = s; k <= e; k++)
        {
            ans[k] = c;
        }
    }
    for(int i = 1; i <= N; i++)
    {
        cout << ans[i] << "\n";
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...