CSES - HIIT Open 2018 - Results
Submission details
Task:Coins
Sender:HIIT AND RUN
Submission time:2018-05-26 15:37:38
Language:C++
Status:READY
Result:
Test results
testverdicttime
#10.67 sdetails
#2ACCEPTED0.45 sdetails
#3ACCEPTED0.43 sdetails
#4ACCEPTED0.44 sdetails
#50.61 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:62:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (isojaMatchaavia >= from->size()) ans.push_back(to == &r ? "<" : ">");
             ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~

Code


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    int n;
    cin >> n;
    
    set<ll> l;
    set<ll> r;
    
    vector<ll> backtrackActions;
    
    for (int i=0; i< n; i++) {
        ll coin;
        cin >> coin;
        ll leftOrRight;
        cin >> leftOrRight;
        
        backtrackActions.push_back(coin);
        backtrackActions.push_back(leftOrRight);
        
        if (leftOrRight == 1) l.insert(coin);
        else r.insert(coin);
    }
    
    
    
    auto to = &r;
    auto from = &l;
    
    if (!l.empty()) {
        if (r.empty() || *l.rbegin() > *r.rbegin()) {
            to = &l;
            from = &r;
            
            
        }
        
    }
    
    ll largest = *to->rbegin();
    
    
    vector<string> ans;
    set<ll> pq;
    
    ll countRemovedSmalls = 0;
    ll i = backtrackActions.size() -1;
    
    while (true) {
        ll smallestInFrom = (from->empty() ? 0 : *from->begin());
        
        //cout << "largest" << largest << "smallest" << smallestInFrom << endl;
        //cout << "from" << from->size() << endl;
        ll countSmallerThanFromSmallest = max((ll) 0, smallestInFrom - 1 - countRemovedSmalls);
        ll isojaMatchaavia = to->size() - countSmallerThanFromSmallest;
        //cout << "isoja" << isojaMatchaavia << endl;
        
        if (isojaMatchaavia >= from->size()) ans.push_back(to == &r ? "<" : ">");
        else ans.push_back("?");

        
        if (i <= 2) break;
        ll leftOrRight = backtrackActions[i--];
        ll coin = backtrackActions[i--];
        
        
        if (coin < smallestInFrom) {
                to->erase(coin);
                countRemovedSmalls++;
        } else if (coin == smallestInFrom) {
                from->erase(coin);
                countRemovedSmalls++;
                ll newSmallest = (from->empty() ? 0 : *from->begin());
                while (!pq.empty() && *pq.begin() < newSmallest) {
                        pq.erase(pq.begin());
                        countRemovedSmalls++;
                }
        } else if (coin == largest) {
                to->erase(coin);
                largest = (to->empty() ? -1 : *to->rbegin());
                //System.out.println("largest " + largest);
                if (largest == -1 || (!from->empty() && *from->rbegin() > largest)) {
                        // swap to and from
                        auto helper = to;
                        to = from;
                        from = helper;
                        largest = *to->rbegin();
                }
                pq.insert(coin);
        } else {
                if (leftOrRight == 2) r.erase(coin);
                else l.erase(coin);
                pq.insert(coin);
        }
    }
        for (int i=ans.size()-1; i >= 0; i--) {
            cout << ans[i] << endl;
            
        }
    
}

Test details

Test 1

Verdict:

input
200000
175878 1
146174 1
4939 2
181388 1
...

correct output
>
>
>
>
>
...

user output
>
>
>
>
>
...

Test 2

Verdict: ACCEPTED

input
200000
1 2
2 1
3 2
4 1
...

correct output
<
>
<
>
<
...

user output
<
>
<
>
<
...

Test 3

Verdict: ACCEPTED

input
200000
1 1
2 1
3 1
4 1
...

correct output
>
>
>
>
>
...

user output
>
>
>
>
>
...

Test 4

Verdict: ACCEPTED

input
200000
1 1
2 1
3 1
4 1
...

correct output
>
>
>
>
>
...

user output
>
>
>
>
>
...

Test 5

Verdict:

input
200000
188909 2
58944 1
26824 1
143263 2
...

correct output
<
<
?
<
<
...

user output
<
<
?
<
<
...