Codeforces 988D Points and Powers of Two 【性質】【卡常】
阿新 • • 發佈:2018-06-03
cout force 大於 codeforce ces com CI ORC size
這道題關鍵在於想到兩個性質,想到就好做了。這還是我做過的第一道卡常題
1.滿足題目中條件的子集,其中元素個數不能大於3
2.如果最大子集為3的話,那一定是x-2^i, k, x+2^i的形式,我們枚舉x就好了,然後i的次數是log10^9;如果最大子集是2,那就是x,x+2^i的形式,同樣枚舉x;如果最大子集是1,輸出a[1]就行
整體復雜度是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 }
Codeforces 988D Points and Powers of Two 【性質】【卡常】