Codeforces Round #486 (Div. 3) D. Points and Powers of Two

Codeforces Round #486 (Div. 3) D. Points and Powers of Two

题目连接:

http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/D

Description

There are n distinct points on a coordinate line, the coordinate of i-th point equals to xi. 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,…,xim such that for each pair xij, xik it is true that |xij?xik|=2d where d is some non-negative integer number (not necessarily the same for each pair of points)

Sample Input

6
3 5 4 7 10 12

Sample Output

3
7 3 5

题意

给定一个集合,求最大子集,该子集内所有元素差都等于2的幂次。

题解:

假设a-b=2^k,a-c=2^i,i!=k时,b-c的值必不满足条件,所以最大子集最多只有三个元素

代码

#include <bits/stdc++.h>

using namespace std;

int n;
set<long long> s;
int ans;
vector<long long> v;

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        long long x;
        cin >> x;
        s.insert(x);
    }
    for (auto i:s) {
        for (int o = 0; o < 33; o++) {
            int flag1 = s.count(i - (1 << o));
            int flag2 = s.count(i + (1 << o));
            if (flag1 && flag2) {
                cout << 3 << endl << (i - (1 << o)) << " " << i << " " << (i + (1 << o)) << endl;
                return 0;
            }
            else if (flag1) {
                if (!v.size()) {
                    v.push_back(i);
                    v.push_back(i - (1 << o));
                }
            }
            else if (flag2) {
                if (!v.size()) {
                    v.push_back(i);
                    v.push_back(i + (1 << o));
                }
            }
        }
    }
    if (v.size()) {
        cout << 2 << endl;
        for (auto i: v) cout << i << " ";
    }
    else cout << 1 << endl << *s.begin();
}

原文地址:https://www.cnblogs.com/EDGsheryl/p/9153683.html

时间: 2024-12-17 15:46:32

Codeforces Round #486 (Div. 3) D. Points and Powers of Two的相关文章

Codeforces Round #245 (Div. 2) A - Points and Segments (easy)

水到家了 #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Point{ int index, pos; Point(int index_ = 0, int pos_ = 0){ index = index_; pos = pos_; } bool operator < (const Point& a) const{ return p

Codeforces Round #486 (Div. 3) C. Equal Sums

Codeforces Round #486 (Div. 3) C. Equal Sums 题目连接: http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/C Description You are given k sequences of integers. The length of the i-th sequence equals to ni. You have to choose exactly two sequences

Codeforces Round #486 (Div. 3) A. Diverse Team

Codeforces Round #486 (Div. 3) A. Diverse Team 题目连接: http://codeforces.com/contest/988/problem/A Description There are n students in a school class, the rating of the i-th student on Codehorses is ai. You have to form a team consisting of k students

Codeforces Round #486 (Div. 3) B. Substrings Sort

Codeforces Round #486 (Div. 3) B. Substrings Sort 题目连接: http://codeforces.com/contest/988/problem/B Description You are given n strings. Each string consists of lowercase English letters. Rearrange (reorder) the given strings in such a way that for e

Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/E Description Polycarp lives on a coordinate line at the point x=0. He goes to his friend that lives at the point x=a. Polycarp can

Codeforces Round #486 (Div. 3)

988A.http://codeforces.com/contest/988/problem/A 题意:给出n个数,让你从中取出m个不同的数组成一组 分析:模拟即可.当每个人为第一次出现时,标记这个位置可取.最后从可取的位置取m个即可 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<vector> 5 using namespace std; 6 const in

Codeforces Round #319 (Div. 2) E - Points on Plane

题目大意:在一个平面里有n个点,点坐标的值在1-1e6之间,让你给出一个遍历所有点的顺序,要求每个点走一次,且 曼哈顿距离之和小于25*1e8. 思路:想了一会就有了思路,我们可以把1e6的x,y坐标都分成2000份,每份500,然后这样整个平面就被分成 了2000*2000个区域,然后按区域输出点就行了. 1 #include<bits/stdc++.h> 2 using namespace std; 3 int n; 4 vector<int> ans[2005][2005];

Codeforces Round #486 (Div. 3) E. Divisibility by 25

E. Divisibility by 25 能被25整除的充要条件就是末两位是00,25,50,75.如果没有过程中不出现前导0这一限制,显然对每种情况,贪心取尽量低位即可.本题的关键就在于如何满足这个条件,首先有个"显然"的方法:讨论...然后会发现情况太多,过于复杂.所以,我们只好从交换本身的性质入手,找找易于实现的写法.注意到我们最多移动3个数字的位置,最终两个最低位的数,可能还有一个非0数作为最高位,而根据交换的性质,可以发现先移动那个数对于最终的结果没有影响,按照题意我们要先

Codeforces Round #486 (Div. 3)988E. Divisibility by 25技巧暴力||更暴力的分类

传送门 题意:给定一个数,可以对其做交换相邻两个数字的操作.问最少要操作几步,使得可以被25整除. 思路:问题可以转化为,要做几次交换,使得末尾两个数为00或25,50,75: 自己一开始就是先for一遍,记录四种可能对于的步数,再对四种可能讨论(有前导0的情况):自己是在数据中,该对了自己的代码, 看了队长和%王宣凯的代码,觉得那才是现场能ac的思路.--暴力交换: #include <iostream> #include <cstdio> #include <algori