제출 #1329628

#제출 시각아이디문제언어결과실행 시간메모리
1329628Valters07Stone Arranging 2 (JOI23_ho_t1)C++20
0 / 100
1 ms344 KiB
#include <bits/stdc++.h>
#define fio ios_base::sync_with_stdio(0);cin.tie(0);
#define ll long long
#define pb push_back
#define fi first
#define se second
#define en exit(0);
using namespace std;
const int N = 2e5 + 5;
const int INF = 1e9 + 5;
int a[N];
bool has_repainted(set<pair<int,int> > &s, int p)
{
    // check if position p has been repainted by another colour
    auto t = s.lower_bound({p + 1, -1});
    assert(t != s.begin());
    t--;
    int l = (*t).fi, r = (*t).se;
    assert(l <= p && p <= r);
    return (a[l] != a[p]);
}
int main()
{
    fio
//    ifstream cin("in.in");
    map<int,stack<int> > pos; // val -> {positions not checked containing this value}
    set<pair<int,int> > s; // a set of intervals {l,r} with colour equal to a[l]
    int n;
    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        cin >> a[i];
        auto &t = pos[a[i]];
        while(!t.empty() && has_repainted(s, t.top()))
            t.pop();
        int l = i, r = i;
        if(!t.empty())
        {
            l = t.top();
            // delete all intervals [l';r'], where l' >= l
            auto t = s.lower_bound({l, -1});
            while(t != s.end())
            {
                s.erase(t);
                t = s.lower_bound({l, -1});
            }
        }
        s.insert({l, r});
        t.push(i);
    }
    for(auto x : s)
        for(int i = x.fi;i <= x.se;i++)
            cout << a[x.fi] << " ";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...