Submission #1093978

# Submission time Handle Problem Language Result Execution time Memory
1093978 2024-09-28T08:04:50 Z vjudge1 Vještica (COCI16_vjestica) C++17
160 / 160
1426 ms 17924 KB
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
int common[(1<<16)+1],fre[(1<<16)+1][30],vis[(1<<16)+1],fre2[(1<<16)+1],dp[(1<<16)+1];
int n;
int count(int x){
    int cnt=0;
    for(int i=0;i<=16;i++) if((bool)((1<<i)&x)!=0) cnt++;
    return cnt;
}
void build(int x){
    if(vis[x]==1||count(x)==1) return;
    vis[x]=1;
    int num=x,j=0;
    for(int bt=0;bt<=16;bt++){ 
        if((bool)((1<<bt)&x)!=0){
            num^=(1<<bt);
            j=bt;
            break;
        }
    }
    build(num);
    int sum=0;
    for(int i=0;i<30;i++){
        fre[x][i]=min(fre[(1<<j)][i],fre[num][i]);
        sum+=fre[x][i];
    }
    common[x]=sum;
    return;
}
int solve(int x);
int rec(int x,int num,int i){
    if(i==n){
        if(x!=num&&num!=0) return solve(num)+solve(x^num)-common[x]*2+common[num]+common[x^num];
        else return 1e15;
    }
    if((bool)(x&(1<<i))==0) return rec(x,num,i+1);
    else return min(rec(x,num,i+1),rec(x,(int)(num|(1<<i)),i+1));
}
int solve(int x){
    if(count(x)==1) return 0;
    if(dp[x]!=-1) return dp[x];
    return dp[x]=rec(x,0,0);
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    memset(dp,-1,sizeof dp);
    cin>>n;
    for(int i=0;i<n;i++){
        string s;
        cin>>s;
        for(int j=0;j<s.size();j++) fre[(1<<i)][s[j]-97]++;
        common[(1<<i)]=s.size();
    }
    for(int i=1;i<=(1<<n)-1;i++) build(i);
    //cout<<(1<<n)-1<<" "<<common[6]<<endl;
    cout<<solve((1<<n)-1)+common[(1<<n)-1]+1<<endl;
    return 0;
}

Compilation message

vjestica.cpp: In function 'int main()':
vjestica.cpp:56:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |         for(int j=0;j<s.size();j++) fre[(1<<i)][s[j]-97]++;
      |                     ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1116 KB Output is correct
2 Correct 2 ms 1116 KB Output is correct
3 Correct 3 ms 1116 KB Output is correct
4 Correct 1385 ms 17244 KB Output is correct
5 Correct 1425 ms 17500 KB Output is correct
6 Correct 1403 ms 17500 KB Output is correct
7 Correct 1416 ms 17872 KB Output is correct
8 Correct 1416 ms 17924 KB Output is correct
9 Correct 1425 ms 17880 KB Output is correct
10 Correct 1426 ms 17752 KB Output is correct