Submission #228296

# Submission time Handle Problem Language Result Execution time Memory
228296 2020-04-30T12:21:25 Z Ruxandra985 Snake Escaping (JOI18_snake_escaping) C++14
75 / 100
2000 ms 26104 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];
void solve_brut (int poz){

    if (poz == n + 1){

        sol += v[nr] - '0';
        return;

    }

    if (s[poz] != '?'){
        nr = nr * 2 + (s[poz] - '0');
        solve_brut (poz + 1);
        nr /= 2;
    }
    else {
        nr = nr * 2;

        solve_brut(poz + 1); /// pui 0

        nr++;

        solve_brut(poz + 1); /// pui 1

        nr /= 2;
    }

}

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
            solve_brut (1);
        }

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

    }

    return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:55: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:57: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:85: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 5 ms 384 KB Output is correct
2 Correct 5 ms 512 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 512 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 206 ms 15224 KB Output is correct
12 Correct 216 ms 14712 KB Output is correct
13 Correct 292 ms 14072 KB Output is correct
14 Correct 237 ms 14328 KB Output is correct
15 Correct 211 ms 15156 KB Output is correct
16 Correct 279 ms 14360 KB Output is correct
17 Correct 282 ms 14328 KB Output is correct
18 Correct 170 ms 16120 KB Output is correct
19 Correct 256 ms 13048 KB Output is correct
20 Correct 209 ms 14840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 512 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 206 ms 15224 KB Output is correct
12 Correct 216 ms 14712 KB Output is correct
13 Correct 292 ms 14072 KB Output is correct
14 Correct 237 ms 14328 KB Output is correct
15 Correct 211 ms 15156 KB Output is correct
16 Correct 279 ms 14360 KB Output is correct
17 Correct 282 ms 14328 KB Output is correct
18 Correct 170 ms 16120 KB Output is correct
19 Correct 256 ms 13048 KB Output is correct
20 Correct 209 ms 14840 KB Output is correct
21 Correct 256 ms 18192 KB Output is correct
22 Correct 240 ms 18336 KB Output is correct
23 Correct 409 ms 17400 KB Output is correct
24 Correct 289 ms 17144 KB Output is correct
25 Correct 232 ms 19192 KB Output is correct
26 Correct 372 ms 17784 KB Output is correct
27 Correct 355 ms 17660 KB Output is correct
28 Correct 179 ms 20088 KB Output is correct
29 Correct 294 ms 16120 KB Output is correct
30 Correct 233 ms 18296 KB Output is correct
31 Correct 264 ms 18156 KB Output is correct
32 Correct 288 ms 18296 KB Output is correct
33 Correct 339 ms 17016 KB Output is correct
34 Correct 296 ms 17316 KB Output is correct
35 Correct 356 ms 17656 KB Output is correct
36 Correct 164 ms 16120 KB Output is correct
37 Correct 219 ms 18168 KB Output is correct
38 Correct 328 ms 16192 KB Output is correct
39 Correct 426 ms 17528 KB Output is correct
40 Correct 298 ms 17144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 512 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 76 ms 16120 KB Output is correct
12 Correct 76 ms 15992 KB Output is correct
13 Correct 101 ms 15992 KB Output is correct
14 Correct 94 ms 15932 KB Output is correct
15 Correct 76 ms 15992 KB Output is correct
16 Correct 109 ms 16120 KB Output is correct
17 Correct 112 ms 15992 KB Output is correct
18 Correct 70 ms 16120 KB Output is correct
19 Correct 86 ms 15864 KB Output is correct
20 Correct 78 ms 15992 KB Output is correct
21 Correct 84 ms 15992 KB Output is correct
22 Correct 92 ms 15992 KB Output is correct
23 Correct 86 ms 15864 KB Output is correct
24 Correct 129 ms 15992 KB Output is correct
25 Correct 109 ms 15992 KB Output is correct
26 Correct 67 ms 15864 KB Output is correct
27 Correct 75 ms 15992 KB Output is correct
28 Correct 80 ms 15864 KB Output is correct
29 Correct 97 ms 15864 KB Output is correct
30 Correct 212 ms 15992 KB Output is correct
31 Correct 87 ms 15996 KB Output is correct
32 Correct 112 ms 15992 KB Output is correct
33 Correct 115 ms 15992 KB Output is correct
34 Correct 73 ms 15864 KB Output is correct
35 Correct 101 ms 15992 KB Output is correct
36 Correct 104 ms 15992 KB Output is correct
37 Correct 100 ms 15992 KB Output is correct
38 Correct 102 ms 15992 KB Output is correct
39 Correct 99 ms 15992 KB Output is correct
40 Correct 102 ms 15992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 512 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 206 ms 15224 KB Output is correct
12 Correct 216 ms 14712 KB Output is correct
13 Correct 292 ms 14072 KB Output is correct
14 Correct 237 ms 14328 KB Output is correct
15 Correct 211 ms 15156 KB Output is correct
16 Correct 279 ms 14360 KB Output is correct
17 Correct 282 ms 14328 KB Output is correct
18 Correct 170 ms 16120 KB Output is correct
19 Correct 256 ms 13048 KB Output is correct
20 Correct 209 ms 14840 KB Output is correct
21 Correct 256 ms 18192 KB Output is correct
22 Correct 240 ms 18336 KB Output is correct
23 Correct 409 ms 17400 KB Output is correct
24 Correct 289 ms 17144 KB Output is correct
25 Correct 232 ms 19192 KB Output is correct
26 Correct 372 ms 17784 KB Output is correct
27 Correct 355 ms 17660 KB Output is correct
28 Correct 179 ms 20088 KB Output is correct
29 Correct 294 ms 16120 KB Output is correct
30 Correct 233 ms 18296 KB Output is correct
31 Correct 264 ms 18156 KB Output is correct
32 Correct 288 ms 18296 KB Output is correct
33 Correct 339 ms 17016 KB Output is correct
34 Correct 296 ms 17316 KB Output is correct
35 Correct 356 ms 17656 KB Output is correct
36 Correct 164 ms 16120 KB Output is correct
37 Correct 219 ms 18168 KB Output is correct
38 Correct 328 ms 16192 KB Output is correct
39 Correct 426 ms 17528 KB Output is correct
40 Correct 298 ms 17144 KB Output is correct
41 Correct 76 ms 16120 KB Output is correct
42 Correct 76 ms 15992 KB Output is correct
43 Correct 101 ms 15992 KB Output is correct
44 Correct 94 ms 15932 KB Output is correct
45 Correct 76 ms 15992 KB Output is correct
46 Correct 109 ms 16120 KB Output is correct
47 Correct 112 ms 15992 KB Output is correct
48 Correct 70 ms 16120 KB Output is correct
49 Correct 86 ms 15864 KB Output is correct
50 Correct 78 ms 15992 KB Output is correct
51 Correct 84 ms 15992 KB Output is correct
52 Correct 92 ms 15992 KB Output is correct
53 Correct 86 ms 15864 KB Output is correct
54 Correct 129 ms 15992 KB Output is correct
55 Correct 109 ms 15992 KB Output is correct
56 Correct 67 ms 15864 KB Output is correct
57 Correct 75 ms 15992 KB Output is correct
58 Correct 80 ms 15864 KB Output is correct
59 Correct 97 ms 15864 KB Output is correct
60 Correct 212 ms 15992 KB Output is correct
61 Correct 87 ms 15996 KB Output is correct
62 Correct 112 ms 15992 KB Output is correct
63 Correct 115 ms 15992 KB Output is correct
64 Correct 73 ms 15864 KB Output is correct
65 Correct 101 ms 15992 KB Output is correct
66 Correct 104 ms 15992 KB Output is correct
67 Correct 100 ms 15992 KB Output is correct
68 Correct 102 ms 15992 KB Output is correct
69 Correct 99 ms 15992 KB Output is correct
70 Correct 102 ms 15992 KB Output is correct
71 Correct 375 ms 22776 KB Output is correct
72 Correct 386 ms 23032 KB Output is correct
73 Correct 782 ms 21496 KB Output is correct
74 Correct 706 ms 21880 KB Output is correct
75 Correct 370 ms 24052 KB Output is correct
76 Correct 1091 ms 22380 KB Output is correct
77 Correct 1063 ms 22116 KB Output is correct
78 Correct 249 ms 26104 KB Output is correct
79 Correct 465 ms 19832 KB Output is correct
80 Correct 391 ms 23160 KB Output is correct
81 Correct 555 ms 22904 KB Output is correct
82 Correct 803 ms 21976 KB Output is correct
83 Correct 623 ms 20984 KB Output is correct
84 Correct 1374 ms 22008 KB Output is correct
85 Correct 1098 ms 22140 KB Output is correct
86 Correct 229 ms 19832 KB Output is correct
87 Correct 369 ms 22904 KB Output is correct
88 Correct 480 ms 19832 KB Output is correct
89 Correct 778 ms 21496 KB Output is correct
90 Execution timed out 2086 ms 20600 KB Time limit exceeded
91 Halted 0 ms 0 KB -