Submission #1187096

#TimeUsernameProblemLanguageResultExecution timeMemory
1187096DanielPr8Boarding Passes (BOI22_passes)C++20
25 / 100
90 ms1096 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvl = vector<vll>;
using pll = pair<ll,ll>;
using vpl = vector<pll>;
using vvp = vector<vpl>;
using pd = pair<double, double>;
using vd = vector<vector<pd>>;
#define f first
#define s second
#define pb push_back
#define all(v) v.begin(),v.end()
struct seg{
    ll n;
    vll bl;
    seg(ll n):n(n){
        bl = vll(n*2);
    }
    void upd(ll i, ll x){
        i+=n;
        bl[i]+=x;
        for(i/=2;i>0;i/=2){
            bl[i] = bl[i*2]+bl[i*2+1];
        }
    }
    ll que(ll a, ll b){
        ll ans = 0;
        for(a+=n,b+=n;a<=b;a/=2, b/=2){
            if(a&1)ans += bl[a++];
            if(!(b&1))ans += bl[b--];
        }
        return ans;
    }
};
int main(){
    string s;
    cin >> s;
    ll n = s.size();
    vll ch(26,-1);
    ll m = 0;
    for(char i:s){
        if(ch[i-'A']==-1){
            ch[i-'A']=m++;
        }
    }
    vvl g(m);
    for(ll i = 0; i < n; ++i){
        g[ch[s[i]-'A']].pb(i);
    }
    ll N = 1 << (32-__builtin_clz(n-1));
    seg *S = new seg(N);
    vd tb(m, vector<pd>(n));
    for(ll i = 0; i < m; ++i){
        for(ll h: g[i])S->upd(h, 1);
        for(ll j = 0; j < m; ++j){
            if(i!=j){
                for(ll h:g[j]){
                    tb[i][h].f += S->que(0, h-1);
                    tb[i][h].s += S->que(h+1, n-1);
                }
            }
            else{
                for(ll h:g[j]){
                    tb[i][h].f += double(S->que(0, h-1))/2;
                    tb[i][h].s += double(S->que(h+1, n-1))/2;
                }
            }
        }
        for(ll h: g[i])S->upd(h, -1);
    }
    double ans = 1e11;
    vll ord(m);
    iota(all(ord),0ll);
    for(ll i = 0; i < 5050; ++i){
        double cur=0;
        for(ll j = 0; j < m; ++j){
            for(ll k:g[ord[j]]){
                pd tr={0,0};
                for(ll h = 0; h <= j; ++h){
                    tr.f += tb[ord[h]][k].f;
                    tr.s += tb[ord[h]][k].s;
                }
                cur += min(tr.f, tr.s);
            }
        }
        ans = min(ans, cur);
        next_permutation(all(ord));
    }
    cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...