概述
转自:https://www.cnblogs.com/xingkongyihao/p/9124635.html
D. Points and Powers of Two
There are nn distinct points on a coordinate line, the coordinate of ii-th point equals to xixi. 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,…,ximxi1,xi2,…,xim such that for each pair xijxij, xikxik it is true that |xij−xik|=2d|xij−xik|=2d where dd is some non-negative integer number (not necessarily the same for each pair of points).
The first line contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of points.
The second line contains nn pairwise distinct integers x1,x2,…,xnx1,x2,…,xn (−109≤xi≤109−109≤xi≤109) — the coordinates of points.
In the first line print mm — the maximum possible number of points in a subset that satisfies the conditions described above.
In the second line print mm integers — the coordinates of points in the subset you have chosen.
If there are multiple answers, print any of them.
6 3 5 4 7 10 12
3 7 3 5
5 -1 2 5 8 11
1 8
In the first example the answer is [7,3,5][7,3,5]. Note, that |7−3|=4=22|7−3|=4=22, |7−5|=2=21|7−5|=2=21 and |3−5|=2=21|3−5|=2=21. You can't find a subset having more points satisfying the required property.
题意:选取最大的区间,使的两两之差是2d ,d是非负整数。
思路:有一个结论,就是这样的区间的元素个数最多为3! 好了,知道了这个结论就很好写了。
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
int a[N], n;
map<int,int> mp;
int main() {
scanf("%d",&n);
for(int i = 0; i < n; i ++) {
scanf("%d",&a[i]);
mp[a[i]] = 1;
}
sort(a,a+n);
for(int i = 0; i < n; i ++) {
for(int j = 0; j < 32; j ++) {
int x = 1 << j;
if(mp[a[i]+x] && mp[a[i]+2*x]) {
return 0*printf("3n%d %d %dn",a[i],a[i]+x,a[i]+x*2);
}
if(a[i]+x > a[n-1] || a[i]+2*x > a[n-1]) break;
}
}
for(int i = 0; i < n; i ++) {
for(int j = 0; j < 32; j ++) {
int x = 1 << j;
if(mp[a[i]+x]) {
return 0*printf("2n%d %dn",a[i],a[i]+x);
}
if(mp[a[i]+x*2]) {
return 0*printf("2n%d %dn",a[i],a[i]+2*x);
}
if(a[i]+x > a[n-1] || a[i]+2*x > a[n-1]) break;
}
}
printf("1n%dn",a[0]);
return 0;
}
最后
以上就是自然烤鸡为你收集整理的Points and Powers of Two CodeForces - 988D转自:https://www.cnblogs.com/xingkongyihao/p/9124635.htmlD. Points and Powers of Two的全部内容,希望文章能够帮你解决Points and Powers of Two CodeForces - 988D转自:https://www.cnblogs.com/xingkongyihao/p/9124635.htmlD. Points and Powers of Two所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复