답안 #947866

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
947866 2024-03-17T07:36:46 Z danikoynov Lamps (JOI19_lamps) C++14
100 / 100
65 ms 51320 KB
#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int maxn = 1e6 + 10;

int n;
string a, b;

/**
0 - no operations
1 - set 0 = 0
2 - set 1 = 1
3 - toggle = switch
4 - set 0 toggle = 1
5 - set 1 toggle = 0
*/

const int maxmask = 6;
int path[maxmask][maxmask], to[2][maxmask];
void precompute_masks()
{
    to[0][0] = 0;
    to[1][0] = 1;

    to[0][1] = 0;
    to[1][1] = 0;

    to[0][2] = 1;
    to[1][2] = 1;

    to[0][3] = 1;
    to[1][3] = 0;

    to[0][4] = 1;
    to[1][4] = 1;

    to[0][5] = 0;
    to[1][5] = 0;

    path[0][0] = 0;
    path[0][1] = 1;
    path[0][2] = 1;
    path[0][3] = 1;
    path[0][4] = 2;
    path[0][5] = 2;

    path[1][0] = 0;
    path[1][1] = 0;
    path[1][2] = 1;
    path[1][3] = 1;
    path[1][4] = 1;
    path[1][5] = 2;

    path[2][0] = 0;
    path[2][1] = 1;
    path[2][2] = 0;
    path[2][3] = 1;
    path[2][4] = 2;
    path[2][5] = 1;

    path[3][0] = 0;
    path[3][1] = 1;
    path[3][2] = 1;
    path[3][3] = 0;
    path[3][4] = 1;
    path[3][5] = 1;

    path[4][0] = 0;
    path[4][1] = 0;
    path[4][2] = 1;
    path[4][3] = 0;
    path[4][4] = 0;
    path[4][5] = 1;

    path[5][0] = 0;
    path[5][1] = 1;
    path[5][2] = 0;
    path[5][3] = 0;
    path[5][4] = 1;
    path[5][5] = 0;
}

int dp[maxn][maxmask], f[maxn][maxmask];
void solve()
{
    cin >> n >> a >> b;

    precompute_masks();
    a = "#" + a;
    b = "#" + b;

    for (int i = 0; i <= n; i ++)
    {
        for (int mask = 0; mask < maxmask; mask ++)
            dp[i][mask] = 1e9, f[i][mask] = -1;
    }

    dp[0][0] = 0;
    for (int i = 1; i <= n; i ++)
    {
        for (int mask = 0; mask < maxmask; mask ++)
        {
            if (to[a[i] - '0'][mask] != b[i] - '0')
                continue;

            for (int from = 0; from < maxmask; from ++)
            {
                int val = dp[i - 1][from] + path[from][mask];
                if (val < dp[i][mask])
                {
                    dp[i][mask] = val;
                    f[i][mask] = from;
                }
            }
        }
        /**cout << "--------------------" << endl;
        for (int mask = 0; mask < maxmask; mask ++)
        {
            cout << dp[i][mask] << " ";
        }
        cout << endl;

                for (int mask = 0; mask < maxmask; mask ++)
        {
            cout << f[i][mask] << " ";
        }
        cout << endl;*/
    }

    int ans = 1e9;
    for (int mask = 0; mask < maxmask; mask ++)
        ans = min(ans, dp[n][mask]);

    cout << ans << endl;

}

