Submission #1200967

#TimeUsernameProblemLanguageResultExecution timeMemory
1200967InvMOD즐거운 채소 기르기 (JOI14_growing)C++17
100 / 100
76 ms9988 KiB
#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define eb emplace_back

#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())

template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}

#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"

using ll = long long;
using ld = long double;

template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);}

const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = numeric_limits<ll>::max() / 8;

struct BIT{
    vector<int> bit;

    BIT(int n = 0): bit(n + 1) {}

    void update(int p, int val){
        for(; p < sz(bit); p += p & -p){
            bit[p] += val;
        }
    }

    int get(int p){
        int ans = 0;
        for(; p > 0; p -= p & -p){
            ans += bit[p];
        }
        return ans;
    }

    int query(int l, int r){
        return get(r) - get(l - 1);
    }
};

/*
  just imagine that we are swapping the smallest element first.
  
  what will happen ? how many operation we have to perform ?
*/

void Main()
{
    int n; cin >> n;

    vi a(n + 1); FOR(i, 1, n) cin >> a[i];

    vi comp; FOR(i, 1, n) comp.eb(a[i]);

    comp.eb(-1); sort(all(comp)), compact(comp);
    FOR(i, 1, n) a[i] = lwrbound(comp, a[i]);

    vector<ll> pref(n + 2), suf(n + 2); BIT bit(sz(comp));
    FOR(i, 1, n){
        pref[i] = 1ll * bit.query(a[i] + 1, sz(comp));
        bit.update(a[i], 1);
    }

    bit = BIT(sz(comp));
    ROF(i, n, 1){
        suf[i] = 1ll * bit.query(a[i] + 1, sz(comp));
        bit.update(a[i], 1);
    }

    ll ans = 0;
    FOR(i, 1, n){ // go to pref or suf does not change the answer :D
        ans = ans + min(pref[i], suf[i]);
    }
    cout << ans << el;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP", "r", stdin);
        freopen(name".OUT", "w", stdout);
    }

    int t = 1; while(t--) Main();
    return 0;
}

/*
    we got suffix[i] and prefix[i] as the number of time we have to swap if we choose that direction:

    prefix[i] = count of a[j] that (1 <= j < i)(a[j] > a[i])
    suffix[i] = count of a[j] that (i < j <= n)(a[j] > a[i])

    now we can see that if (j > i)(a[j] <= a[i]) choose to swap to prefix or suffix it does not matter

    explain: if a[j] swap to suffix it does not change our answer (ofc)
             if a[j] swap to prefix it will go through a[i] and make a[i] become a[i + 1] so it does not change our answer too
    calculate prefix, suffix by BIT and get answer
*/

Compilation message (stderr)

growing.cpp: In function 'int32_t main()':
growing.cpp:103:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |         freopen(name".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
growing.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |         freopen(name".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...