Submission #107622

# Submission time Handle Problem Language Result Execution time Memory
107622 2019-04-25T10:21:32 Z Milki Vještica (COCI16_vjestica) C++14
48 / 160
2000 ms 1152 KB
#include<bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<
#define sz(x) ((int) x.size())
#define pb(x) push_back(x)
#define TRACE(x) cerr << #x << " = " << x << endl

typedef long long ll;
typedef pair<int, int> point;

const int MAXN = 17, MAX = 1 << 17, ABC = 27, inf = 1e9;

int n;
int dp[MAX], let[MAXN][ABC];
bool bio[MAX];
vector <int> visited, tmp;

int get(int mask){
  int tmp[26];
  REP(i, 26)
    tmp[i] = inf;
  REP(i, n){
    if( (mask & (1 << i)) == 0) continue;
    REP(j, 26)
      tmp[j] = min(tmp[j], let[i][j]);
  }
  int ret = 0;
  REP(i, 26)
    ret += tmp[i];
  return ret;
}

void generiraj(int mask){

  if(bio[mask]) return;
  bio[mask] = true;
  visited.pb(mask);

  REP(i, sz(tmp)){
    if( (mask & (1 << tmp[i])) == 0) continue;
    int newMask = (mask ^ (1 << tmp[i]));
    if(newMask == 0) continue;

    generiraj(newMask);
  }
}

int rek(int mask){
  if(dp[mask]) return dp[mask];
  if(__builtin_popcount(mask) == 1) return dp[mask] = get(mask);
  tmp.clear();
  visited.clear();

  int sub = get(mask), gen = 0;
  REP(i, n){
    if((1 << i) & mask){
      tmp.pb(i);
      gen |= (1 << i);
    }
  }
  int ret = inf;

  generiraj( gen );

  for(auto it : visited)
    bio[it] = false;

  for(auto it : visited){
    if( (it ^ mask) == 0) continue;
    ret = min(ret, rek(it) + rek(it ^ mask) - sub);
  }
  return dp[mask] = ret;
}

int main(){
  ios_base::sync_with_stdio(false); cin.tie(0);

  cin >> n;
  REP(i, n){
    string s; cin >> s;
    REP(j, sz(s))
      let[i][s[j] - 'a'] ++;
  }
  cout << rek((1 << n) - 1) + 1;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 384 KB Output is correct
2 Correct 6 ms 512 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Execution timed out 2055 ms 1152 KB Time limit exceeded
5 Execution timed out 2033 ms 1024 KB Time limit exceeded
6 Execution timed out 2061 ms 1152 KB Time limit exceeded
7 Execution timed out 2037 ms 1016 KB Time limit exceeded
8 Execution timed out 2035 ms 1140 KB Time limit exceeded
9 Execution timed out 2075 ms 1140 KB Time limit exceeded
10 Execution timed out 2027 ms 1012 KB Time limit exceeded