Summarize to the Power of Two(map+思维)
A sequence a1,a2,…,ana1,a2,…,an is called good if, for each element aiai, there exists an element ajaj (i≠ji≠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.
The first line contains the integer nn (1≤n≤1200001≤n≤120000) — the length of the given sequence.
The second line contains the sequence of integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).
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.
6
4 7 1 5 4 9
1
5
1 2 3 4 5
2
1
16
1
4
1 1 1 1023
0
In he first example, it is enough to delete one element a4=5. The remaining elements form the sequence [4,7,1,4,9][4,7,1,4,9], which is good.
题目意思:给你一个数列,然后让你删掉某个数,让每一个数都可以与另外一个数相加,之和等于2的^d次方,问最少删掉的个数
解题思路:先预处理出2的次幂,然后暴力枚举出每个数与每个2的次幂之差,去判断在map中是否有这样的差存在(注意相同的两个数,比如4 ,4 这个时候就可以特判一下,出现次数),若不存在那么就需要删除掉这个数。
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
#define ll long long int
using namespace std;
ll d[];
ll a[];
map<ll,ll>mp;
void double_2()
{
int i;
d[]=;
for(i=; i<=; i++)
{
d[i]=d[i-]*;
}
}
int main()
{
int i,j,flag,n;
ll k;
double_2();///2的幂
while(scanf("%d",&n)!=EOF)
{
int count=;
mp.clear();///map清零
for(i=; i<n; i++)
{
scanf("%lld",&a[i]);
mp[a[i]]++;///记录出现的次数
}
for(i=; i<n; i++)
{
flag=;
for(j=; j<=; j++)
{
if(a[i]>=d[j])
{
continue;
}
else
{
k=d[j]-a[i];
if(mp[k])///map中存在
{
if(a[i]==k)
{
if(mp[k]>=)///map中需要至少两个
{
flag=;
break;
}
}
else
{
flag=;
break;
}
}
}
}
if(!flag)
{
count++;
}
}
printf("%d\n",count);
}
return ;
}
Summarize to the Power of Two(map+思维)的更多相关文章
- 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 inpu ...
- CF 1005C Summarize to the Power of Two 【hash/STL-map】
A sequence a1,a2,-,an is called good if, for each element ai, there exists an element aj (i≠j) such ...
- [USACO12NOV]同时平衡线Concurrently Balanced Strings DP map 思维
题面 [USACO12NOV]同时平衡线Concurrently Balanced Strings 题解 考虑DP. \(f[i]\)表示以\(i\)为左端点的合法区间个数.令\(pos[i]\)表示 ...
- Bracket Sequences Concatenation Problem括号序列拼接问题(栈+map+思维)
A bracket(括号) sequence is a string containing only characters "(" and ")".A regu ...
- Codeforces 755B:PolandBall and Game(map+思维)
http://codeforces.com/problemset/problem/755/B 题意:A可以喊出n个字符串,B可以喊出m个字符串,如果一个字符串之前被喊过,那么它再也不能喊了,A先喊,最 ...
- mind map 思维导图
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- HDU 6623 Minimal Power of Prime(思维)题解
题意: 已知任意大于\(1\)的整数\(a = p_1^{q_1}p_2^{q_2} \cdots p_k^{q_k}\),现给出\(a \in [2,1e18]\),求\(min\{q_i\},q ...
- IDE引入mindmap插件,在项目中添加思维导图
1.打开IDE,file--settings--plugins,搜索IDEA Mind Map 2.点击install,进行下载,然后按照提示restart重启IDEA,安装完成 3.创建mind m ...
- Codeforces Round #496 (Div. 3) ABCDE1
//B. Delete from the Left #include <iostream> #include <cstdio> #include <cstring> ...
随机推荐
- codeforces 979 C. Kuro and Walking Route
C. Kuro and Walking Route time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- The Bitizens Team
bitizens.bitguild.com 首个区块链3D艺术品. https://mybitizens.bitguild.com/#/igo https://www.youtube.com/watc ...
- Ubuntu16.04上安装neo4j数据库
什么是neo4j数据库? neo4j数据库是图数据库的一种,属于nosql的一种,常见的nosql数据库还有redis.memcached.mongDB等,不同于传统的关系型数据库,nosql数据也有 ...
- PHP的strtotime()函数2038年bug问题
最近在开发一个订单查询模块的时候,想当然的写了个2099年的日期,结果PHP返回了空值,肯定是发生溢出错误了,搜索了网上,发现下面这篇文章,但是我的问题依然没有解决,要怎么得到2038年以后的时间戳呢 ...
- MFC 程序退出方法
基於對話框的: 1.PostQuitMessage(0);2.PostMessage(WM_QUIT,0,0);3.ExitProcess(0);注意使用时先释放分配的内存,以免造成内存泄露4.exi ...
- 执行Go程序的三种方式及Go语言关键字
执行 Go 程序的三种方式及 Go 语言关键字 执行 Go 程序的三种方式 一.使用 go run 命令 二.使用 go build 命令 Step1. 对 go 源码源文件执行 go build 命 ...
- android studio 插件开发(自动生成框架代码插件)
android studio 插件开发 起因 去年公司开始上新项目,正好android在架构这方面的讨论也开始多了起来,于是mvp架构模型就进入我们技术选择方案里面,mvp有很多好处,但是有一个非常麻 ...
- windows server dump文件
1. mini dump: ***** 需要包含 dbghelp.dll 库 ****mini_dump.h文件: // reference:https://msdn.microsoft.com/zh ...
- Testing for diversifying selection for two clades with a background clade
在利用branch-site检测趋同进化的时候 .可以将各个趋同进化枝分别进行检测,分析的时候不去除某一趋同枝系 .在分析的时候,需要去除其他趋同枝系的影响 I have sequences of a ...
- 20155211 2016-2017-2 《Java程序设计》第一周学习总结
20155211 2006-2007-2 <Java程序设计>第1周学习总结 教材学习内容总结 首先根据博客上的指导安装了jdk,并且首次尝试了设置环境变量path和classpath. ...