Codeforces 702B【二分】
题意:
给一个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【二分】的更多相关文章
- 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, ...
- CodeForces - 363D --二分和贪心
题目:CodeForces - 363D 题意:给定n个学生,其中每个学生都有各自的私己钱,并且自己的私己钱只能用在自己买自行车,不能给别人. 给定m个自行车,每个自行车都有一个价格. 给定公有财产a ...
- 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 ...
- Codeforces 732D [二分 ][贪心]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: n天进行m科考试,每科考试需要a的复习时间,n天每天最多可以考一科.并且指定哪天考哪科. 注意考试那天不能复习. 问最少需要多少天可全部通过考试. ...
- codeforces 359D 二分答案+RMQ
上学期刷过裸的RMQ模板题,不过那时候一直不理解>_< 其实RMQ很简单: 设f[i][j]表示从i开始的,长度为2^j的一段元素中的最小值or最大值 那么f[i][j]=min/max{ ...
- CodeForces - 551C 二分+贪心
题意:有n个箱子形成的堆,现在有m个学生,每个学生每一秒可以有两种操作: 1: 向右移动一格 2: 移除当前位置的一个箱子 求移除所有箱子需要的最短时间.注意:所有学生可以同时行动. 思路:二分时间, ...
- Robin Hood CodeForces - 672D (二分)
大意: 给定数组$a$, 每次操作使最大元素减小1最小元素增大1, 求k次操作后最大值与最小值的差. 二分出k次操作后最大值的最小值以及最小值的最大值, 若和能平分答案即为$max(0,R-L)$, ...
- 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 ...
- Enduring Exodus CodeForces - 655C (二分)
链接 大意: n个房间, 1为占用, 0为未占用, John要将k头奶牛和自己分进k+1个空房间, 求John距最远的奶牛距离的最小值 这种简单题卡了20min.... 显然对于固定的k+1个房间, ...
随机推荐
- SolidEdge如何复制特征 建立类似于UG 块的概念
直接Ctrl+C和Ctrl+V可以实现特征的复制粘贴 按N键可以改变特征方向 已经复制完成的特征要进行定位,则右击该特征,编辑轮廓,可以进行聪慧尺寸的标注 使用特征库的方式,就像UG的块一样, ...
- leetcode Valid Palindrome C++&python 题解
题目描写叙述 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- Webservice WCF WebApi 前端数据可视化 前端数据可视化 C# asp.net PhoneGap html5 C# Where 网站分布式开发简介 EntityFramework Core依赖注入上下文方式不同造成内存泄漏了解一下? SQL Server之深入理解STUFF 你必须知道的EntityFramework 6.x和EntityFramework Cor
Webservice WCF WebApi 注明:改编加组合 在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在.net平台下, ...
- HDU 2669 Romantic(扩展欧几里德)
题目链接:pid=2669">http://acm.hdu.edu.cn/showproblem.php?pid=2669 Problem Description The Sky is ...
- 关于使用openfiler作为共享存储来安装rac时的问题
关于使用openfiler作为共享存储来安装rac时的问题 第一:一定要使用openfiler-2.3-x86-disc1.iso这个版本号的openfiler,不要使用其它版本号的openfiler ...
- Drupal 7模板(主题钩子)的建议
这一块的内容很多其它的讲的是样例.所以这里请直接稳步官方站点查看吧.链接 https://drupal.org/node/1089656
- Python2.7安装教程
作者:zhanhailiang 日期:2014-11-16 [root@~/software]# yum install bzip* [root@~/software]# wget http://ww ...
- dubbo简单配置与使用
dubbo简介 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 当网站流量很小时 ...
- Apach Web Server区别于其他应用服务器的主要特点是什么?
Web服务器一般指的是处理静态请求或转发http请求的服务器,而应用服务器一般是用来处理动态请求的服务器.
- 微软下一代站点开发框架:ASP.NET MVC 6 新特性揭秘
国内第一个<微软下一代站点开发框架:ASP.NET MVC 6 新特性揭秘 >课程 微软特邀讲师 徐雷!周六晚8点YY预定:id=28447" href="htt ...