1. 程式人生 > >Codeforces Round #486 (Div. 3) D. Points and Powers of Two

Codeforces Round #486 (Div. 3) D. Points and Powers of Two

equals tegra names size AR tin ++ include ESS

Codeforces Round #486 (Div. 3) D. Points and Powers of Two

題目連接:

http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/D

Description

There are n distinct points on a coordinate line, the coordinate of i-th point equals to xi. Choose a subset of the given set of points such that the distance between each pair of points in a subset is an integral power of two. It is necessary to consider each pair of points, not only adjacent. Note that any subset containing one element satisfies the condition above. Among all these subsets, choose a subset with maximum possible size.

In other words, you have to choose the maximum possible number of points xi1,xi2,…,xim such that for each pair xij, xik it is true that |xij?xik|=2d where d is some non-negative integer number (not necessarily the same for each pair of points)

Sample Input

6
3 5 4 7 10 12

Sample Output

3
7 3 5

題意

給定一個集合,求最大子集,該子集內所有元素差都等於2的冪次。

題解:

假設a-b=2^k,a-c=2^i,i!=k時,b-c的值必不滿足條件,所以最大子集最多只有三個元素

代碼

#include <bits/stdc++.h>

using namespace std;

int n;
set<long long> s;
int ans;
vector<long long> v;

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        long long x;
        cin >> x;
        s.insert(x);
    }
    for
(auto i:s) { for (int o = 0; o < 33; o++) { int flag1 = s.count(i - (1 << o)); int flag2 = s.count(i + (1 << o)); if (flag1 && flag2) { cout << 3 << endl << (i - (1 << o)) << " " << i << " " << (i + (1 << o)) << endl; return 0; } else if (flag1) { if (!v.size()) { v.push_back(i); v.push_back(i - (1 << o)); } } else if (flag2) { if (!v.size()) { v.push_back(i); v.push_back(i + (1 << o)); } } } } if (v.size()) { cout << 2 << endl; for (auto i: v) cout << i << " "; } else cout << 1 << endl << *s.begin(); }

Codeforces Round #486 (Div. 3) D. Points and Powers of Two