제출 #1281778

#제출 시각아이디문제언어결과실행 시간메모리
1281778hynmjStone Arranging 2 (JOI23_ho_t1)C++20
35 / 100
20 ms8836 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;
const long long N = 2e5 + 5;
pair<int, int> a[N];
int last[N];
int idx[N];

void solve()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i].first;
        a[i].second = i;
    }
    vector<pair<int, int>> stak;

    for (int i = 1; i <= n; i++)
    {
        if (last[a[i].first] != 0)
        {
            while (stak.size())
            {
                pair<int, int> sp = stak.back();
                if (sp.second <= last[a[i].first])
                {
                    break;
                }
                else
                {
                    last[sp.first] = idx[sp.second];

                    // cout << "lastof " << sp.first << " assigned to " << idx[sp.second] << endl;
                    stak.pop_back();
                }
            }
        }
        stak.push_back(a[i]);
        idx[i] = last[a[i].first];
        last[a[i].first] = i;
        // for (int i = 1; i <= n; i++)
        // {
        //     cerr << idx[i] << " ";
        // }
        // cerr << endl;
        // for (int i = 1; i <= n; i++)
        // {
        //     cerr << last[i] << " ";
        // }
        // cerr << endl;
    }

    // vector<int> ans(n+1);
    int j = 0;
    for (int i = 1; i <= n; i++)
    {
        while (j < stak.size() - 1 and stak[j].second < i)
        {
            j++;
        }
        cout << stak[j].first << " ";
    }
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    int t = 1;
    // cin >> t;
    for (int i = 1; i <= t; i++)
    {
        // cout << "Case #" << i << ':' << ' ';
        solve();
        cout << endl;
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...