Submission #1188024

#TimeUsernameProblemLanguageResultExecution timeMemory
1188024yhx3Stone Arranging 2 (JOI23_ho_t1)C++20
60 / 100
2094 ms3816 KiB
#include <iostream>
#include <bits/stdc++.h>
#include <iomanip>
#include <numeric>
 
using namespace std;
 
#define ll long long
 
bool comp(const pair<ll,ll> &a,pair<ll,ll> &vl)
{
    return a.second > vl.first;
}
 
bool scomp(const pair<ll,ll> &a,pair<ll,ll> &vl)
{
    return a.first >= vl.first;
}
 

void solve() {
    ll n;
    cin >> n;
    vector<ll> a(n);
    ll mx = 0;
    for (ll i = 0; i < n; i++)
    {
        cin >> a[i];
        mx = max(mx,a[i]);
    }
    
    vector<ll> ans(n);
    if (mx <= 2) {
        if (a[0] == 1) {
            bool ok = false;
            for (ll i = n - 1; i >= 0; i--)
            {
                if (ok) {
                    ans[i] = 1;
                    continue;
                }
                if (a[i] == 1){
                    ans[i] = 1;
                    ok = true;
                } else {
                    ans[i] = 2;
                }
            }
        } else {
            bool ok = false;
            for (ll i = n - 1; i >= 0; i--)
            {
                if (ok) {
                    ans[i] = 2;
                    continue;
                }
                if (a[i] == 2){
                    ans[i] = 2;
                    ok = true;
                } else {
                    ans[i] = 1;
                }
            }
        }
    } else {
        map<ll,ll> mp;
        for (ll i = 0; i < n; i++)
        {
            if (mp.find(a[i]) != mp.end()) {
                int idx = 0;
                for (ll j = i - 1; j > -1; j--)
                {
                    if (ans[j] != a[i]) {
                        mp.erase(a[j]);
                        ans[j] = a[i];
                    } else {
                        idx = j;
                        break;
                    }
                }
                for (ll j = 0; j < idx; j++)
                {
                    mp[ans[j]] = j;  
                }
            } 
            mp[a[i]] = i;
            ans[i] = a[i];
        }
        
    }
    for (ll i = 0; i < n; i++)
    {
        cout << ans[i] << endl;
    }
    
}

int main() {
    int t = 1;
    // cin>>t;

    while(t--) {
        solve();
    }
    return 0;

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...