Submission #1161725

#TimeUsernameProblemLanguageResultExecution timeMemory
1161725RiverFlowThe Big Prize (IOI17_prize)C++20
20 / 100
32 ms12148 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, 446)) {
        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];

    for(int i = 446; y>0; ){
        //tim vi tri tiep theo y thay doi
        int lb = i+1, rb=n-1, 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 == 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;
            }
        }
        i=pos;
        --y;
    }
    return 0;
}

#ifdef LOCAL
int main() {
    freopen("main.inp", "r", stdin);
    cin >> n;
    for(int i = 0; i < n; ++i) cin >> a[i];
    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...