Submission #1161733

#TimeUsernameProblemLanguageResultExecution timeMemory
1161733RiverFlowThe Big Prize (IOI17_prize)C++20
98.99 / 100
24 ms12176 KiB
#include <bits/stdc++.h>

// #define LOCAL

#ifndef LOCAL
#include "prize.h"
#endif

#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())

using namespace std;

template<typename U, typename V> bool maxi(U &a, V b) {
    if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
    if (a > b) { a = b; return 1; } return 0;
}

const int N = (int)2e5 + 9;
const int mod = (int)1e9 + 7;

void prepare(); void main_code();


#ifdef LOCAL
int n, a[N];
vector<int> ask(int i) {
    int x = 0, y = 0;
    for(int j = i - 1; j >= 0; --j) x += a[i] > a[j];
    for(int j = i + 1; j < n; ++j) y += a[i] > a[j];
    return {x, y};
}
#endif

vector<int> d[N];

int find_best(int n) {
    srand(time(NULL));
    vector<int> b(n);
    iota(all(b), 0);
    random_shuffle(all(b));

    FOR(i, 0, n - 1) d[i] = {-1, -1};

    int mx = 0, luu = -1;
    FOR(i, 0, min(n-1, 475)) {
        d[i]=ask(i);
        if(d[i][0]+d[i][1]==0)return i;
        if(d[i][0]+d[i][1]>mx){
            mx=d[i][0] + d[i][1], luu=i;
        }
    }

    //chi can y ma tang len
    int x=d[luu][0], y=d[luu][1];
    // chia can
    for(int j = luu+1;j<=475;++j){
        y-=(d[j][0]+d[j][1] < d[luu][0]+d[luu][1]);
    }
    int r = min(n-1, 2*475);
    int i = 475;
//    cerr << y << nl;

    set<int> up;
    auto bs=[&](int lb,int rb){
//        cerr << lb << ' ' << rb << nl;
        int pos=-1;
        while(lb<=rb){
            int mid=(lb+rb)>>1;
            if (d[mid][0]==-1)d[mid]=ask(mid);
            int v = d[mid][0] + d[mid][1];
            if (v != mx) up.insert(mid);
            if (v == 0) return mid;
            if (v == mx) {
                if (d[mid][0] - x - (mx - (x + y)) > 0) {
                    rb=mid-1;
                }else{
                    lb=mid+1;
                }
            }else{
                pos=mid, rb=mid-1;
            }
        }
        assert(pos!=-1);
        return pos;
    };
    while (y>0){
        if (d[r][0]==-1)d[r]=ask(r);
        int v=d[r][0]+d[r][1];
        if (v != mx) up.insert(r);

        if (v == mx) {
            if (d[r][0] - x - (mx - (x + y)) > 0) {
                int L = r;
                if (sz(up) and *up.rbegin() > i){
                    L = min(L, *up.upper_bound(i));
                }
                int pos = bs(i + 1, L);
                i=pos;
                --y;
                if (i==r) r=min(n-1, r+475);
            }else{
                i = r;
                r = min(n-1, r+475);
                continue;
            }
        }else{
            int L = r;
            if (sz(up) and *up.rbegin() > i){
                L = min(L, *up.upper_bound(i));
            }
            int pos = bs(i + 1, r);
            i=pos;
            --y;
            if (i==r) r=min(n-1, r+475);
        }
        if (d[i][0] + d[i][1] == 0)
            return i;
    }
    return 0;
}

#ifdef LOCAL
int main() {
    freopen("test.txt", "r", stdin);
    cin >> n;
    for(int i = 0; i < n; ++i) cin >> a[i];
//    FOR(i, 0, n - 1) {
//        if (a[i]==5){
//            cerr<<i<<nl;
//            exit(0);
//        }
//    }
    int x = find_best(n);
    cout << x << nl;
    assert(a[x] == 1);
}

#endif


/*     Let the river flows naturally     */

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