Submission #233640

# Submission time Handle Problem Language Result Execution time Memory
233640 2020-05-21T08:29:26 Z Coroian_David Lamps (JOI19_lamps) C++11
100 / 100
422 ms 90588 KB
#include <bits/stdc++.h>

#define MAX_N 1000000

using namespace std;

const int sqrInf = (1e9) - 1;

int n;

string a, b;

int rez;
int dp[MAX_N + 1][16];

void readFile()
{
    cin >> n;
    cin >> a >> b;

    a = " " + a;
    b = " " + b;

    for(int i = 1; i <= n; i ++)
    {
        a[i] -= '0';
        b[i] -= '0';
    }
}

int p[4][2];

///ON OFF T

vector <int> g[MAX_N + 1];

void add(int a, int b)
{
    g[a].push_back(b);
}

struct elem
{
    int a, b, c;

    bool operator == (elem &x)
    {
        return (a == x.a && b == x.b && c == x.c);
    }
};

elem f[16];

int cod[16];
int r[16][2];

void preCalc()
{
    p[1][0] = p[1][1] = 1;
    p[2][0] = p[2][1] = 0;
    p[3][1] = 0;
    p[3][0] = 1;

    cod[0] = 0;///000
    cod[1] = 1;///001
    cod[2] = 2;///010
    cod[3] = 4;///100

    cod[4] = cod[5] = 3;///011
    cod[6] = cod[7] = 5;///101
    cod[8] = cod[9] = 6;///110

    cod[10] = cod[11] = cod[12] = cod[13] = cod[14] = cod[15] = 7;///111

    r[0][0] = 0;
    r[0][1] = 1;

    r[1][0] = r[1][1] = 1;
    r[2][0] = r[2][1] = 0;

    r[3][1] = 0;
    r[3][0] = 1;

    r[4][0] = r[4][1] = 0;
    r[5][0] = r[5][1] = 1;
    r[6][0] = r[6][1] = 0;
    r[7][0] = r[7][1] = 1;
    r[8][0] = r[8][1] = 1;
    r[9][0] = r[9][1] = 0;

    r[10][0] = r[10][1] = 1;
    r[11][0] = r[11][1] = 0;
    r[12][0] = r[12][1] = 0;
    r[13][0] = r[13][1] = 1;
    r[14][0] = r[14][1] = 0;
    r[15][0] = r[15][1] = 1;

    for(int i = 1; i <= 15; i ++)
        add(i, 0);

    for(int i = 4; i <= 7; i ++)
        add(i, 1);

    add(4, 2);
    add(5, 2);
    add(8, 2);
    add(9, 2);

    for(int i = 6; i <= 9; i ++)
        add(i, 3);

    for(int i = 10; i <= 15; i ++)
    {
        add(i, 1);
        add(i, 2);
        add(i, 3);
    }

    add(10, 4);
    add(10, 6);
    add(10, 8);

    add(11, 4);
    add(11, 9);
    add(11, 6);

    add(12, 6);
    add(12, 8);
    add(12, 5);

    add(13, 7);
    add(13, 5);
    add(13, 8);

    add(14, 4);
    add(14, 9);
    add(14, 7);

    add(15, 5);
    add(15, 7);
    add(15, 9);

    f[0] = {0, 0, 0};
    f[1] = {1, 0, 0};
    f[2] = {2, 0, 0};
    f[3] = {3, 0, 0};
    f[4] = {1, 2, 0};
    f[5] = {2, 1, 0};
    f[6] = {1, 3, 0};
    f[7] = {3, 1, 0};
    f[8] = {2, 3, 0};
    f[9] = {3, 2, 0};
    f[10] = {1, 2, 3};
    f[11] = {1, 3, 2};
    f[12] = {2, 1, 3};
    f[13] = {2, 3, 1};
    f[14] = {3, 1, 2};
    f[15] = {3, 2, 1};
}

int cost[16][16];

int Cost(int a, int b)
{
    int c = 0;
    for(int x = 0; x <= 2; x ++)
    {
        if((((1 << x) & cod[a]) == 0) && (((1 << x) & cod[b]) != 0))
            c ++;
    }

    return c;
}

int viz[16];
int que[16];

int getCod(elem x)
{
    for(int i = 0; i <= 15; i ++)
    {
        if(f[i] == x)
            return i;
    }

    return -1;
}

int bagaSt(elem &x, int i)
{
    x.c = x.b;
    x.b = x.a;
    x.a = i;

    return getCod(x);
}

int bagaDr(elem &x, int i)
{
    if(x.a == 0)
        x.a = i;

    else if(x.b == 0)
        x.b = i;

    else
        x.c = i;

    return getCod(x);
}

int bagaMid(elem &x, int i)
{
    x.c = x.b;
    x.b = i;

    return getCod(x);
}

