codeforces 988D

题意:给你n个数,找到一个集合使得任意两数之差为2^d,输出这个

题解:经过分析我们发现要想组成这样一个集合,我们集合中不同的数一定是3个,并且这三个元素从小到大排列后,相邻两个元素之查一定是相等的都是2的d次方。那么我们就枚举每个元素i作为最小元素,然后再枚举d,判断下i+2^d和i+2^d+2^d是否存在,加上存在的个数,最后取最大就ok了

#include<bits/stdc++.h>
using namespace  std;
const int INF = 0x3f3f3f3f;
const int maxn = 2e5+7;
int a[maxn];
map<int, int>mp;
int cnt[10];
int ans[10];
int main()
{
	int n;
	cin >> n;
	int mx = -INF;
	for(int i = 0; i < n; i++)
	{
		cin >> a[i];
		mx= max(mx, a[i]);
		mp[a[i]] ++;
	}
	sort(a, a+n);
	int ans1 = 0, ans_id;
	int flag;
	for(int i = 0; i < n; i++)
	{
		if(ans1 < mp[a[i]])
		{
			ans1 = mp[a[i]];
			ans[0] = a[i];
			cnt[0] = mp[a[i]];
		}
		int size = mp[a[i]];
		for(int j = 1; a[i] + j <= mx;j *= 2)
		{
			int tmp = mp[a[i]];
			map<int, int>::iterator it = mp.find(a[i] + j);
			if(it == mp.end())continue;
			tmp += it->second;
			map<int, int>::iterator its = mp.find(a[i] + j * 2);
			if(its == mp.end())
			{
				if(tmp >ans1)
				{
					ans1 = tmp;
					ans[0] = a[i];
					ans[1] = a[i] + j;
					cnt[0] = size;
					cnt[1] = it->second;
				}
			}
			else
			{
				tmp += its->second;
				if(tmp >ans1)
				{
					ans1 = tmp;
					ans[0] = a[i];
					ans[1] = a[i] + j;
					ans[2] = a[i] + j * 2;
					cnt[0] = size;
					cnt[1] = it->second;
					cnt[2] = its->second;
				}
				
			}
		}
	}

	cout << ans1 << endl;
	cout << ans[0];
	cnt[0] --;
	ans1 --;
	int j = 0;
	while(ans1)
	{
		for(int i = 0; i < cnt[j]; i++)
		{
			cout <<" "<< ans[j]; 
		}
		ans1 -= cnt[j++];
	}


	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值