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个房间, ...
随机推荐
- C++写动态站点之HelloWorld!
演示样例源码下载地址:Fetch_Platform.7z 更复杂的代码可參考本博客BBS的实现 简单的说.动态站点就是能够动态变更的站点.动态变化的内容通常来自后端数据库.例如以下省略万字(动态站点) ...
- LeetCode(21)题解:Merge Two Sorted Lists
https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked lists and return it as ...
- java获取class的几种方式
以获取Hello.class为例 public class Hello { public static void main(String[] args) { // TODO Auto-generate ...
- mongo-spark 安装排故 ./sbt check
[error] at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:) [error] at com.mong ...
- 几个 PHP 的"魔术常量"
__LINE__ 文件中的当前行号. __FILE__ 文件的完整路径和文件名.如果用在被包含文件中,则返回被包含的文件名.自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对路径(如果是符 ...
- bashdb bashdebug
sudo apt-get install bashdb bashdb --debug 一.列出代码和查询代码类: l 列出当前行以下的10行 - 列出正在执行的代码行的前面10行 . 回到正在执行 ...
- ios对于枚举的使用
引言: 枚举值 它是一个整形(int) 并且,它不参与内存的占用和释放,枚举定义变量即可直接使用,不用初始化. 在代码中使用枚举的目的只有一个,那就是增加代码的可读性. 使用: 枚举的定义如下: t ...
- JVM Safepoint 安全点
一.什么是安全点: 在可达性分析算法中查找存活的对象,首先要找到哪些是GC Roots: 有两种查找GC Roots的方法: 一种是遍历方法区和栈区来查找(保守式GC): 一种是通过OopMap的数据 ...
- ref 与 $refs 如何关联
先问大家一个简单的问题: 还有人记得 jquery 里面的 data 方法是如何让 DOM 节点绑定对应的数据对象的吗 有时候我们做节点关联设计的思路其实有一点类似,但是在 vue 里面多了很多概念, ...
- HDU3613 Best Reward —— Manacher算法 / 扩展KMP + 枚举
题目链接:https://vjudge.net/problem/HDU-3613 Best Reward Time Limit: 2000/1000 MS (Java/Others) Memor ...