n개의 수와 x라는 수가 주어졌을 때


ai (1<=i<=n) 에 x를 &(bitwise and)하는 연산을 할 수 있고


i!=j일 때 ai==aj 인 쌍을 하나 만들기 위한 최소연산 횟수를 물어보는 문제다.


ai를 입력받고 ai&x한걸 다른 배열에 체크해놓으면 된다.


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
#include <stdio.h>
#include <algorithm>
using namespace std;
 
int n, x;
int ans = 3;
bool c[100001];
bool c2[100001];
 
int main() {
    scanf("%d%d"&n, &x);
    for (int i = 0, temp; i < n; ++i) {
        scanf("%d"&temp);
        if (c[temp]) 
            ans = min(ans, 0);
        else if (c2[temp] || c[temp&x]) 
            ans = min(ans, 1);
        else if (c2[temp&x]) 
            ans = min(ans, 2);
        c[temp] = 1;
        c2[temp&x] = 1;
    }
    if (ans == 3printf("-1");
    else printf("%d", ans);
    return 0;
}
cs


'codeforces > #500 div2' 카테고리의 다른 글

C. Photo of The Sky  (0) 2018.08.02
A. Piles With Stones  (0) 2018.08.02

+ Recent posts