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();
}
原文地址:https://www.cnblogs.com/EDGsheryl/p/9153683.html