• 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 }
  • 相关阅读:
    uva11025 The broken pedometer
    uva131 The Psychic Poker Player
    子集生成算法
    uva10167 Birthday Cake
    poj1129 Channel Allocation
    poj2676 Sudoku
    Emacs杂谈(一)Emacs环境 c++ 快捷键
    poj1416 Shredding Company
    英文报刊推荐
    搜索练习(二)工作效益
  • 原文地址:https://www.cnblogs.com/ZhenghangHu/p/9129422.html
Copyright © 2020-2023  润新知