답안 #82917

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
82917 2018-11-02T20:28:47 Z alexpetrescu Snake Escaping (JOI18_snake_escaping) C++14
12 / 100
392 ms 66560 KB
#include <cstdio>
#include <algorithm>

#define fin stdin
#define fout stdout
//FILE *fin = fopen("a.in", "r"), *fout = fopen("a.out", "w");

#define MAXN 20
#define MAXL (1 << MAXN)

int n, q, l, cnt[2], a[MAXN], v[MAXN];
int dp0[MAXL], dp1[MAXL];
char u[MAXL + 10], s[MAXN + 10];

inline void solve() {
    fgets(s, MAXN + 5, fin);
    for (int i = 0, j = n - 1; i < j; i++, j--)
        std::swap(s[i], s[j]);
    cnt[0] = cnt[1] = 0;
    for (int i = 0; i < n; i++)
        if (s[i] != '?')
            cnt[s[i] - '0']++;
    v[0] = 0;
    long long ans = 0;
    if (n - cnt[0] - cnt[1] <= cnt[0] && n - cnt[0] - cnt[1] <= cnt[1]) {
        int x = 0;
        for (int i = 0; i < n; i++) {
            if (s[i] == '?')
                v[++v[0]] = i;
            else if (s[i] == '1')
                x += 1 << i;
        }
        for (int i = 1; i <= v[0]; i++)
            a[i] = 0;
        ans = u[x] - '0';
        for (int i = 1; i < (1 << v[0]); i++) {
            int p = 1;
            while (a[p]) {
                x -= 1 << v[p];
                a[p] = 0;
                p++;
            }
            x += 1 << v[p];
            a[p] = 1;
            ans += u[x] - '0';
        }
    } else if (cnt[0] <= cnt[1]) {
        int x = 0;
        for (int i = 0; i < n; i++) {
            if (s[i] == '0')
                v[++v[0]] = i;
            else if (s[i] == '1')
                x += 1 << i;
        }
        for (int i = 0; i < n; i++)
            a[i] = 0;
        ans = dp1[x];
        int S = 1;
        for (int i = 1; i < (1 << v[0]); i++) {
            int p = 1;
            while (a[p]) {
                S = -S;
                x -= 1 << v[p];
                a[p] = 0;
                p++;
            }
            S = -S;
            x += 1 << v[p];
            a[p] = 1;
            ans += S * dp1[x];
        }
    } else {
        int x = l - 1;
        for (int i = 0; i < n; i++) {
            if (s[i] == '1')
                v[++v[0]] = i;
            else if (s[i] == '0')
                x -= 1 << i;
        }
        for (int i = 0; i < n; i++)
            a[i] = 0;
        ans = dp0[x];
        int S = 1;
        for (int i = 1; i < (1 << v[0]); i++) {
            int p = 1;
            while (a[p]) {
                S = -S;
                x += 1 << v[p];
                a[p] = 0;
                p++;
            }
            S = -S;
            x -= 1 << v[p];
            a[p] = 1;
            ans += S * dp0[x];
        }
    }
    fprintf(fout, "%lld\n", ans);
}

int main() {
    fscanf(fin, "%d%d ", &n, &q);
    fgets(u, MAXL + 5, fin);
    l = 1 << n;
    for (int i = 0; i < l; i++)
        dp0[i] = dp1[i] = u[i] - '0';

    for (int i = 0; i < n; i++)
        for (int j = 0; j < l; j++)
            if (!(j & (1 << i)))
                dp1[j] += dp1[j ^ (1 << i)];
    for (int i = 0; i < n; i++)
        for (int j = l - 1; j >= 0; j--)
            if (j & (1 << i))
                dp0[j] += dp0[j ^ (1 << i)];

    for (; q; q--)
        solve();

    fclose(fin);
    fclose(fout);
    return 0;
}

Compilation message

snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:102:11: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf(fin, "%d%d ", &n, &q);
     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~
snake_escaping.cpp:103:10: warning: ignoring return value of 'char* fgets(char*, int, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     fgets(u, MAXL + 5, fin);
     ~~~~~^~~~~~~~~~~~~~~~~~
snake_escaping.cpp: In function 'void solve()':
snake_escaping.cpp:16:10: warning: ignoring return value of 'char* fgets(char*, int, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     fgets(s, MAXN + 5, fin);
     ~~~~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 524 KB Output is correct
