Submission #635149

#TimeUsernameProblemLanguageResultExecution timeMemory
635149kawaii즐거운 채소 기르기 (JOI14_growing)C++17
0 / 100
15 ms9508 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second

int t, n, m, k, ans = 1e18, a[1000005], pfx[1000005], sfx[1000005], tree[1000005], mod = 1e9 + 7;
set<int> s;
map<int, int> mp;
mt19937_64 rng;

int mul(int x, int y){
    if(y == 0) return 1;
    int ans = mul(x, y / 2);
    if(y % 2 == 0) return (ans * ans) % mod;
    else return (((ans * ans) % mod) * x) % mod;
}

void update(int x, int y){
    while(x){
        tree[x] += y; x -= (x & -x);
    }
}

int get(int x){
    int res = 0;
    while(x <= n){
        res += tree[x]; x += (x & -x);
    }
    return res;
}

void solve(){
    int cnt = 0, mx = 0;
    for(auto i: s) mp[i] = ++cnt;
    for(int i = 1; i <= n; i++){
        a[i] = mp[a[i]];
        pfx[i] = pfx[i - 1] + get(a[i] + 1);
        update(a[i], 1);
    }
    memset(tree, 0, sizeof(tree));
    for(int i = n; i >= 1; i--){
        sfx[i] = sfx[i + 1] + get(a[i] + 1);
        update(a[i], 1);
    }
    for(int i = 1; i <= n; i++){
        mx = max(mx, a[i]); update(a[i], -1);
        int val = pfx[i] + sfx[i + 1] + get(mx + 1);
        ans = min(ans, val);
    }
    cout << ans << "\n";
}

signed main(){
    ios_base::sync_with_stdio(NULL); cin.tie(nullptr); cout.tie(nullptr);
    rng.seed((int)main ^ time(0));
    #ifdef Kawaii
        auto starttime = chrono::high_resolution_clock::now();
    #endif

    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i]; s.insert(a[i]);
    }
    solve();

    #ifdef Kawaii
        auto endtime = chrono::high_resolution_clock::now();
        auto duration = chrono::duration_cast<chrono::milliseconds>(endtime - starttime).count(); 
        cout << "\n=====" << "\nUsed: " << duration << " ms\n";
    #endif
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...