Codeforces 1005C(贪心)

传送门

题面:

C. Summarize to the Power of Two
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A sequence a1,a2,,ana1,a2,…,an is called good if, for each element aiai, there exists an element ajaj (iji≠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.

Input

The first line contains the integer nn (1n1200001≤n≤120000) — the length of the given sequence.

The second line contains the sequence of integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109).

Output

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.

Examples
input
Copy
6
4 7 1 5 4 9
output
Copy
1
input
Copy
5
1 2 3 4 5
output
Copy
2
input
Copy
1
16
output
Copy
1
input
Copy
4
1 1 1 1023
output
Copy
0
Note

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.

题目描述:

    给你一个大小为n的数组,问你对于每个数ai,使得ai+aj都为2的幂,问你不符合条件的个数。

题目分析:

    事实上这个题跟之前的CF1003D很相似。因为要求我们判断是否和为2的幂,因此我们可以贪心的从2^30次方往下进行枚举,如果数组中的每一个数ai,在数组中存在j-ai的话,则证明符合条件,反之则不符合。

    而因为题目的数据范围较大,因此我们需要开map来进行存储。

    值得注意的是,因为题目要保证ai与bi不相同,因此得小心处理好ai与bi相同的情况,防止多解。

代码:

#include <bits/stdc++.h>
#define maxn 120005
using namespace std;
typedef long long ll;
map<ll,ll>mp;
map<ll,bool>vis;
ll a[maxn];
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        scanf("%I64d",&a[i]);
        mp[a[i]]++;
    }
    sort(a,a+n);
    int cnt=0;
    for(int i=0;i<n;i++){
        if(vis[a[i]]) continue;
        bool flag=true;
        for(ll j=1<<30;j>=1;j>>=1){
            if(j<a[i]) break;
            if(mp.count(j-a[i])){//如果在map中存在j-a[i],则证明原数列中存在j-a[i],则a[i]符合条件
                if(j-a[i]==a[i]&&mp[j-a[i]]==1) continue;//如果j-a[i]与a[i]相等,同时map中a[i]只有一个,则证明不能符合条件
                vis[a[i]]=1;
                vis[j-a[i]]=1;
                flag=false;
                break;
            }
        }
        if(flag) cnt++;//不符合则答案+1
    }
    cout<<cnt<<endl;
}

猜你喜欢

转载自blog.csdn.net/weixin_39453270/article/details/81004271