답안 #921958

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
921958 2024-02-04T15:04:53 Z boris_mihov Lamps (JOI19_lamps) C++17
100 / 100
742 ms 192352 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>

typedef long long llong;
const int MAXN = 1000000 + 10;
const int INF  = 1e9;

int n;
char a[MAXN];
char b[MAXN];
int add[16][16];
int rem[16][16];
int getBit[2][16];
int dp[MAXN][16];
bool bl[MAXN][16];

const std::vector <int> statePerms[] = 
{
    {},
    {1},
    {2},
    {3},
    {1, 2},
    {1, 3},
    {2, 3},
    {2, 1},
    {3, 1},
    {3, 2},
    {1, 2, 3},
    {1, 3, 2},
    {2, 1, 3},
    {2, 3, 1},
    {3, 1, 2},
    {3, 2, 1}
};

int f(int idx, int state)
{
    if (idx == n + 1)
    {
        return 0;
    }

    if (bl[idx][state])
    {
        return dp[idx][state];
    }

    bl[idx][state] = true;
    dp[idx][state] = f(idx + 1, state) + (getBit[a[idx] - '0'][state] != b[idx] - '0');

    for (int newMask = 0 ; newMask < 16 ; ++newMask)
    {
        if (add[state][newMask] == INF || getBit[a[idx] - '0'][newMask] != b[idx] - '0')
        {
            continue;
        }

        dp[idx][state] = std::min(dp[idx][state], add[state][newMask] + f(idx + 1, newMask));
    }

    for (int newMask = 0 ; newMask < 16 ; ++newMask)
    {
        if (rem[state][newMask] != INF)
        {
            dp[idx][state] = std::min(dp[idx][state], (getBit[a[idx] - '0'][state] != b[idx] - '0') + f(idx + 1, newMask));
        }
    }

    return dp[idx][state];
}

bool contains(int idxOne, int idxTwo)
{
    int ptrA = 0;
    int ptrB = 0;
    while (ptrA < statePerms[idxOne].size())
    {
        if (ptrB == statePerms[idxTwo].size())
        {
            return false;
        }

        if (statePerms[idxOne][ptrA] == statePerms[idxTwo][ptrB])
        {
            ptrA++;
        }

        ptrB++;
    }

    return true;
}

int getNewBit(int myBit, int myOp)
{
    if (myOp == 1)
    {
        return 0;
    }

    if (myOp == 2)
    {
        return 1;
    }

    return myBit ^ 1;
}

void solve()
{
    for (int bit = 0 ; bit < 2 ; ++bit)
    {
        for (int mask = 0 ; mask < 16 ; ++mask)
        {
            getBit[bit][mask] = bit;
            for (const int &op : statePerms[mask])
            {
                getBit[bit][mask] = getNewBit(getBit[bit][mask], op);
            }
        }
    }

    for (int i = 0 ; i < 16 ; ++i)
    {
        for (int j = 0 ; j < 16 ; ++j)
        {
            add[i][j] = INF;
            rem[i][j] = INF;
        }
    }

    for (int maskOne = 0 ; maskOne < 16 ; ++maskOne)
    {
        for (int maskTwo = 0 ; maskTwo < 16 ; ++maskTwo)
        {
            if (maskOne == maskTwo || !contains(maskOne, maskTwo))
            {
                continue;
            }
            
            add[maskOne][maskTwo] = statePerms[maskTwo].size() - statePerms[maskOne].size();
            rem[maskTwo][maskOne] = 0;
        }
    }

    std::cout << f(1, 0) << '\n';
}

void input()
{
    std::cin >> n;
    std::cin >> a + 1;
    std::cin >> b + 1;
}   

void fastIOI()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIOI();
    input();
    solve();

    return 0;
}

Compilation message

lamp.cpp: In function 'bool contains(int, int)':
lamp.cpp:80:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |     while (ptrA < statePerms[idxOne].size())
      |            ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
lamp.cpp:82:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |         if (ptrB == statePerms[idxTwo].size())
      |             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
lamp.cpp: In function 'void input()':
lamp.cpp:156:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  156 |     std::cin >> a + 1;
      |                 ~~^~~
lamp.cpp:157:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  157 |     std::cin >> b + 1;
      |                 ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2512 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2716 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2512 KB Output is correct
