zoukankan      html  css  js  c++  java
  • Codeforces 988D Points and Powers of Two 【性质】【卡常】

    这道题关键在于想到两个性质,想到就好做了。这还是我做过的第一道卡常题

    1.满足题目中条件的子集,其中元素个数不能大于3

    2.如果最大子集为3的话,那一定是x-2^i,  x, x+2^i的形式,我们枚举x就好了,然后i的次数是log10^9;如果最大子集是2,那就是x,x+2^i的形式,同样枚举x;如果最大子集是1,输出a[1]就行

    可以用set来判断一个数存不存在

    整体复杂度是O(n*logn*log10^9)

     1 #include<iostream>
     2 #include<set>
     3 using namespace std;
     4 
     5 int a[200005];
     6 set<int> m;
     7 
     8 int main(){
     9     int n,size=0; cin>>n;
    10     int x1,x2;
    11     for(int i=1;i<=n;i++) {
    12         cin>>a[i];
    13         m.insert(a[i]);
    14     }
    15     for(int i=1;i<=n;i++){
    16         for(int j=0;j<31;j++) {
    17             if( m.count( a[i]+(1<<j)  ) && m.count( a[i]-(1<<j)  ) ){
    18                 cout<<3<<endl;
    19                 cout<<a[i]<<" "<<a[i]+(1<<j)<<" "<<a[i]-(1<<j);
    20                 return 0;
    21             }
    22             if( m.count( a[i]+(1<<j)  ) && size==0) {
    23                 size = 2;
    24                 x1 = a[i];
    25                 x2 = a[i] + (1 << j);
    26             }
    27         }
    28     }
    29     if(size==2) cout<<2<<endl<<x1<<" "<<x2;
    30     else cout<<1<<endl<<a[1];
    31 
    32     return 0;
    33 }
  • 相关阅读:
    16-面向对象之语法(1)
    4-编辑器IDE_for_python
    3-python入门学习路线
    2-学习方法心得
    1-语法基础
    NSMutableArray基本概念
    NSArray 与字符串
    NSArray文件读写
    NSArray排序
    NSArray 遍历
  • 原文地址:https://www.cnblogs.com/ZhenghangHu/p/9129422.html
Copyright © 2011-2022 走看看