Codeforces Round #486 (Div. 3) D. Points and Powers of Two【贪心】

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.

思路:

可以先写几组数据分析一下怎么构造解。先排序,大小为1和2就不用说了,大小为3的时候,相邻的差值一定相等且为2的幂,这样最大和最小的差值一定也是2的幂。分析大小为4的时候,如果前三个数按照以上规则构造,那么最后一个数一定构造不出来。由此可知大小最大为3。并且贪心策略也有了。这一题用map会超,可以用二分查找。

代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
using namespace std;
#define inf 0x3f3f3f3f
#define MAXN 200005
#define ll long long
int n,ans;
ll a[MAXN],b[10],fac[66];
int main()
{
    scanf("%d",&n);
    fac[0]=1;
    for(int i=1;i<=63;i++)
    {
        fac[i]=fac[i-1]<<1;
    }
    for(int i=0;i<n;i++)
    {
        scanf("%lld",&a[i]);
    }
    sort(a,a+n);
    ans=1;
    b[0]=a[0];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<=62;j++)
        {
            if(i+2<n && (*lower_bound(a,a+n,a[i]+fac[j]))==a[i]+fac[j] && (*lower_bound(a,a+n,a[i]+fac[j+1]))==a[i]+fac[j+1])
            {
                printf("3\n");
                printf("%lld %lld %lld\n",a[i],a[i]+fac[j],a[i]+fac[j+1]);
                exit(0);
            }
            else if(i+1<n && (*lower_bound(a,a+n,a[i]+fac[j]))==a[i]+fac[j] && ans==1)
            {
                ans=2;
                b[0]=a[i];
                b[1]=a[i]+fac[j];
            }
        }
    }
    printf("%d\n",ans);
    for(int i=0;i<ans;i++)
    {
        if(i) printf(" ");
        printf("%lld",b[i]);
    }
    printf("\n");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/u013852115/article/details/80559188