Code Submission Evaluation System Login

CSES - HIIT Open 2018

HIIT Open 2018

Contest start:2018-05-26 11:00:00
Contest end:2018-05-26 16:00:00

Task list | Submit code | Submissions | Messages | Scoreboard | Statistics


History
2018-05-26 15:37:38
2018-05-26 14:47:44
Task:Coins
Sender:HIIT AND RUN
Submission time:2018-05-26 15:37:38
Status:READY
Result:WRONG ANSWER

Show test data

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;
            
        }
    
}