Submission #986437

# Submission time Handle Problem Language Result Execution time Memory
986437 2024-05-20T14:11:18 Z kh0i Vještica (COCI16_vjestica) C++17
160 / 160
117 ms 1688 KB
#include "bits/stdc++.h"
using namespace std;

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

using ll = long long;
using pii = pair<int, int>;

#define F first
#define S second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r) {
    assert(l <= r);
    return uniform_int_distribution<ll> (l, r)(rng);
}

const int N = 16;

int n;
string s[N];
int cnt[N][26], mn[26];
int f[1 << N];

int val(int mask){
    for(int c = 0; c < 26; ++c){
        mn[c] = 1e9;
        for(int i = 0; i < n; ++i)
            if(mask & (1 << i))
                mn[c] = min(mn[c], cnt[i][c]);
    }
    return accumulate(mn, mn + 26, 0);
}

int calc(int mask){
    if(mask == 0)
        return 0;
    if(f[mask] != -1)
        return f[mask];
    int pref = val(mask);

    if(__builtin_popcount(mask) == 1)
        return f[mask] = pref;

    int res = 1e9;
    for(int nmask = (mask - 1) & mask; nmask; nmask = (nmask - 1) & mask)
        res = min(res, calc(nmask) + calc(mask ^ nmask) - pref);
    return f[mask] = res;
}

void solve(){
    cin >> n;
    for(int i = 0; i < n; ++i){
        cin >> s[i];
        for(char c : s[i])
            ++cnt[i][c - 'a'];
    }

    memset(f, -1, sizeof(f));
    cout << calc((1 << n) - 1) + 1;
}

int32_t main() {
    cin.tie(nullptr)->sync_with_stdio(0);
    #define task "troll"
    if(fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int test = 1;
//    cin >> test;
    for(int i = 1; i <= test; ++i){
//        cout << "Case #" << i << ": ";
        solve();
    }
    #ifdef LOCAL
        cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
    #endif
    return 0;
}

Compilation message

vjestica.cpp: In function 'int32_t main()':
vjestica.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
vjestica.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 105 ms 604 KB Output is correct
5 Correct 105 ms 1016 KB Output is correct
6 Correct 103 ms 1260 KB Output is correct
7 Correct 106 ms 1372 KB Output is correct
8 Correct 102 ms 1688 KB Output is correct
9 Correct 117 ms 1624 KB Output is correct
10 Correct 102 ms 1640 KB Output is correct