Submission #228302

# Submission time Handle Problem Language Result Execution time Memory
228302 2020-04-30T12:25:32 Z Ruxandra985 Snake Escaping (JOI18_snake_escaping) C++14
0 / 100
6 ms 384 KB
#include <bits/stdc++.h>
#define DIMN 2000000
using namespace std;
int sol , nr;
int n;
char s[30];
char v[2000000];
int bits[DIMN] , dp0[DIMN] , dp1[DIMN];

int calcul (int x){
    int nr = 0;

    while (x){
        nr += (x % 2);
        x /= 2;
    }

    return nr;


}

int main()
{
    FILE *fin = stdin;
    FILE *fout = stdout;
    int q , i , c0 , c1 , c2 , nr0 , nr1 , nr2 , j , mask , stot = 0;
    fscanf (fin,"%d%d\n",&n,&q);

    fgets (v , (1 << n) + 10 , fin);

    /// precalculari ???

    /// cum il fac pe dp0

    for (i = 0 ; i < (1 << n) ; i++){

        bits[i] = calcul(i);

        dp1[i] = dp0[i] = v[i] - '0';

        stot += v[i] - '0';

    }

    for (i = 0 ; i < n ; i++){
        for (j = 0 ; j < ( 1 << n ) ; j++){

            if ((j & (1 << i)) == 0)
                dp0[j] += dp0[j ^ (1 << i)];
            else dp1[j] += dp1[j ^ (1 << i)];

        }
    }


    for (;q;q--){
        fgets (s + 1 , n + 10 , fin);

        c0 = c1 = c2 = 0;
        nr1 = nr0 = nr2 = 0;
        for (i = 1 ; i <= n ; i++){

            c0 *= 2;
            c1 *= 2;
            c2 *= 2;

            if (s[i] == '1')
                c1++ , nr1++;
            else if (s[i] == '0')
                c0++ , nr0++;
            else
                c2++ , nr2++;

            /// numar cati de 0 , cati de 1 si cati de ? sunt + unde sunt
        }

        sol = 0;

        if (nr0 == min(nr0 , min(nr1 , nr2))){ /// 0 apare de cele mai putine ori
            for ( i = c0 ; true ; i = ( (i - 1) & c0) ){

                mask = (i | c1);
                /// eu vreau ca bitii din mask sa fie FIXATI

                if (bits[i] % 2 == 1)
                    sol -= dp0[mask];
                else sol += dp0[mask];

                if (i == 0)
                    break;

            }

        }
        else if (nr1 == min(nr0 , min(nr1 , nr2))){ /// 1 apare de cele mai putine ori
            for ( i = c1 ; true ; i = ( (i - 1) & c1) ){

                mask = (i | c2);
                /// eu vreau ca bitii din mask sa fie FIXATI

                if (bits[i] % 2 == 1)
                    sol -= dp1[mask];
                else sol += dp1[mask];

                if (i == 0)
                    break;

            }
        }
        else { /// ? apare de cele mai putine ori
            /// pt cazul asta poti sa faci brut
            for ( i = c2 ; true ; i = ( (i - 1) & c2) ){

                sol += v[(c1 | c0) | i] - '0';

                /// eu vreau ca bitii din mask sa fie FIXAT

                if (i == 0)
                    break;

            }
        }

        sol = max(sol , -sol);
        fprintf (fout,"%d\n" , sol);

    }

    return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:28:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf (fin,"%d%d\n",&n,&q);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:30:11: warning: ignoring return value of 'char* fgets(char*, int, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     fgets (v , (1 << n) + 10 , fin);
     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:58:15: warning: ignoring return value of 'char* fgets(char*, int, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         fgets (s + 1 , n + 10 , fin);
         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Incorrect 5 ms 384 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Incorrect 5 ms 384 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Incorrect 5 ms 384 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Incorrect 5 ms 384 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Incorrect 5 ms 384 KB Output isn't correct
4 Halted 0 ms 0 KB -