void bfs(int s)
{
    memset(viz, 0, sizeof(viz));
    int st = 1, dr = 0;

    for(auto u : g[s])
    {
        que[++ dr] = u;
        viz[u] = 1;
    }

    que[++ dr] = s;
    viz[s] = 1;

    int c = 0;

    while(st <= dr)
    {
        int cr = que[st ++];

        if(cr <= 10)
        for(int i = 1; i <= 3; i ++)
        {
            elem x = f[cr];
            c = bagaSt(x, i);
            if(c != -1 && viz[c] == 0)
            {
                cost[s][c] = cost[s][cr] + 1;
                viz[c] = 1;
                que[++ dr] = c;
            }

            x = f[cr];
            c = bagaDr(x, i);
            if(c != -1 && viz[c] == 0)
            {
                cost[s][c] = cost[s][cr] + 1;
                viz[c] = 1;
                que[++ dr] = c;
            }

            if(cr >= 4 && cr <= 9)
            {
                x = f[cr];
                c = bagaMid(x, i);
                if(c != -1 && viz[c] == 0)
                {
                    cost[s][c] = cost[s][cr] + 1;
                    viz[c] = 1;
                    que[++ dr] = c;
                }
            }
        }
    }
}

void getCosts()
{
    for(int j = 0; j <= 15; j ++)
    {
        cost[0][j] = Cost(0, j);
        cost[j][0] = 0;

        //cout << "LA " << j << " " << cost[0][j] << "\n";
    }

    for(int i = 1; i <= 15; i ++)
    {
        bfs(i);

       // for(int j = 1; j <= 15; j ++)
          //  cout << " DE LA " << i << " La " << j << " " << cost[i][j] << "\n";
    }
}

void getDp()
{
    for(int i = 0; i <= n; i ++)
    {
        for(int j = 0; j <= 15; j ++)
            dp[i][j] = sqrInf;
    }

    dp[0][0] = 0;

    for(int i = 0; i < n; i ++)
    {
        for(int j = 0; j <= 15; j ++)
        {
            if(dp[i][j] != sqrInf)
            {
                //cout << " SUJNTEMN " << i << " COD " << j << "  DP " << dp[i][j] << "\n";

                for(int h = 0; h <= 15; h ++)
                {
                    if(r[h][a[i + 1]] == b[i + 1])
                    {
                       // cout << "MERGEM urm " << h << " cost " << cost[j][h] << "\n";

                        dp[i + 1][h] = min(dp[i + 1][h], dp[i][j] + cost[j][h]);
                    }
                }
            }
        }
    }

   // cout << (int)a[n] << " " << (int)b[n] << " " << r[0][0] << "\n";

    rez = sqrInf;
    for(int i = 0; i <= 15; i ++)
    {
        if(r[i][a[n]] == b[n])
            rez = min(rez, dp[n][i]);
    }
}

void solve()
{
    preCalc();

    getCosts();

    getDp();
}

void printFile()
{
    cout << rez << "\n";
}

int main()
{
    readFile();

    solve();

    printFile();

    return 0;
}

Compilation message

lamp.cpp: In function 'void getDp()':
lamp.cpp:315:37: warning: array subscript has type 'char' [-Wchar-subscripts]
                     if(r[h][a[i + 1]] == b[i + 1])
                                     ^