int main()
{
    speed();
    int t = 1;
    ///cin >> t;
    while(t --)
        solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 460 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 356 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 352 KB Output is correct
18 Correct 1 ms 352 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 460 KB Output is correct
27 Correct 0 ms 464 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 352 KB Output is correct
34 Correct 0 ms 352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 460 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 356 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 352 KB Output is correct
18 Correct 1 ms 352 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 460 KB Output is correct
27 Correct 0 ms 464 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 352 KB Output is correct
34 Correct 0 ms 352 KB Output is correct
35 Correct 1 ms 356 KB Output is correct
36 Correct 0 ms 360 KB Output is correct
37 Correct 1 ms 352 KB Output is correct
38 Correct 0 ms 360 KB Output is correct
39 Correct 0 ms 352 KB Output is correct
40 Correct 0 ms 352 KB Output is correct
41 Correct 0 ms 352 KB Output is correct
42 Correct 1 ms 352 KB Output is correct
43 Correct 1 ms 352 KB Output is correct
44 Correct 1 ms 464 KB Output is correct
45 Correct 1 ms 352 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 0 ms 472 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 0 ms 352 KB Output is correct
50 Correct 0 ms 352 KB Output is correct
51 Correct 1 ms 352 KB Output is correct
52 Correct 1 ms 472 KB Output is correct
53 Correct 1 ms 360 KB Output is correct
54 Correct 1 ms 352 KB Output is correct
55 Correct 1 ms 360 KB Output is correct
56 Correct 0 ms 352 KB Output is correct
57 Correct 0 ms 352 KB Output is correct
58 Correct 0 ms 480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 352 KB Output is correct
2 Correct 0 ms 352 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 356 KB Output is correct
5 Correct 0 ms 356 KB Output is correct
6 Correct 1 ms 352 KB Output is correct
7 Correct 51 ms 51296 KB Output is correct
8 Correct 54 ms 51196 KB Output is correct
9 Correct 48 ms 51096 KB Output is correct
10 Correct 47 ms 51200 KB Output is correct
11 Correct 47 ms 51196 KB Output is correct
12 Correct 52 ms 51192 KB Output is correct
13 Correct 46 ms 51200 KB Output is correct
14 Correct 46 ms 51200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 460 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 1 ms 356 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 352 KB Output is correct
18 Correct 1 ms 352 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 460 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 460 KB Output is correct
27 Correct 0 ms 464 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 352 KB Output is correct
34 Correct 0 ms 352 KB Output is correct
35 Correct 1 ms 356 KB Output is correct
36 Correct 0 ms 360 KB Output is correct
37 Correct 1 ms 352 KB Output is correct
38 Correct 0 ms 360 KB Output is correct
39 Correct 0 ms 352 KB Output is correct
40 Correct 0 ms 352 KB Output is correct
41 Correct 0 ms 352 KB Output is correct
42 Correct 1 ms 352 KB Output is correct
43 Correct 1 ms 352 KB Output is correct
44 Correct 1 ms 464 KB Output is correct
45 Correct 1 ms 352 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 0 ms 472 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 0 ms 352 KB Output is correct
50 Correct 0 ms 352 KB Output is correct
51 Correct 1 ms 352 KB Output is correct
52 Correct 1 ms 472 KB Output is correct
53 Correct 1 ms 360 KB Output is correct
54 Correct 1 ms 352 KB Output is correct
55 Correct 1 ms 360 KB Output is correct
56 Correct 0 ms 352 KB Output is correct
57 Correct 0 ms 352 KB Output is correct
58 Correct 0 ms 480 KB Output is correct
59 Correct 0 ms 352 KB Output is correct
60 Correct 0 ms 352 KB Output is correct
61 Correct 0 ms 348 KB Output is correct
62 Correct 0 ms 356 KB Output is correct
63 Correct 0 ms 356 KB Output is correct
64 Correct 1 ms 352 KB Output is correct
65 Correct 51 ms 51296 KB Output is correct
66 Correct 54 ms 51196 KB Output is correct
67 Correct 48 ms 51096 KB Output is correct
68 Correct 47 ms 51200 KB Output is correct
69 Correct 47 ms 51196 KB Output is correct
70 Correct 52 ms 51192 KB Output is correct
71 Correct 46 ms 51200 KB Output is correct
72 Correct 46 ms 51200 KB Output is correct
73 Correct 46 ms 51204 KB Output is correct
74 Correct 47 ms 51192 KB Output is correct
75 Correct 46 ms 51256 KB Output is correct
76 Correct 45 ms 51200 KB Output is correct
77 Correct 45 ms 51132 KB Output is correct
78 Correct 46 ms 51196 KB Output is correct
79 Correct 43 ms 51220 KB Output is correct
80 Correct 43 ms 51196 KB Output is correct
81 Correct 45 ms 51228 KB Output is correct
82 Correct 44 ms 51192 KB Output is correct
83 Correct 54 ms 51196 KB Output is correct
84 Correct 54 ms 51200 KB Output is correct
85 Correct 53 ms 51192 KB Output is correct
86 Correct 56 ms 51260 KB Output is correct
87 Correct 64 ms 51196 KB Output is correct
88 Correct 65 ms 51200 KB Output is correct
89 Correct 54 ms 51196 KB Output is correct
90 Correct 55 ms 51244 KB Output is correct
91 Correct 57 ms 51200 KB Output is correct
92 Correct 65 ms 51192 KB Output is correct
93 Correct 61 ms 51264 KB Output is correct
94 Correct 61 ms 51192 KB Output is correct
95 Correct 63 ms 51196 KB Output is correct
96 Correct 60 ms 51200 KB Output is correct
97 Correct 61 ms 51320 KB Output is correct
98 Correct 60 ms 51196 KB Output is correct
99 Correct 61 ms 51204 KB Output is correct
100 Correct 61 ms 51200 KB Output is correct
101 Correct 64 ms 51208 KB Output is correct
102 Correct 61 ms 51208 KB Output is correct