D - Points and Powers of Two(思维)

CodeForces - 988D

  • 题意
    给你n个不同的点的坐标(一维),找出最大的子集,子集满足,任意两点之间的距离为2k,k为非负整数。
  • 思路:结果只能有三种1,2,3。即子集最大为3(原因自己思考)。
  • 坑点:注意lower_bound搜索的边界,若搜索不到则返回右边界的下一位,所以要特判一下。
#include<bits/stdc++.h>
#include<cstdio>
#include<queue>
#include<cstdlib>
#include<string.h>
#include<string>
#include<iostream>
#include<cmath>
#include<map>
#include<algorithm>
#define endl "\n"
#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
#define ft first
#define sd second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ll long long int
#define mt(a,b) memset(a, b, sizeof a)
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
using namespace std;
const int N = 2e5, M = 1e6;
int a[N], b[N];
int main()
{
	IOS;
	int n; cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];

	sort(a + 1, a + n + 1);
	int ans = 1;

	int q = log2(a[n] - a[1]) + 1;

	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j <= q; j++)
		{
			int cnt = 0;
			int idx = lower_bound(a + i, a + n + 1, a[i] + (int)pow(2, j)) - a;
			if (a[idx] == a[i] + (int)pow(2, j) && idx != n + 1) cnt++;//注意特判

			int idx2 = lower_bound(a + 1, a + i, a[i] - (int)pow(2, j)) - a;
			if (a[idx2] == a[i] - (int)pow(2, j) && idx2 != i) cnt++;//注意特判
			
			if (cnt == 2)
			{
				ans = 3; b[1] = a[idx2], b[2] = a[i], b[3] = a[idx];
				break;
			}
			else if(cnt == 1)
			{
				ans = 2;
				if (a[idx] == a[i] + (int)pow(2, j)) b[1] = a[i], b[2] = a[idx];
				if (a[idx2] == a[i] - (int)pow(2, j)) b[1] = a[idx2], b[2] = a[i];
			}
			//cout << i << "---" << j << "---" << cnt << endl;
		}
		if (ans == 3) break;
	}

	cout << ans << endl;
	if (ans == 3) cout << b[1] << " " << b[2] << " " << b[3] << endl;
	else if (ans == 2) cout << b[1] << " " << b[2] << endl;
	else cout << a[1] << endl;

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

to cling

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值