3 Correct 2 ms 580 KB Output is correct
4 Correct 2 ms 580 KB Output is correct
5 Correct 2 ms 676 KB Output is correct
6 Correct 2 ms 676 KB Output is correct
7 Correct 2 ms 676 KB Output is correct
8 Correct 2 ms 676 KB Output is correct
9 Correct 2 ms 676 KB Output is correct
10 Correct 2 ms 676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 524 KB Output is correct
3 Correct 2 ms 580 KB Output is correct
4 Correct 2 ms 580 KB Output is correct
5 Correct 2 ms 676 KB Output is correct
6 Correct 2 ms 676 KB Output is correct
7 Correct 2 ms 676 KB Output is correct
8 Correct 2 ms 676 KB Output is correct
9 Correct 2 ms 676 KB Output is correct
10 Correct 2 ms 676 KB Output is correct
11 Correct 231 ms 15352 KB Output is correct
12 Correct 268 ms 18668 KB Output is correct
13 Correct 245 ms 18668 KB Output is correct
14 Correct 279 ms 18668 KB Output is correct
15 Correct 255 ms 18996 KB Output is correct
16 Correct 286 ms 18996 KB Output is correct
17 Correct 271 ms 18996 KB Output is correct
18 Correct 217 ms 20188 KB Output is correct
19 Correct 182 ms 20188 KB Output is correct
20 Correct 257 ms 20188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 524 KB Output is correct
3 Correct 2 ms 580 KB Output is correct
4 Correct 2 ms 580 KB Output is correct
5 Correct 2 ms 676 KB Output is correct
6 Correct 2 ms 676 KB Output is correct
7 Correct 2 ms 676 KB Output is correct
8 Correct 2 ms 676 KB Output is correct
9 Correct 2 ms 676 KB Output is correct
10 Correct 2 ms 676 KB Output is correct
11 Correct 231 ms 15352 KB Output is correct
12 Correct 268 ms 18668 KB Output is correct
13 Correct 245 ms 18668 KB Output is correct
14 Correct 279 ms 18668 KB Output is correct
15 Correct 255 ms 18996 KB Output is correct
16 Correct 286 ms 18996 KB Output is correct
17 Correct 271 ms 18996 KB Output is correct
18 Correct 217 ms 20188 KB Output is correct
19 Correct 182 ms 20188 KB Output is correct
20 Correct 257 ms 20188 KB Output is correct
21 Correct 279 ms 20188 KB Output is correct
22 Correct 289 ms 20188 KB Output is correct
23 Correct 331 ms 20188 KB Output is correct
24 Correct 362 ms 20188 KB Output is correct
25 Correct 332 ms 20244 KB Output is correct
26 Correct 392 ms 32492 KB Output is correct
27 Correct 365 ms 46276 KB Output is correct
28 Correct 198 ms 62244 KB Output is correct
29 Runtime error 198 ms 66560 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
30 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 524 KB Output is correct
3 Correct 2 ms 580 KB Output is correct
4 Correct 2 ms 580 KB Output is correct
5 Correct 2 ms 676 KB Output is correct
6 Correct 2 ms 676 KB Output is correct
7 Correct 2 ms 676 KB Output is correct
8 Correct 2 ms 676 KB Output is correct
9 Correct 2 ms 676 KB Output is correct
10 Correct 2 ms 676 KB Output is correct
11 Runtime error 84 ms 66560 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 524 KB Output is correct
3 Correct 2 ms 580 KB Output is correct
4 Correct 2 ms 580 KB Output is correct
5 Correct 2 ms 676 KB Output is correct
6 Correct 2 ms 676 KB Output is correct
7 Correct 2 ms 676 KB Output is correct
8 Correct 2 ms 676 KB Output is correct
9 Correct 2 ms 676 KB Output is correct
10 Correct 2 ms 676 KB Output is correct
11 Correct 231 ms 15352 KB Output is correct
12 Correct 268 ms 18668 KB Output is correct
13 Correct 245 ms 18668 KB Output is correct
14 Correct 279 ms 18668 KB Output is correct
15 Correct 255 ms 18996 KB Output is correct
16 Correct 286 ms 18996 KB Output is correct
17 Correct 271 ms 18996 KB Output is correct
18 Correct 217 ms 20188 KB Output is correct
19 Correct 182 ms 20188 KB Output is correct
20 Correct 257 ms 20188 KB Output is correct
21 Correct 279 ms 20188 KB Output is correct
22 Correct 289 ms 20188 KB Output is correct
23 Correct 331 ms 20188 KB Output is correct
24 Correct 362 ms 20188 KB Output is correct
25 Correct 332 ms 20244 KB Output is correct
26 Correct 392 ms 32492 KB Output is correct
27 Correct 365 ms 46276 KB Output is correct
28 Correct 198 ms 62244 KB Output is correct
29 Runtime error 198 ms 66560 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
30 Halted 0 ms 0 KB -