本文共 1903 字,大约阅读时间需要 6 分钟。
【题目】
Time limit : 2sec / Memory limit : 1024MB
Score : 600 points
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.
Input is given from Standard Input in the following format:
NA1 A2 … AN
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.
31 2 3
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.
53 11 14 5 13
2
【题解】
题意:给定长度为n的序列,输出最多的两两组合之和为2的整次方的对数。
按理说我当时也是从后往前匹配的思路啊,但是为什么wa了...谜(留个悬念)
做法就是存入序列multiset自动排序,然后从大到小寻找是否存在可匹配满足条件的项,如果存在,答案+1,并删去匹配项。
认识了新容器 --- multiset,加深了迭代器的印象(太久没用都忘了)。
【代码】
#include#include #include #include #include #include #include
转载地址:http://hyben.baihongyu.com/