概述
A sequence a1,a2,…,ana1,a2,…,an is called good if, for each element aiai, there exists an element ajaj (i≠ji≠j) such that ai+ajai+aj is a power of two (that is, 2d2d for some non-negative integer dd).
For example, the following sequences are good:
- [5,3,11][5,3,11] (for example, for a1=5a1=5 we can choose a2=3a2=3. Note that their sum is a power of two. Similarly, such an element can be found for a2a2 and a3a3),
- [1,1,1,1023][1,1,1,1023],
- [7,39,89,25,89][7,39,89,25,89],
- [][].
Note that, by definition, an empty sequence (with a length of 00) is good.
For example, the following sequences are not good:
- [16][16] (for a1=16a1=16, it is impossible to find another element ajaj such that their sum is a power of two),
- [4,16][4,16] (for a1=4a1=4, it is impossible to find another element ajaj such that their sum is a power of two),
- [1,3,2,8,8,8][1,3,2,8,8,8] (for a3=2a3=2, it is impossible to find another element ajaj such that their sum is a power of two).
You are given a sequence a1,a2,…,ana1,a2,…,an. What is the minimum number of elements you need to remove to make it good? You can delete an arbitrary set of elements.
The first line contains the integer nn (1≤n≤1200001≤n≤120000) — the length of the given sequence.
The second line contains the sequence of integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).
Print the minimum number of elements needed to be removed from the given sequence in order to make it good. It is possible that you need to delete all nn elements, make it empty, and thus get a good sequence.
6 4 7 1 5 4 9
1
5 1 2 3 4 5
2
1 16
1
4 1 1 1 1023
0
In the first example, it is enough to delete one element a4=5a4=5. The remaining elements form the sequence [4,7,1,4,9][4,7,1,4,9], which is good.
题意:给一组数据 , 如果一个数和其他数相加,不是2的某一次方,那么就删去这个数,统计需要删去的个数。
思路:因为 2^28 > 1e9. 因此我们先用一个数组把 2 的这些次方存下来。
把输入的数用map标记一下,这样我们在后面循环找的时候,如果它被标记了就说明存在 , 没有的话就让ans++即可。。。。
所以这题也可以用二分写?
emmm.......这样的时候
4 7 1 5 4 9
如果那个数本身就是2的某一次方,比如4 ,那么M[4]至少要2个才可以
如果不是,只要M[t]非0 即可。
下面代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 120000 + 10;
int a[maxn];
int p[maxn];
vector<int>V;
map<int,int>M;
int cnt = 0;
void init()
{
for(int i = 0 ; i<=30 ; i++) p[i] = 1<<i;
}
int main()
{
int n;
scanf("%d",&n);
init();
M.clear();
for(int i = 0 ; i < n ; i++)
{
scanf("%d",&a[i]);
if(!M.count(a[i]))
{
M[a[i]] = 1;
}
else
{
M[a[i]] ++;
}
}
int ans = 0;
for(int i = 0 ; i < n ; i++)
{
int j;
for(j = 0 ; j <= 30 ; j++)
{
int t = p[j] - a[i];
if(t == a[i])
{
if(M[t]>=2)
{
break;
}
}
else
{
if(M[t])
{
break;
}
}
}
if(j == 31)
{
ans++;
}
}
cout<<ans<<endl;
}
最后
以上就是勤奋钥匙为你收集整理的Codeforces Round #496 (Div. 3)--C. Summarize to the Power of Two(没错!用的map!!,哇咔咔咔~~)的全部内容,希望文章能够帮你解决Codeforces Round #496 (Div. 3)--C. Summarize to the Power of Two(没错!用的map!!,哇咔咔咔~~)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复