Submission #877182

# Submission time Handle Problem Language Result Execution time Memory
877182 2023-11-23T02:56:00 Z noiaint Vještica (COCI16_vjestica) C++17
160 / 160
104 ms 2352 KB
#include <bits/stdc++.h>
#define int long long

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

#define file ""

#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()

#define bit(x) (1LL << (x))
#define getbit(x, i) (((x) >> (i)) & 1)
#define popcount __builtin_popcountll

mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
    return l + rd() % (r - l + 1);
}

const int N = 16;
const int mod = (int)1e9 + 7; // 998244353;
const int lg = 25; // lg + 1
const int oo = 1e9;
const long long ooo = 1e18;

template<class X, class Y> bool mini(X &a, Y b) {
    return a > b ? (a = b, true) : false;
}
template<class X, class Y> bool maxi(X &a, Y b) {
    return a < b ? (a = b, true) : false;
}
void add(int &a, int b) {
    a += b;
    if (a >= mod) a -= mod;
    if (a < 0) a += mod;
}

int n;
int f[1 << 16];
int mn[1 << 16];
int cnt[16][26];

void process() {
    memset(f, 0x3f3f3f, sizeof f);

    cin >> n;
    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;
        for (char c : s) cnt[i][c - 'a']++;
        f[bit(i)] = (int) s.size();
    }
    for (int mask = 1; mask < bit(n); ++mask) {
        for (int c = 0; c < 26; ++c) {
            int cur = oo;
            for (int i = 0; i < n; ++i) {
                if (getbit(mask, i)) mini(cur, cnt[i][c]);
            }
            mn[mask] += cur;
        }
    }

    for (int x = 0; x < bit(n); ++x) {
        int s = (bit(n) - 1) ^ x;
        for (int y = s; y; y = (y - 1) & s) {
            mini(f[x | y], f[x] + f[y] - mn[x | y]);
        }
    }

    cout << f[bit(n) - 1] + 1;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);

    int tc = 1;
    // cin >> tc;
    
    while (tc--) {
        process();
    }

    return 0;
}

/*

*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 1 ms 860 KB Output is correct
4 Correct 92 ms 1368 KB Output is correct
5 Correct 87 ms 1372 KB Output is correct
6 Correct 88 ms 1628 KB Output is correct
7 Correct 88 ms 1972 KB Output is correct
8 Correct 91 ms 2352 KB Output is correct
9 Correct 88 ms 1868 KB Output is correct
10 Correct 104 ms 1876 KB Output is correct