17 Correct 1 ms 2648 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2524 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2392 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
29 Correct 1 ms 2396 KB Output is correct
30 Correct 1 ms 2396 KB Output is correct
31 Correct 1 ms 2396 KB Output is correct
32 Correct 1 ms 2396 KB Output is correct
33 Correct 1 ms 2396 KB Output is correct
34 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2512 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2716 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2512 KB Output is correct
17 Correct 1 ms 2648 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2524 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2392 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
29 Correct 1 ms 2396 KB Output is correct
30 Correct 1 ms 2396 KB Output is correct
31 Correct 1 ms 2396 KB Output is correct
32 Correct 1 ms 2396 KB Output is correct
33 Correct 1 ms 2396 KB Output is correct
34 Correct 1 ms 2396 KB Output is correct
35 Correct 2 ms 2652 KB Output is correct
36 Correct 2 ms 2828 KB Output is correct
37 Correct 2 ms 2652 KB Output is correct
38 Correct 3 ms 2652 KB Output is correct
39 Correct 2 ms 2652 KB Output is correct
40 Correct 2 ms 2652 KB Output is correct
41 Correct 2 ms 2652 KB Output is correct
42 Correct 2 ms 2772 KB Output is correct
43 Correct 2 ms 2652 KB Output is correct
44 Correct 2 ms 2648 KB Output is correct
45 Correct 2 ms 2772 KB Output is correct
46 Correct 2 ms 2904 KB Output is correct
47 Correct 2 ms 2652 KB Output is correct
48 Correct 2 ms 2652 KB Output is correct
49 Correct 2 ms 2656 KB Output is correct
50 Correct 2 ms 2652 KB Output is correct
51 Correct 2 ms 2652 KB Output is correct
52 Correct 3 ms 2652 KB Output is correct
53 Correct 2 ms 2652 KB Output is correct
54 Correct 2 ms 2652 KB Output is correct
55 Correct 2 ms 2816 KB Output is correct
56 Correct 2 ms 2652 KB Output is correct
57 Correct 2 ms 2652 KB Output is correct
58 Correct 2 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 542 ms 192192 KB Output is correct
8 Correct 665 ms 192056 KB Output is correct
9 Correct 645 ms 192204 KB Output is correct
10 Correct 640 ms 192060 KB Output is correct
11 Correct 658 ms 192080 KB Output is correct
12 Correct 594 ms 192024 KB Output is correct
13 Correct 696 ms 191988 KB Output is correct
14 Correct 653 ms 192016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2512 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2716 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2512 KB Output is correct
17 Correct 1 ms 2648 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2524 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2392 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
29 Correct 1 ms 2396 KB Output is correct
30 Correct 1 ms 2396 KB Output is correct
31 Correct 1 ms 2396 KB Output is correct
32 Correct 1 ms 2396 KB Output is correct
33 Correct 1 ms 2396 KB Output is correct
34 Correct 1 ms 2396 KB Output is correct
35 Correct 2 ms 2652 KB Output is correct
36 Correct 2 ms 2828 KB Output is correct
37 Correct 2 ms 2652 KB Output is correct
38 Correct 3 ms 2652 KB Output is correct
39 Correct 2 ms 2652 KB Output is correct
40 Correct 2 ms 2652 KB Output is correct
41 Correct 2 ms 2652 KB Output is correct
42 Correct 2 ms 2772 KB Output is correct
43 Correct 2 ms 2652 KB Output is correct
44 Correct 2 ms 2648 KB Output is correct
45 Correct 2 ms 2772 KB Output is correct
46 Correct 2 ms 2904 KB Output is correct
47 Correct 2 ms 2652 KB Output is correct
48 Correct 2 ms 2652 KB Output is correct
49 Correct 2 ms 2656 KB Output is correct
50 Correct 2 ms 2652 KB Output is correct
51 Correct 2 ms 2652 KB Output is correct
52 Correct 3 ms 2652 KB Output is correct
53 Correct 2 ms 2652 KB Output is correct
54 Correct 2 ms 2652 KB Output is correct
55 Correct 2 ms 2816 KB Output is correct
56 Correct 2 ms 2652 KB Output is correct
57 Correct 2 ms 2652 KB Output is correct
58 Correct 2 ms 2652 KB Output is correct
59 Correct 1 ms 2396 KB Output is correct
60 Correct 1 ms 2396 KB Output is correct
61 Correct 1 ms 2396 KB Output is correct
62 Correct 1 ms 2396 KB Output is correct
63 Correct 1 ms 2396 KB Output is correct
64 Correct 1 ms 2396 KB Output is correct
65 Correct 542 ms 192192 KB Output is correct
66 Correct 665 ms 192056 KB Output is correct
67 Correct 645 ms 192204 KB Output is correct
68 Correct 640 ms 192060 KB Output is correct
69 Correct 658 ms 192080 KB Output is correct
70 Correct 594 ms 192024 KB Output is correct
71 Correct 696 ms 191988 KB Output is correct
72 Correct 653 ms 192016 KB Output is correct
73 Correct 569 ms 192064 KB Output is correct
74 Correct 726 ms 192036 KB Output is correct
75 Correct 695 ms 192084 KB Output is correct
76 Correct 639 ms 192352 KB Output is correct
77 Correct 659 ms 191980 KB Output is correct
78 Correct 640 ms 192244 KB Output is correct
79 Correct 643 ms 192144 KB Output is correct
80 Correct 644 ms 192080 KB Output is correct
81 Correct 643 ms 192212 KB Output is correct
82 Correct 644 ms 191996 KB Output is correct
83 Correct 645 ms 192048 KB Output is correct
84 Correct 632 ms 192048 KB Output is correct
85 Correct 628 ms 192192 KB Output is correct
86 Correct 648 ms 192180 KB Output is correct
87 Correct 644 ms 192048 KB Output is correct
88 Correct 642 ms 192092 KB Output is correct
89 Correct 637 ms 192084 KB Output is correct
90 Correct 643 ms 192232 KB Output is correct
91 Correct 696 ms 192004 KB Output is correct
92 Correct 742 ms 192168 KB Output is correct
93 Correct 702 ms 192204 KB Output is correct
94 Correct 686 ms 192032 KB Output is correct
95 Correct 706 ms 192256 KB Output is correct
96 Correct 702 ms 192252 KB Output is correct
97 Correct 682 ms 192052 KB Output is correct
98 Correct 701 ms 192084 KB Output is correct
99 Correct 701 ms 192208 KB Output is correct
100 Correct 693 ms 192048 KB Output is correct
101 Correct 683 ms 192048 KB Output is correct
102 Correct 696 ms 192212 KB Output is correct