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个房间, ...
随机推荐
- [WASM] Access WebAssembly Memory Directly from JavaScript
While JavaScript has a garbage-collected heap, WebAssembly has a linear memory space. Nevertheless u ...
- 三期_day05_Dao层的准备工作_II
工作文件夹: 实体类:UserInfo.java package com.yc.crm.entity; import java.util.Date; public class UserInfo { p ...
- cocos2d-x改底层之获取UIListView的实际内容大小
实际项目中UI界面中常常会用到UIListView.大多会在CocoStudio中直接加入这个控件. 可是在使用中发现了一些坑和功能缺乏,然后就看了一下底层的逻辑,发现略微改一下底层就能够满足需求,所 ...
- C#语言 ArrayList集合
- 豆瓣面试题strstr)
/*(豆瓣2013面试题strstr) 有一个在给定字符串中查找子串的函数strstr, 该函数从给定的字符串src中查找substr并返回一个整数, 指明substr第一次出现的位置(从0开始计数) ...
- java-冒泡
一.java冒泡排序 冒泡排序(Bubble Sort)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要 ...
- Spring中的事务管理(学习笔记)
什么是事物? 事物是指逻辑上的一组操作,这组操作要么全部成功,要么全部失败. 事物的特性: 原子性.一致性.隔离性.持久性 Spring事务管理的高级接口: PlatformTransactionMa ...
- jQuery.ajaxSetup()
jQuery.ajaxSetup()函数用于设置AJAX的全局默认设置. 该函数用于更改jQuery中AJAX请求的默认设置选项.之后执行的所有AJAX请求,如果对应的选项参数没有设置,将使用更改后的 ...
- 【翻译自mos文章】即使resource_limit = false, password的 资源限制也会生效
即使resource_limit = false, password的 资源限制也会生效 參考原文: Resource limits for passwords work even with reso ...
- Servlet session的理解
servlet参见http://blog.csdn.net/bryanliu1982/article/details/5214899 session参见http://lavasoft.blog.51c ...