Submission #404396

# Submission time Handle Problem Language Result Execution time Memory
404396 2021-05-14T11:00:40 Z jenkinsser Slagalica (COCI19_slagalica2) C++17
70 / 70
77 ms 8716 KB
#include <bits/stdc++.h>
#define FOR(ii,aa,bb) for(int ii=aa;ii<bb;ii++)
#define for0(ii,bb) FOR(ii,0,bb)
#define for1(ii,bb) FOR(ii,1,bb+1)
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define st first
#define nd second
#define pii pair<int,int>
#define piii pair<int,pii>
#define pdi pair<double,int>
#define vi vector<int>
#define sp " "
#define nl "\n"
#define all(x) x.begin(),x.end()
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);
#define ll long long
#define int ll
 
using namespace std;
 
const int N = 1e5+5;
const int INF = 1e9+5;
const int mod = 1e9+7;

int n,cnt[9],tmp[9];
vector<multiset<int>> s(9);

signed main(){
    fastio()
    cin >> n;
    for(int i=1;i<=n;i++){
        int t,x;
        cin >> t >> x;
        s[t].insert(x);
        cnt[t]++;
    }
    vector<pii> ans;
    int cur;
    if(s[5].empty()){
        ans.pb({*s[6].begin(),6});
        cur=6;
    }
    else{
        ans.pb({*s[5].begin(),5});
        cur=5;
    }
    for(int i=2;i<n;i++){
        if(cur==1||cur==3||cur==5){
            if(s[3].empty()&&s[4].empty()){
                break;
            }
            if(s[3].empty()){
                ans.pb({*s[4].begin(),4});
                s[4].erase(s[4].begin());
                cur=4;
            }
            else if(s[4].empty()){
                ans.pb({*s[3].begin(),3});
                s[3].erase(s[3].begin());
                cur=3;
            }
            else{
                if(*s[3].begin()<=*s[4].begin()){
                    ans.pb({*s[3].begin(),3});
                    s[3].erase(s[3].begin());
                    cur=3;
                }
                else{
                    ans.pb({*s[4].begin(),4});
                    s[4].erase(s[4].begin());
                    cur=4;
                }
            }
        }
        else{
            if(s[1].empty()&&s[2].empty()){
               break;
            }
            if(s[1].empty()){
                ans.pb({*s[2].begin(),2});
                s[2].erase(s[2].begin());
                cur=2;
            }
            else if(s[2].empty()){
                ans.pb({*s[1].begin(),1});
                s[1].erase(s[1].begin());
                cur=1;
            }
            else{
                if(*s[1].begin()<=*s[2].begin()){
                    ans.pb({*s[1].begin(),1});
                    s[1].erase(s[1].begin());
                    cur=1;
                }
                else{
                    ans.pb({*s[2].begin(),2});
                    s[2].erase(s[2].begin());
                    cur=2;
                }
            }
        }
    }
    if(s[1].size()||s[4].size()){
        cout << -1;
        return 0;
    }
    if(s[8].empty()){
        if(cur==2||cur==4||cur==6)
            ans.pb({*s[7].begin(),7});
        else{
            cout << -1;
            return 0;
        }
    }
    else{
        if(cur==1||cur==3||cur==5)
            ans.pb({*s[8].begin(),8});
        else{
            cout << -1;
            return 0;
        }
    }
    for(auto e:ans){
        tmp[e.nd]++;
        if(s[2].size()&&tmp[1]==cnt[1]){
            while(s[2].size()){
                cout << *s[2].begin() << sp;
                s[2].erase(s[2].begin());
            }
        }
        if(s[3].size()&&tmp[4]==cnt[4]){
            while(s[3].size()){
                cout << *s[3].begin() << sp;
                s[3].erase(s[3].begin());
            }
        }
        cout << e.st << sp;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 2 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 7656 KB Output is correct
2 Correct 57 ms 7984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 7472 KB Output is correct
2 Correct 49 ms 7612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 7216 KB Output is correct
2 Correct 64 ms 7768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 6400 KB Output is correct
2 Correct 74 ms 7348 KB Output is correct
3 Correct 74 ms 8256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 7100 KB Output is correct
2 Correct 55 ms 6808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 7484 KB Output is correct
2 Correct 60 ms 6756 KB Output is correct
3 Correct 62 ms 7888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 7520 KB Output is correct
2 Correct 64 ms 7460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 8716 KB Output is correct
2 Correct 53 ms 7392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 7300 KB Output is correct
2 Correct 52 ms 7516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 8580 KB Output is correct
2 Correct 64 ms 8060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 7288 KB Output is correct
2 Correct 52 ms 7620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 7304 KB Output is correct
2 Correct 60 ms 7852 KB Output is correct