제출 #1129163

#제출 시각아이디문제언어결과실행 시간메모리
1129163adiyerBoarding Passes (BOI22_passes)C++20
55 / 100
146 ms18424 KiB
#include <bits/stdc++.h>

#define file(s) freopen(s".in", "r", stdin); freopen(s".out", "w", stdout);
#define adiyer(); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define debug cout << "bug"; return;
#define bitcount(n) __builtin_popcountll(n)
#define all(x) x.begin(), x.end()
#define len(s) (int) s.size()
#define md ((l + r) >> 1)
#define pb push_back
#define S second
#define F first

// #define int long long

using namespace std;

typedef long long ll;
typedef long double ld;

const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
const int N = 2e5 + 11;
const int MAX = 1e6;
const int mod = 1e9 + 7;
const long long inf = 1e9 + 10;
const double eps = 1e-9;

ll n;
ll a[N], dp[(1 << 16)], pr[30][N], sf[30][N], cnt[30][30][N];

string s, t;

vector < ll > g[N];

ll f(ll x, ll pos, ll msk){
    ll ans = pos * (pos - 1) / 2 + (len(g[x]) - pos) * (len(g[x]) - pos - 1) / 2;
    for(ll y = 0; y < 15; y++)
        if(x != y && (msk >> y & 1))
            ans += cnt[x][y][pos] * 2;
    return ans; 
}

void output(){
    cin >> s, n = len(s);
    for(ll i = 0; i < n; i++) a[i] = s[i] - 'A', g[a[i]].pb(i);
    for(ll x = 0; x < 15; x++){
        for(ll y = 0; y < 15; y++){
            if(x == y) continue;
            for(ll i = 0; i < len(g[x]) + 5; i++) pr[x][i] = sf[x][i] = 0;
            for(ll i = 0, j = 0; i < len(g[x]); i++){
                while(j < len(g[y]) && g[y][j] <= g[x][i]) j++;
                pr[x][i + 1] = pr[x][i] + j;
            }
            for(ll i = len(g[x]) - 1, j = len(g[y]) - 1; i >= 0; i--){
                while(j >= 0 && g[y][j] >= g[x][i]) j--;
                sf[x][i + 1] = sf[x][i + 2] + len(g[y]) - j - 1;
            }
            for(ll i = 0; i <= len(g[x]); i++) cnt[x][y][i] = pr[x][i] + sf[x][i + 1];
        }
    }
    for(ll msk = 1; msk < (1 << 15); msk++){
        dp[msk] = inf;
        for(ll i = 0; i < 15; i++){
            if((msk >> i & 1)){
                ll l = 0, r = len(g[i]);
                while(r - l > 5){
                    ll m1 = l + (r - l) / 3;
                    ll m2 = r - (r - l) / 3;
                    if(f(i, m1, msk) > f(i, m2, msk)) l = m1;
                    else r = m2;
                }
                for(ll j = l; j <= r; j++)
                   dp[msk] = min(dp[msk], dp[(msk ^ (1 << i))] + f(i, j, msk));
            }
        }
        // cout << msk << ' ' << dp[msk] << '\n';
    }
    ll ans = dp[(1 << 15) - 1];
    if(ans % 2) cout << ans / 2 << ".5";
    else cout << ans / 2; 
}

const bool cases = 0;

signed main(){
//  file("disrupt");
    adiyer();
    int tt = 1;
    if(cases) cin >> tt;
    for(int i = 1; i <= tt; i++){
		  output();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...