博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Powers of two(multiset的运用)
阅读量:3898 次
发布时间:2019-05-23

本文共 1903 字,大约阅读时间需要 6 分钟。

【题目】

B - Powers of two


Time limit : 2sec / Memory limit : 1024MB

Score : 600 points

Problem Statement

Takahashi has N balls with positive integers written on them. The integer written on the i-th ball is Ai. He would like to form some number of pairs such that the sum of the integers written on each pair of balls is a power of 2. Note that a ball cannot belong to multiple pairs. Find the maximum possible number of pairs that can be formed.

Here, a positive integer is said to be a power of 2 when it can be written as 2t using some non-negative integer t.

Constraints

  • 1≤N≤2×105
  • 1≤Ai≤109
  • Ai is an integer.

Input

Input is given from Standard Input in the following format:

NA1 A2 … AN

Output

Print the maximum possible number of pairs such that the sum of the integers written on each pair of balls is a power of 2.

Sample Input 1

31 2 3

Sample Output 1

1

We can form one pair whose sum of the written numbers is 4 by pairing the first and third balls. Note that we cannot pair the second ball with itself.


Sample Input 2

53 11 14 5 13

Sample Output 2

2

【题解】

题意:给定长度为n的序列,输出最多的两两组合之和为2的整次方的对数。

按理说我当时也是从后往前匹配的思路啊,但是为什么wa了...谜(留个悬念)

做法就是存入序列multiset自动排序,然后从大到小寻找是否存在可匹配满足条件的项,如果存在,答案+1,并删去匹配项。

认识了新容器 --- multiset,加深了迭代器的印象(太久没用都忘了)。

【代码】

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define go(i,a,b) for(int i=a;i<=b;i++)#define og(i,a,b) for(int i=a;i>=b;i--)#define mem(a,b) memset(a,b,sizeof(a))#define Pi acos(-1)#define eps 1e-8using namespace std;typedef long long int ll;const int maxn=2*1e5+5;const ll inf=0x3f3f3f3f;const int mod=1e9+7;multiset
s;int main(){ int n; scanf("%d",&n); for(int i=0;i
::iterator it=s.end(); it--; int x=*it; s.erase(it); for(n=1;n<=x;n*=2); if(s.find(n-x)!=s.end()) ans++,s.erase( s.find(n-x) ); } printf("%d\n",ans); return 0;}

 

转载地址:http://hyben.baihongyu.com/

你可能感兴趣的文章
PAT---A1027. Colors in Mars (20)
查看>>
PAT---1058. A+B in Hogwarts (20)
查看>>
PAT---A1001. A+B Format (20)
查看>>
PAT---A1005. Spell It Right (20)
查看>>
PAT---A1035. Password (20)
查看>>
PAT---A1077. Kuchiguse (20)
查看>>
PAT---A1062. Talent and Virtue (25)
查看>>
PAT---A1012. The Best Rank (25)
查看>>
数据库SQL语言语法总结3---查询语句
查看>>
数据库SQL语言语法总结4---数据更新
查看>>
数据库SQL语言语法总结5---视图
查看>>
数据库SQL语言语法总结6---数据控制
查看>>
数据库SQL语言语法总结1---表操作
查看>>
Numpy中stack(),hstack(),vstack()函数详解
查看>>
基于3D卷积神经网络的行为识别
查看>>
K.function用法
查看>>
keras -- multi-loss
查看>>
pytorch数据增强的具体细节
查看>>
pytorch专题 --- load模型
查看>>
VSCode编写C++代码从零开始
查看>>