Submission #168369

# Submission time Handle Problem Language Result Execution time Memory
168369 2019-12-12T19:35:27 Z rzbt Vještica (COCI16_vjestica) C++14
48 / 160
2000 ms 1272 KB
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define MAXN 17
typedef long long ll;


using namespace std;

int n;
char s[1000006];

int svi[MAXN][28];
int dp[(1<<MAXN)+5];
vector<int> tren;

int main()
{
    scanf("%d", &n);
    for(int i=0;i<n;i++){
        scanf("%s",s);
        int len=strlen(s);
        for(int j=0;j<len;j++)
            svi[i][s[j]-'a']++;

        dp[1<<i]=len;
    }
    for(int i=1;i<(1<<n);i++){
        if(__builtin_popcount(i)==1)continue;
        dp[i]=INT_MAX;
        tren.clear();
        int zajed=0,kolko=0;
        for(int j=0;j<n;j++){
            if((1<<j)&i)tren.pb(j);
        }
        kolko=(1<<tren.size())-1;
        for(int j=0;j<26;j++){
            int kek=22;
            for(auto x:tren)kek=min(kek,svi[x][j]);
            zajed+=kek;
        }
        for(int j=1;j<kolko;j++){
            int prvi=0,drugi=0;
            for(int k=0;k<tren.size();k++){
                if(j&(1<<k))prvi|=1<<tren[k];
                else drugi |= 1<<tren[k];
            }
            //printf("    %d  %d     %d        %d\n",prvi,drugi,j,i);
            dp[i]=min(dp[i],dp[prvi]+dp[drugi]-zajed);
        }

    }
    printf("%d",1+dp[(1<<n)-1]);

    return 0;
}

Compilation message

vjestica.cpp: In function 'int main()':
vjestica.cpp:47:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for(int k=0;k<tren.size();k++){
                         ~^~~~~~~~~~~~
vjestica.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
vjestica.cpp:24:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%s",s);
         ~~~~~^~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 4 ms 376 KB Output is correct
4 Execution timed out 2065 ms 796 KB Time limit exceeded
5 Execution timed out 2064 ms 768 KB Time limit exceeded
6 Execution timed out 2085 ms 1016 KB Time limit exceeded
7 Execution timed out 2085 ms 1160 KB Time limit exceeded
8 Execution timed out 2082 ms 1272 KB Time limit exceeded
9 Execution timed out 2066 ms 1264 KB Time limit exceeded
10 Execution timed out 2078 ms 1220 KB Time limit exceeded