ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 1547-D, *1300
    코드포스 2021. 7. 30. 21:08
     

    Problem - 1547D - Codeforces

     

    codeforces.com

    풀이

    • 수열 $a_{1}a_{2}...a_{n}$에 대해 $i$에서 $a_{i}\&a_{i+1}=a_{i},\ i = 1,\ 2,...,\ n-1$일때 수열이 $growing$이라고 한다
    • 두 수열 $a,\ b$에 대해 수열 $\left \{ (a_{1}\oplus b_{1}),\ (a_{2}\oplus b_{2}),\ ...\ ,\ (a_{n}\oplus b_{n}) \right \}$이 $growing$이면
      두 수열 $a,\ b$는 $co-growing$이라고 한다
    • 수열 $x$가 주어질 때 $x,\ y$가 $co-growing$에게 하는 lexicographically minimal sequence $y$를 구하라

     

    $O(n)$

    index $i$ $i+1$
    $x_{(2)}$ 1011 1101
    $y_{(2)}$ 0 a
    $x\oplus y$ 1011 b

    위 표와 같이 a, b 를 제외하고 정보를 알고있다고 하자
    수열 {$x\oplus y$}은 $growing$이기 때문에 b = 1?11 (비트가 1이었던 부분은 유지되어야 한다)
    따라서 a = 0?10 ($\because 1101 \oplus a = b\Leftrightarrow a=b\oplus 1101$)

    a 는 최소값이어야 하므로 ?는 0으로 바꾸면 되고, a = 0010. 따라서 b = 1111

    귀납적으로 $y$가 결정된다

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    #include <bits/stdc++.h>
    #define endl "\n"
    using namespace std;
     
    int n;
     
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0), cout.tie(0);
     
        int t;
        cin >> t;
        while(t--){
            cin >> n;
     
            int x;
            int y = 0;
            cin >> x;
            cout << y << ' ';
            int z = x;
     
            for(int i = 0; i < n-1; i++){
                cin >> x;
                y = (z^x)&z;
                z = x^y;
                cout << y << ' ';
            }
            cout << endl;
     
        }
     
        return 0;

     

    '코드포스' 카테고리의 다른 글

    1555-B, *1300  (0) 2021.07.31
    1555-C, *1300  (0) 2021.07.31
    1553-B, *1300  (0) 2021.07.28
    459-B, *1300  (0) 2021.07.28
    451-B, *1300  (0) 2021.07.28

    댓글

Designed by Tistory.