Codeforces 988D(STL运用+思维)

传送门

题面:

    

D. Points and Powers of Two
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 xijxijxikxik it is true that |xijxik|=2d|xij−xik|=2d where dd is some non-negative integer number (not necessarily the same for each pair of points).

Input

The first line contains one integer nn (1n21051≤n≤2⋅105) — the number of points.

The second line contains nn pairwise distinct integers x1,x2,,xnx1,x2,…,xn (109xi109−109≤xi≤109) — the coordinates of points.

Output

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.

Examples
input
Copy
6
3 5 4 7 10 12
output
Copy
3
7 3 5
input
Copy
5
-1 2 5 8 11
output
Copy
1
8
Note

In the first example the answer is [7,3,5][7,3,5]. Note, that |73|=4=22|7−3|=4=22|75|=2=21|7−5|=2=21 and |35|=2=21|3−5|=2=21. You can't find a subset having more points satisfying the required property.


题目描述:

    给你一个大小为n的数列,问你最多能够在这个这n个数中选取多少个,使得由他们组成的集合中,两两之间的差的绝对值为2的幂。

题目分析:

    对于这个题目,我们需要发现这么一个结论,答案中形成的集合的大小最大只能达到3。

    下面对这个命题进行简单的证明:

    我们设当集合大小=3,三个数从小到大分别为a,b,c,即要符合条件,则需要满足:

    b-a=k1 (1)

    c-b=k2 (2)

    c-a=k3 (3)

    将(2)+(1)可得 c-a=k1+k2,再根据(3)可得

    k3=k1+k2。

    因为我们知道,要满足题意,k1,k2,k3都必须为2的幂。而要使得2^a1+2^b1==2^c1成立,则不难得出,当且仅当a1==b1时成立,即k1==k2时成立,此时,不难发现,a,b,c三个数是形成一个等差数列的。

    而当集合的大小>=4时,设大小为4,四个数由大到小分别为a,b,c,d。则根据上面的证明,则我们要满足答案,则需要abc、abd、acd、bcd......所有的三元组都需要满足上述式子。显然这是不成立的,因此,大小大于3的答案是不合理的。

    因此,我们只需要考虑三个以内的答案即可。首先我们需要开一个set记录数列中的数。而因为存在的几个数是需要满足等差数列的,因此,我们可以枚举1到1e9中2的幂j,判断a+j和a+j+j是否在set内即可。倘若都不在set内,则随便输出一个数即可。

代码:

    

#include <bits/stdc++.h>
#define maxn 200005
using namespace std;
typedef long long ll;
ll a[maxn];
set<ll>st;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
        st.insert(a[i]);
    }
    for(int i=0;i<n;i++){
        for(ll j=1;j<=2e9;j<<=1){
            if(st.count(a[i]+j)&&st.count(a[i]+2*j)){
                cout<<3<<endl;
                cout<<a[i]<<" "<<a[i]+j<<" "<<a[i]+2*j<<endl;
                return 0;
            }
        }
    }
    for(int i=0;i<n;i++){
        for(ll j=1;j<=2e9;j<<=1){
            if(st.count(a[i]+j)){
                cout<<"2"<<endl;
                cout<<a[i]<<" "<<a[i]+j<<endl;
                return 0;
            }
        }
    }
    cout<<1<<endl;
    cout<<a[0]<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_39453270/article/details/80548442