zoukankan      html  css  js  c++  java
  • CF1005C Summarize to the Power of Two 暴力 map

    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
    6
    4 7 1 5 4 9
    output
    1
    input
    5
    1 2 3 4 5
    output
    2
    input
    1
    16
    output
    1
    input
    4
    1 1 1 1023
    output
    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个数,问去掉几个数后,每个数都被用上与另一个数和为2的幂数

    分析:直接暴力枚举每个二的幂数减去当前数的差是否存在于这个数列中,用map存下每个数

    #include <map>
    #include <set>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <cstdio>
    #include <vector>
    #include <string>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define debug(a) cout << #a << " " << a << endl
    using namespace std;
    const int maxn = 2e5 + 10;
    const int mod = 1e9 + 7;
    typedef long long ll;
    ll a[maxn], vis[maxn];
    int main() {
        ll n;
        while( cin >> n ) {
            map<ll,ll> mm;
            for( ll i = 0; i < n; i ++ ) {
                cin >> a[i];
                mm[a[i]] ++;
            }
            ll cnt = 0;
            for( ll i = 0; i < n; i ++ ) {
                bool flag = false;
                for( ll j = 1<<30; j >= 1; j /=2 ) {
                    if( j > a[i] ) {
                        if( ( a[i] == j/2 && mm[j-a[i]] > 1 ) || ( mm[j-a[i]] > 0 && a[i] != j/2 ) ) {
                            flag = true;
                            break;
                        }
                    }
                }
                if( !flag ) {
                    //debug(a[i]), debug(i);
                    cnt ++;
                }
            }
            cout << cnt << endl;
        }
        return 0;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    表数据文件DBF的读取和写入操作
    【转】Hive SQL的编译过程
    Hadoop配置机架感知
    pssh安装和使用
    hive12启动报错org.apache.thrift.server.TThreadPoolServer.<init>(Lorg/apache/thrift/server/TThreadPoolServer$Args;)
    【转】Oozie4.2.0配置安装实战
    【转】别因为要学的太多反而压垮自己
    【转】hadoop2.6 配置lzo压缩
    hive 启动 Specified key was too long; max key length is 767 bytes解决办法
    【转】linux configure报错configure: error: C++ preprocessor “/lib/cpp” fails sanity 的解决办法
  • 原文地址:https://www.cnblogs.com/l609929321/p/9313643.html
Copyright © 2011-2022 走看看