Submission #228304

# Submission time Handle Problem Language Result Execution time Memory
228304 2020-04-30T12:27:09 Z Ruxandra985 Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
930 ms 23672 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 | 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 5 ms 384 KB Output is correct
2 Correct 5 ms 384 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 384 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 198 ms 6392 KB Output is correct
12 Correct 214 ms 5880 KB Output is correct
13 Correct 219 ms 5260 KB Output is correct
14 Correct 225 ms 5240 KB Output is correct
15 Correct 212 ms 6324 KB Output is correct
16 Correct 233 ms 5468 KB Output is correct
17 Correct 244 ms 5368 KB Output is correct
18 Correct 168 ms 7288 KB Output is correct
19 Correct 194 ms 4344 KB Output is correct
20 Correct 206 ms 5996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 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 198 ms 6392 KB Output is correct
12 Correct 214 ms 5880 KB Output is correct
13 Correct 219 ms 5260 KB Output is correct
14 Correct 225 ms 5240 KB Output is correct
15 Correct 212 ms 6324 KB Output is correct
16 Correct 233 ms 5468 KB Output is correct
17 Correct 244 ms 5368 KB Output is correct
18 Correct 168 ms 7288 KB Output is correct
19 Correct 194 ms 4344 KB Output is correct
20 Correct 206 ms 5996 KB Output is correct
21 Correct 215 ms 6476 KB Output is correct
22 Correct 236 ms 6520 KB Output is correct
23 Correct 284 ms 5624 KB Output is correct
24 Correct 288 ms 5368 KB Output is correct
25 Correct 232 ms 7288 KB Output is correct
26 Correct 287 ms 6012 KB Output is correct
27 Correct 288 ms 5752 KB Output is correct
28 Correct 177 ms 8312 KB Output is correct
29 Correct 210 ms 4344 KB Output is correct
30 Correct 230 ms 6520 KB Output is correct
31 Correct 256 ms 6136 KB Output is correct
32 Correct 283 ms 6136 KB Output is correct
33 Correct 242 ms 5240 KB Output is correct
34 Correct 289 ms 4984 KB Output is correct
35 Correct 287 ms 5752 KB Output is correct
36 Correct 158 ms 4088 KB Output is correct
37 Correct 217 ms 6108 KB Output is correct
38 Correct 223 ms 3960 KB Output is correct
39 Correct 271 ms 5116 KB Output is correct
40 Correct 283 ms 4728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 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 75 ms 15992 KB Output is correct
12 Correct 77 ms 15992 KB Output is correct
13 Correct 84 ms 15864 KB Output is correct
14 Correct 100 ms 15864 KB Output is correct
15 Correct 80 ms 16000 KB Output is correct
16 Correct 93 ms 15992 KB Output is correct
17 Correct 92 ms 15992 KB Output is correct
18 Correct 69 ms 16120 KB Output is correct
19 Correct 74 ms 15864 KB Output is correct
20 Correct 76 ms 16120 KB Output is correct
21 Correct 83 ms 15992 KB Output is correct
22 Correct 92 ms 15996 KB Output is correct
23 Correct 75 ms 15864 KB Output is correct
24 Correct 97 ms 15864 KB Output is correct
25 Correct 97 ms 15992 KB Output is correct
26 Correct 68 ms 15864 KB Output is correct
27 Correct 76 ms 15992 KB Output is correct
28 Correct 77 ms 15864 KB Output is correct
29 Correct 85 ms 15864 KB Output is correct
30 Correct 80 ms 15864 KB Output is correct
31 Correct 74 ms 15868 KB Output is correct
32 Correct 94 ms 15992 KB Output is correct
33 Correct 89 ms 15992 KB Output is correct
34 Correct 72 ms 15864 KB Output is correct
35 Correct 86 ms 15852 KB Output is correct
36 Correct 85 ms 15992 KB Output is correct
37 Correct 91 ms 15864 KB Output is correct
38 Correct 90 ms 15988 KB Output is correct
39 Correct 87 ms 15864 KB Output is correct
40 Correct 88 ms 15864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 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 198 ms 6392 KB Output is correct
12 Correct 214 ms 5880 KB Output is correct
13 Correct 219 ms 5260 KB Output is correct
14 Correct 225 ms 5240 KB Output is correct
15 Correct 212 ms 6324 KB Output is correct
16 Correct 233 ms 5468 KB Output is correct
17 Correct 244 ms 5368 KB Output is correct
18 Correct 168 ms 7288 KB Output is correct
19 Correct 194 ms 4344 KB Output is correct
20 Correct 206 ms 5996 KB Output is correct
21 Correct 215 ms 6476 KB Output is correct
22 Correct 236 ms 6520 KB Output is correct
23 Correct 284 ms 5624 KB Output is correct
24 Correct 288 ms 5368 KB Output is correct
25 Correct 232 ms 7288 KB Output is correct
26 Correct 287 ms 6012 KB Output is correct
27 Correct 288 ms 5752 KB Output is correct
28 Correct 177 ms 8312 KB Output is correct
29 Correct 210 ms 4344 KB Output is correct
30 Correct 230 ms 6520 KB Output is correct
31 Correct 256 ms 6136 KB Output is correct
32 Correct 283 ms 6136 KB Output is correct
33 Correct 242 ms 5240 KB Output is correct
34 Correct 289 ms 4984 KB Output is correct
35 Correct 287 ms 5752 KB Output is correct
36 Correct 158 ms 4088 KB Output is correct
37 Correct 217 ms 6108 KB Output is correct
38 Correct 223 ms 3960 KB Output is correct
39 Correct 271 ms 5116 KB Output is correct
40 Correct 283 ms 4728 KB Output is correct
41 Correct 75 ms 15992 KB Output is correct
42 Correct 77 ms 15992 KB Output is correct
43 Correct 84 ms 15864 KB Output is correct
44 Correct 100 ms 15864 KB Output is correct
45 Correct 80 ms 16000 KB Output is correct
46 Correct 93 ms 15992 KB Output is correct
47 Correct 92 ms 15992 KB Output is correct
48 Correct 69 ms 16120 KB Output is correct
49 Correct 74 ms 15864 KB Output is correct
50 Correct 76 ms 16120 KB Output is correct
51 Correct 83 ms 15992 KB Output is correct
52 Correct 92 ms 15996 KB Output is correct
53 Correct 75 ms 15864 KB Output is correct
54 Correct 97 ms 15864 KB Output is correct
55 Correct 97 ms 15992 KB Output is correct
56 Correct 68 ms 15864 KB Output is correct
57 Correct 76 ms 15992 KB Output is correct
58 Correct 77 ms 15864 KB Output is correct
59 Correct 85 ms 15864 KB Output is correct
60 Correct 80 ms 15864 KB Output is correct
61 Correct 74 ms 15868 KB Output is correct
62 Correct 94 ms 15992 KB Output is correct
63 Correct 89 ms 15992 KB Output is correct
64 Correct 72 ms 15864 KB Output is correct
65 Correct 86 ms 15852 KB Output is correct
66 Correct 85 ms 15992 KB Output is correct
67 Correct 91 ms 15864 KB Output is correct
68 Correct 90 ms 15988 KB Output is correct
69 Correct 87 ms 15864 KB Output is correct
70 Correct 88 ms 15864 KB Output is correct
71 Correct 349 ms 20856 KB Output is correct
72 Correct 369 ms 20984 KB Output is correct
73 Correct 452 ms 19704 KB Output is correct
74 Correct 687 ms 20004 KB Output is correct
75 Correct 375 ms 21752 KB Output is correct
76 Correct 791 ms 19816 KB Output is correct
77 Correct 696 ms 19832 KB Output is correct
78 Correct 255 ms 23672 KB Output is correct
79 Correct 333 ms 17528 KB Output is correct
80 Correct 369 ms 20600 KB Output is correct
81 Correct 580 ms 20784 KB Output is correct
82 Correct 861 ms 19972 KB Output is correct
83 Correct 407 ms 19580 KB Output is correct
84 Correct 907 ms 20356 KB Output is correct
85 Correct 759 ms 20592 KB Output is correct
86 Correct 247 ms 18372 KB Output is correct
87 Correct 422 ms 21344 KB Output is correct
88 Correct 367 ms 18400 KB Output is correct
89 Correct 528 ms 19996 KB Output is correct
90 Correct 485 ms 22816 KB Output is correct
91 Correct 367 ms 21124 KB Output is correct
92 Correct 930 ms 22244 KB Output is correct
93 Correct 739 ms 22212 KB Output is correct
94 Correct 255 ms 19868 KB Output is correct
95 Correct 604 ms 22136 KB Output is correct
96 Correct 601 ms 22152 KB Output is correct
97 Correct 581 ms 22176 KB Output is correct
98 Correct 598 ms 22168 KB Output is correct
99 Correct 623 ms 22136 KB Output is correct
100 Correct 636 ms 21996 KB Output is correct