lamp.cpp:331:21: warning: array subscript has type 'char' [-Wchar-subscripts]
         if(r[i][a[n]] == b[n])
                     ^
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23808 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
3 Correct 19 ms 23808 KB Output is correct
4 Correct 19 ms 23808 KB Output is correct
5 Correct 18 ms 23808 KB Output is correct
6 Correct 19 ms 23808 KB Output is correct
7 Correct 19 ms 23808 KB Output is correct
8 Correct 18 ms 23808 KB Output is correct
9 Correct 23 ms 23808 KB Output is correct
10 Correct 18 ms 23808 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 18 ms 23808 KB Output is correct
13 Correct 18 ms 23808 KB Output is correct
14 Correct 18 ms 23808 KB Output is correct
15 Correct 18 ms 23808 KB Output is correct
16 Correct 18 ms 23808 KB Output is correct
17 Correct 19 ms 23808 KB Output is correct
18 Correct 18 ms 23808 KB Output is correct
19 Correct 19 ms 23808 KB Output is correct
20 Correct 18 ms 23808 KB Output is correct
21 Correct 19 ms 23808 KB Output is correct
22 Correct 18 ms 23808 KB Output is correct
23 Correct 19 ms 23808 KB Output is correct
24 Correct 19 ms 23808 KB Output is correct
25 Correct 18 ms 23808 KB Output is correct
26 Correct 18 ms 23808 KB Output is correct
27 Correct 18 ms 23808 KB Output is correct
28 Correct 18 ms 23808 KB Output is correct
29 Correct 20 ms 23808 KB Output is correct
30 Correct 18 ms 23808 KB Output is correct
31 Correct 19 ms 23808 KB Output is correct
32 Correct 18 ms 23808 KB Output is correct
33 Correct 18 ms 23808 KB Output is correct
34 Correct 18 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23808 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
3 Correct 19 ms 23808 KB Output is correct
4 Correct 19 ms 23808 KB Output is correct
5 Correct 18 ms 23808 KB Output is correct
6 Correct 19 ms 23808 KB Output is correct
7 Correct 19 ms 23808 KB Output is correct
8 Correct 18 ms 23808 KB Output is correct
9 Correct 23 ms 23808 KB Output is correct
10 Correct 18 ms 23808 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 18 ms 23808 KB Output is correct
13 Correct 18 ms 23808 KB Output is correct
14 Correct 18 ms 23808 KB Output is correct
15 Correct 18 ms 23808 KB Output is correct
16 Correct 18 ms 23808 KB Output is correct
17 Correct 19 ms 23808 KB Output is correct
18 Correct 18 ms 23808 KB Output is correct
19 Correct 19 ms 23808 KB Output is correct
20 Correct 18 ms 23808 KB Output is correct
21 Correct 19 ms 23808 KB Output is correct
22 Correct 18 ms 23808 KB Output is correct
23 Correct 19 ms 23808 KB Output is correct
24 Correct 19 ms 23808 KB Output is correct
25 Correct 18 ms 23808 KB Output is correct
26 Correct 18 ms 23808 KB Output is correct
27 Correct 18 ms 23808 KB Output is correct
28 Correct 18 ms 23808 KB Output is correct
29 Correct 20 ms 23808 KB Output is correct
30 Correct 18 ms 23808 KB Output is correct
31 Correct 19 ms 23808 KB Output is correct
32 Correct 18 ms 23808 KB Output is correct
33 Correct 18 ms 23808 KB Output is correct
34 Correct 18 ms 23808 KB Output is correct
35 Correct 19 ms 23936 KB Output is correct
36 Correct 19 ms 23936 KB Output is correct
37 Correct 19 ms 23936 KB Output is correct
38 Correct 19 ms 23936 KB Output is correct
39 Correct 19 ms 23936 KB Output is correct
40 Correct 20 ms 23936 KB Output is correct
41 Correct 19 ms 23936 KB Output is correct
42 Correct 19 ms 23936 KB Output is correct
43 Correct 21 ms 24064 KB Output is correct
44 Correct 19 ms 23936 KB Output is correct
45 Correct 20 ms 23936 KB Output is correct
46 Correct 19 ms 23936 KB Output is correct
47 Correct 19 ms 23936 KB Output is correct
48 Correct 20 ms 23936 KB Output is correct
49 Correct 19 ms 23936 KB Output is correct
50 Correct 19 ms 23936 KB Output is correct
51 Correct 19 ms 23936 KB Output is correct
52 Correct 19 ms 23936 KB Output is correct
53 Correct 19 ms 23936 KB Output is correct
54 Correct 19 ms 23948 KB Output is correct
55 Correct 23 ms 23936 KB Output is correct
56 Correct 22 ms 23936 KB Output is correct
57 Correct 20 ms 23936 KB Output is correct
58 Correct 19 ms 23936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 23808 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
3 Correct 18 ms 23808 KB Output is correct
4 Correct 19 ms 23808 KB Output is correct
5 Correct 18 ms 23808 KB Output is correct
6 Correct 18 ms 23808 KB Output is correct
7 Correct 356 ms 90292 KB Output is correct
8 Correct 368 ms 90436 KB Output is correct
9 Correct 356 ms 90292 KB Output is correct
10 Correct 362 ms 90420 KB Output is correct
11 Correct 362 ms 90424 KB Output is correct
12 Correct 360 ms 90356 KB Output is correct
13 Correct 354 ms 90420 KB Output is correct
14 Correct 358 ms 90424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23808 KB Output is correct
2 Correct 18 ms 23808 KB Output is correct
3 Correct 19 ms 23808 KB Output is correct
4 Correct 19 ms 23808 KB Output is correct
5 Correct 18 ms 23808 KB Output is correct
6 Correct 19 ms 23808 KB Output is correct
7 Correct 19 ms 23808 KB Output is correct
8 Correct 18 ms 23808 KB Output is correct
9 Correct 23 ms 23808 KB Output is correct
10 Correct 18 ms 23808 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
12 Correct 18 ms 23808 KB Output is correct
13 Correct 18 ms 23808 KB Output is correct
14 Correct 18 ms 23808 KB Output is correct
15 Correct 18 ms 23808 KB Output is correct
16 Correct 18 ms 23808 KB Output is correct
17 Correct 19 ms 23808 KB Output is correct
18 Correct 18 ms 23808 KB Output is correct
19 Correct 19 ms 23808 KB Output is correct
20 Correct 18 ms 23808 KB Output is correct
21 Correct 19 ms 23808 KB Output is correct
22 Correct 18 ms 23808 KB Output is correct
23 Correct 19 ms 23808 KB Output is correct
24 Correct 19 ms 23808 KB Output is correct
25 Correct 18 ms 23808 KB Output is correct
26 Correct 18 ms 23808 KB Output is correct
27 Correct 18 ms 23808 KB Output is correct
28 Correct 18 ms 23808 KB Output is correct
29 Correct 20 ms 23808 KB Output is correct
30 Correct 18 ms 23808 KB Output is correct
31 Correct 19 ms 23808 KB Output is correct
32 Correct 18 ms 23808 KB Output is correct
33 Correct 18 ms 23808 KB Output is correct
34 Correct 18 ms 23808 KB Output is correct
35 Correct 19 ms 23936 KB Output is correct
36 Correct 19 ms 23936 KB Output is correct
37 Correct 19 ms 23936 KB Output is correct
38 Correct 19 ms 23936 KB Output is correct
39 Correct 19 ms 23936 KB Output is correct
40 Correct 20 ms 23936 KB Output is correct
41 Correct 19 ms 23936 KB Output is correct
42 Correct 19 ms 23936 KB Output is correct
43 Correct 21 ms 24064 KB Output is correct
44 Correct 19 ms 23936 KB Output is correct
45 Correct 20 ms 23936 KB Output is correct
46 Correct 19 ms 23936 KB Output is correct
47 Correct 19 ms 23936 KB Output is correct
48 Correct 20 ms 23936 KB Output is correct
49 Correct 19 ms 23936 KB Output is correct
50 Correct 19 ms 23936 KB Output is correct
51 Correct 19 ms 23936 KB Output is correct
52 Correct 19 ms 23936 KB Output is correct
53 Correct 19 ms 23936 KB Output is correct
54 Correct 19 ms 23948 KB Output is correct
55 Correct 23 ms 23936 KB Output is correct
56 Correct 22 ms 23936 KB Output is correct
57 Correct 20 ms 23936 KB Output is correct
58 Correct 19 ms 23936 KB Output is correct
59 Correct 19 ms 23808 KB Output is correct
60 Correct 18 ms 23808 KB Output is correct
61 Correct 18 ms 23808 KB Output is correct
62 Correct 19 ms 23808 KB Output is correct
63 Correct 18 ms 23808 KB Output is correct
64 Correct 18 ms 23808 KB Output is correct
65 Correct 356 ms 90292 KB Output is correct
66 Correct 368 ms 90436 KB Output is correct
67 Correct 356 ms 90292 KB Output is correct
68 Correct 362 ms 90420 KB Output is correct
69 Correct 362 ms 90424 KB Output is correct
70 Correct 360 ms 90356 KB Output is correct
71 Correct 354 ms 90420 KB Output is correct
72 Correct 358 ms 90424 KB Output is correct
73 Correct 371 ms 90424 KB Output is correct
74 Correct 372 ms 90424 KB Output is correct
75 Correct 361 ms 90296 KB Output is correct
76 Correct 361 ms 90416 KB Output is correct
77 Correct 360 ms 90420 KB Output is correct
78 Correct 365 ms 90420 KB Output is correct
79 Correct 357 ms 90424 KB Output is correct
80 Correct 372 ms 90424 KB Output is correct
81 Correct 357 ms 90548 KB Output is correct
82 Correct 378 ms 90548 KB Output is correct
83 Correct 387 ms 90452 KB Output is correct
84 Correct 376 ms 90420 KB Output is correct
85 Correct 374 ms 90292 KB Output is correct
86 Correct 377 ms 90424 KB Output is correct
87 Correct 400 ms 90420 KB Output is correct
88 Correct 397 ms 90548 KB Output is correct
89 Correct 377 ms 90424 KB Output is correct
90 Correct 398 ms 90588 KB Output is correct
91 Correct 422 ms 90420 KB Output is correct
92 Correct 395 ms 90420 KB Output is correct
93 Correct 420 ms 90420 KB Output is correct
94 Correct 407 ms 90500 KB Output is correct
95 Correct 402 ms 90420 KB Output is correct
96 Correct 394 ms 90428 KB Output is correct
97 Correct 389 ms 90392 KB Output is correct
98 Correct 395 ms 90424 KB Output is correct
99 Correct 389 ms 90344 KB Output is correct
100 Correct 393 ms 90548 KB Output is correct
101 Correct 391 ms 90288 KB Output is correct
102 Correct 390 ms 90340 KB Output is correct