Codeforces 988D Points and Powers of Two 【性质】【卡常】

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

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 }

原文地址:https://www.cnblogs.com/ZhenghangHu/p/9129422.html

时间: 2024-11-10 08:15:22

Codeforces 988D Points and Powers of Two 【性质】【卡常】的相关文章

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

Codeforces 19D Points 线段树+set

题目链接:点击打开链接 线段树维护y值大于val的最小x值 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <map> #include <set> #include <math.h> using namespace std; #define inf

Codeforces 19D Points(树状数组)

题目链接:Codeforces 19D Points 题目大意:N中操作,每次添加一个点,或者删除一个点,以及找到给定x,y坐标最近的一个坐标,并且保证xi,yi在x,y的右上角. 解题思路:这题的解法还是很机智的. y坐标离散化,然后树状数组的每个单位用一个set代替,set记录的是点集. 剩下的操作就像树状数组一样,每次添加就等于是+w的操作,移除就等于是-w,只是w是一个点,那么find操作就等于是在sum操作生成的点集中二分查找. #include <cstdio> #include

Codeforces 430A Points and Segments (easy)

题意:让你染色点,要求在给出的区间内|红色个数-蓝色个数|<=1 思路:排序后依次交替染色就能达到效果 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <vector> using namespace std; const int MAXN = 110; int arr[MAXN]; int n,m,x,y; int

CodeForces 251A. Points on Line(数学 lower_bound )

题目链接:http://codeforces.com/problemset/problem/251/A Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance be

CodeForces A. Points in Segments

http://codeforces.com/contest/1015/problem/A You are given a set of nn segments on the axis OxOx, each segment has integer endpoints between 11 and mm inclusive. Segments may intersect, overlap or even coincide with each other. Each segment is charac

CodeForces 19D Points(离散化+线段树+单点更新)

题目链接: huangjing 题意:给了三种操作 1:add(x,y)将这个点加入二维坐标系 2:remove(x,y)将这个点从二维坐标系移除. 3:find(x,y)就是找到在(x,y)右上方的第一个点. 思路:我们可以建立n个set以x为横坐标,那么我们这个题就转化为找一个最小的x是否存在满足条件,那么x一旦被找到,那么纵坐标就自然而然的找到了,当然更新操作就是对maxy的维护,然后查询操作就是找出一个最小的x..还有因为n非常大,所以要采用离散化的方法,然后进行离线处理.还是就是掌握s

codeforces 251A Points on Line(二分or单调队列)

Description Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't e

Codeforces 870E Points, Lines and Ready-made Titles:并查集【两个属性二选一】

题目链接:http://codeforces.com/problemset/problem/870/E 题意: 给出平面坐标系上的n个点. 对于每个点,你可以画一条经过这个点的横线或竖线或什么都不画. 两条重合的直线算作一条直线. 问你能画出多少种不同的图案. 题解: 将所有横坐标或纵坐标相同的两点之间连边. 对于一个连通块,设这个连通块中不同的横坐标个数为sx,不同的纵坐标个数为sy. 有可能画出的线的个数即为sx + sy. 可以发现,如果一个联通块中有环(即siz[fa] >= sx +