zoukankan      html  css  js  c++  java
  • 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=2k,a-c=2i,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();
    }
    
  • 相关阅读:
    xcode5 ios6程序图标去掉阴影
    ios程序启动时去掉状态栏
    ios启动图标程序适配
    c语言基础:各种数据类型的输出占位符
    c语言变量作用域问题
    JS 学习笔记--11---内置对象(Global/Math)
    JS 学习笔记--10---基本包装类型
    JS 学习笔记--9---变量-作用域-内存相关
    JS 学习笔记--8---Function类型
    JS 学习笔记--6---日期和时间
  • 原文地址:https://www.cnblogs.com/EDGsheryl/p/9153683.html
Copyright © 2011-2022 走看看