Submission #1360477

#TimeUsernameProblemLanguageResultExecution timeMemory
1360477baoquanStone Arranging 2 (JOI23_ho_t1)C++20
100 / 100
159 ms21504 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pa pair<int, int>
#define tup tuple<int, int, int>
#define db double
#define fi first
#define se second
#define ull unsigned long long

const int maxn = 2e5 + 4, INF = 1e17 + 4, lg = 17, base = 31, mod = 1e9 + 7;

int n;
int a[maxn];
stack <pa> st;
map <int , int> check;
vector <int> ans;

void nhap()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1 ; i <= n ; i ++)
        cin >> a[i];
}

void Solve()
{
    for (int i = 1 ; i <= n ; i ++)
    {
        int cnt = 1;
        if (check[a[i]])
        {
            while (!st.empty() && st.top().fi != a[i])
            {
                check[st.top().fi] = false;
                cnt += st.top().se;
                st.pop();
            }
        }
        check[a[i]] = true;
        st.push({a[i] , cnt});
    }
    while (!st.empty())
    {
        auto [x , cnt] = st.top();
        for (int i = 1 ; i <= cnt ; i ++)
            ans.push_back(x);
        st.pop();
    }
    reverse(ans.begin() , ans.end());
    for (int v : ans)
        cout << v << '\n';
}

signed main()
{
    nhap();
    Solve();
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...