题意:

给一个a数组,输出有多少对相加是等于2^x的。1<=a[i]<=1e9,n<=1e5

思路:

a[i]+a[j]=2^x 对于每个a[i],枚举x,然后二分查找a[j];

ps:

①:数组大的简单题,基本上就是二分这种降低复杂度;

②:对于a+b=c,三个对象都有考虑的意义;

加一发挫code……

#include<cstdio>
#include<iostream>
#include<queue>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define eps 1e-8
typedef __int64 LL; const int N=1e5+10;
int a[N]; int t[35];
void init()
{
for(int i=0;i<31;i++)
t[i]=1<<i;
} int main()
{
init();
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n); int temp,s,e,mid;
LL ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<31;j++)
{
temp=t[j]-a[i];
if(temp<1||temp>1e9)
continue;
s=lower_bound(a+i+1,a+n,temp)-a;//第一个等于temp的位置;
e=upper_bound(a+i+1,a+n,temp)-a;//大于temp的位置;
ans+=e-s;
}
}
printf("%lld\n",ans);
return 0;
}

其实更好利用那个等式a+b=c;

我们可以直接标记数组元素的个数,然后中间直接操作。

再加一发挫code…….

#include<cstdio>
#include<iostream>
#include<map>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define eps 1e-8
typedef __int64 LL; map<int,int>cnt; int main()
{
LL ans=0;
int n;
int x,t;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&t);
for(int j=0;j<31;j++)
{
x=1<<j;
if(x>t)
ans+=cnt[x-t];
}
cnt[t]++;
}
printf("%I64d\n",ans);
return 0;
}

Codeforces 702B【二分】的更多相关文章

  1. CodeForces 702B Powers of Two【二分/lower_bound找多少个数/给出一个数组 求出ai + aj等于2的幂的数对个数】

    B. Powers of Two   You are given n integers a1, a2, ..., an. Find the number of pairs of indexes i,  ...

  2. CodeForces - 363D --二分和贪心

    题目:CodeForces - 363D 题意:给定n个学生,其中每个学生都有各自的私己钱,并且自己的私己钱只能用在自己买自行车,不能给别人. 给定m个自行车,每个自行车都有一个价格. 给定公有财产a ...

  3. Codeforces Round #425 (Div. 2) Problem C Strange Radiation (Codeforces 832C) - 二分答案 - 数论

    n people are standing on a coordinate axis in points with positive integer coordinates strictly less ...

  4. Codeforces 732D [二分 ][贪心]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意: n天进行m科考试,每科考试需要a的复习时间,n天每天最多可以考一科.并且指定哪天考哪科. 注意考试那天不能复习. 问最少需要多少天可全部通过考试. ...

  5. codeforces 359D 二分答案+RMQ

    上学期刷过裸的RMQ模板题,不过那时候一直不理解>_< 其实RMQ很简单: 设f[i][j]表示从i开始的,长度为2^j的一段元素中的最小值or最大值 那么f[i][j]=min/max{ ...

  6. CodeForces - 551C 二分+贪心

    题意:有n个箱子形成的堆,现在有m个学生,每个学生每一秒可以有两种操作: 1: 向右移动一格 2: 移除当前位置的一个箱子 求移除所有箱子需要的最短时间.注意:所有学生可以同时行动. 思路:二分时间, ...

  7. Robin Hood CodeForces - 672D (二分)

    大意: 给定数组$a$, 每次操作使最大元素减小1最小元素增大1, 求k次操作后最大值与最小值的差. 二分出k次操作后最大值的最小值以及最小值的最大值, 若和能平分答案即为$max(0,R-L)$, ...

  8. Queries about less or equal elements CodeForces - 600B(二分)

    You are given two arrays of integers a and b. For each element of the second arraybj you should find ...

  9. Enduring Exodus CodeForces - 655C (二分)

    链接 大意: n个房间, 1为占用, 0为未占用, John要将k头奶牛和自己分进k+1个空房间, 求John距最远的奶牛距离的最小值 这种简单题卡了20min.... 显然对于固定的k+1个房间, ...

随机推荐

  1. JavaScript - 正则表达式解惑

    正则表达式手册: http://tool.oschina.net/uploads/apidocs/jquery/regexp.html 正则表达式测试地址: http://tool.chinaz.co ...

  2. 从Nginx源代码谈大写和小写字符转化的最高效代码以及ASCII码表的科学

    说起大写和小写字母转换.大家非常easy想起系统函数是不是,差点儿全部的编程语言都提供了这样的转换函数,可是你有没有想过这背后是怎么实现的? 让你写怎么实现? 我们都知道Nginx是眼下用的最多的Ht ...

  3. C#连接数据库 增删改查

  4. POJ 2184 Cow Exhibition (01背包变形)(或者搜索)

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10342   Accepted: 4048 D ...

  5. 理解OpenStack认证:Keystone PKI

    原文链接: https://www.mirantis.com/blog/understanding-openstack-authentication-keystone-pki/ The latest ...

  6. ios+Appium+Java

    To run iOS tests, you can follow these steps : (Note : I am using Java language here in Eclipse IDE ...

  7. Android Baseline小tip

    转载请注明出处:http://blog.csdn.net/bbld_/article/details/40709353 Baseline Alignment

  8. leetcode笔记:Pascal&#39;s Triangle

    一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...

  9. MySQL安装、安装时未提示输入密码、如何修改密码小结

    http://blog.csdn.net/fr555wlj/article/details/54971412

  10. Spark基本原理

    仅作<Spark快速大数据分析>学习笔记 定义:Spark是一个用来实现 快速 而 通用 的集群计算平台:(通用的大数据处理引擎:) 改进了原Hadoop MapReduce处理模型,体现 ...