Submission #938535

# Submission time Handle Problem Language Result Execution time Memory
938535 2024-03-05T09:20:28 Z LucaIlie Boarding Passes (BOI22_passes) C++17
0 / 100
0 ms 348 KB
#include <bits/stdc++.h>

using namespace std;

const int MAX_G = 15;
const int MAX_N = 1e5;
int n, g;
string s;
double dp[1 << MAX_G];
int frecv[MAX_G][MAX_N + 1];
double costPref[MAX_N + 2], costSuf[MAX_N + 2];
unordered_map<char, int> lit;

double expectedInversions( int n ) {
    return (double)n * (n - 1) / 4;
}

double solve( int c, int mask ) {
    int crt, prv;

    crt = prv = 0;
    costPref[0] = 0;
    for ( int i = 1; i <= n; i++ ) {
        costPref[i] = costPref[i - 1];
        if ( s[i] == c ) {
            crt++;
            costPref[i] += prv;
        }
        if ( (mask >> s[i]) & 1 )
            prv++;
    }
    crt = prv = 0;
    costSuf[n + 1] = 0;
    for ( int i = n; i >= 1; i-- ) {
        costSuf[i] = costSuf[i + 1];
        if ( s[i] == c ) {
            crt++;
            costSuf[i] += prv;
        }
        if ( (mask >> s[i]) & 1 )
            prv++;
    }

    double best = (double)n * n;
    double prvcost = best;
    bool cresc = false, ok = true;
    for ( int i = 0; i <= n; i++ ) {
        double cost = costPref[i] + expectedInversions( frecv[c][i] ) + costSuf[i + 1] + expectedInversions( frecv[c][n] - frecv[c][i] );
        best = min( best, cost );
        //cout << cost << " ";
        if ( cresc ) {
            if ( prvcost > cost )
                ok = false;
        } else {
            if ( prvcost < cost )
                cresc = true;
        }
    }
    //cout << "\n";
    if ( !ok )
        exit( 1 );

    return best;
}

int main() {
    cin >> s;
    n = s.size();
    s = " " + s;

    g = 0;
    for ( int i = 1; i <= n; i++ ) {
        if ( lit[s[i]] == 0 )
            lit[s[i]] = ++g;
        s[i] = lit[s[i]] - 1;
        cout << (int)s[i] << " ";
    }
    cout << "\n";

    for ( int c = 0; c < g; c++ ) {
        for ( int i = 1; i <= n; i++ )
            frecv[c][i] = frecv[c][i - 1] + (s[i] == c);
    }

    cout << fixed << setprecision( 5 );
    for ( int mask = 1; mask < (1 << g); mask++ ) {
        dp[mask] = (double)n * n;
        for ( int c = 0; c < g; c++ ) {
            if ( (mask >> c) & 1 )
                dp[mask] = min( dp[mask], dp[mask - (1 << c)] + solve( c, mask - (1 << c) ) );
        }
    }

    cout << dp[(1 << g) - 1];

    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB 1st numbers differ - expected: '100800.5000000000', found: '0.0000000000', error = '1.0000000000'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB 1st numbers differ - expected: '1.0000000000', found: '0.0000000000', error = '1.0000000000'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB 1st numbers differ - expected: '1.0000000000', found: '0.0000000000', error = '1.0000000000'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 344 KB 1st numbers differ - expected: '100800.5000000000', found: '0.0000000000', error = '1.0000000000'
2 Halted 0 ms 0 KB -