Submission #1093950

# Submission time Handle Problem Language Result Execution time Memory
1093950 2024-09-28T06:36:23 Z m5588ohammed Vještica (COCI16_vjestica) C++14
160 / 160
1434 ms 17984 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 1112 KB Output is correct
2 Correct 2 ms 1248 KB Output is correct
3 Correct 3 ms 1116 KB Output is correct
4 Correct 1413 ms 17392 KB Output is correct
5 Correct 1374 ms 17240 KB Output is correct
6 Correct 1397 ms 17496 KB Output is correct
7 Correct 1373 ms 17984 KB Output is correct
8 Correct 1407 ms 17752 KB Output is correct
9 Correct 1434 ms 17876 KB Output is correct
10 Correct 1387 ms 17752 KB Output is correct