13级春季校赛的热身题,但优化后我的代码也超时了,后来看了看学长的解法,觉得最简单的还是map,再一次感受到了map的强大。

题目描述如下

Description

There is an integer set A. How many couples of a and b, which can make a+b=0(a∈A, b∈A,a<=b).

Input

There are multiple test cases. The first line is an integer T indicating for the number of test cases. The first line of each case is an integer n, standing for the number of integers in set A.

The second line is n integers in set A separated by space.

(1<=n<=100 000,|ai|<=1000 000 000)

Output

For each test case, output all the unique couples of a and b by the order of a from small to large.

If there is no such a situation, output "<empty>".

Output a blank line after each case.

Sample Input

2

6

-1 0 1 4 -1 -4

3

1 1 2

Sample Output

-4 4

-1 1

<empty>

  map的遍历方式:使用迭代器,it->first指原集合的元素,it-second指映射集合的元素。这样仅用O(N)的复杂度就求出了答案,尽管map是c++封装好的函数,运行相对较慢。

对比数组来说,map的好处有很好的处理负值,能够容下更大的数字等等,代码如下:

#include<cstdio>
#include<map>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
int n,t,b,mark;
scanf("%d",&t);
while(t--)
{
map<int,int>a;
a.clear();
mark = ;
scanf("%d",&n);
while(n--)
{
scanf("%d",&b);
a[b]++;
}
map<int,int>::iterator it;
for(it = a.begin(); it->first < && it != a.end(); it++)
{
///cout<<"first = "<<it->first<<endl;
if(it->second)
{
///cout<<"second = "<<it->second<<endl;
if(a[-it->first])
{
printf("%d %d\n",it->first,-it->first);
mark = ;
}
}
}
if(a[] >= )
{
cout<<"0 0"<<endl;
mark = ;
}
if(mark == )
printf("<empty>\n");
printf("\n");
}
}

hrbust 1721 A + B = 0 map的应用的更多相关文章

  1. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(六)针对spark2.2.1以yarn方式启动spark-shell抛出异常:ERROR cluster.YarnSchedulerBackend$YarnSchedulerEndpoint: Sending RequestExecutors(0,0,Map(),Set()) to AM was unsuccessful

    Spark以yarn方式运行时抛出异常: [spark@master bin]$ cd /opt/spark--bin-hadoop2./bin [spark@master bin]$ ./spark ...

  2. Google map v3 using simple tool file google.map.util.js v 1.0

    /** * GOOGLE地图开发使用工具 * @author BOONYACHENGDU@GMAIL.COM * @date 2013-08-23 * @notice 地图容器的(div)z-inde ...

  3. Cocos2d-x3.0模版容器具体解释之二:cocos2d::Map&lt;K,V&gt;

    1.概述: 版本号: v3.0 beta 语言: C++ 定义在 "COCOS2DX_ROOT/cocos/base" 路径下的 "CCMap.h" 的头文件里 ...

  4. 现在有一个城市销售经理,需要从公司出发,去拜访市内的商家,已知他的位置以及商家的位置,但是由于城市道路交通的原因,他只能在左右中选择一个方向,在上下中选择一个方向,现在问他有多少种方案到达商家地址。给定一个地图map及它的长宽n和m,其中1代表经理位置,2代表商家位置,-1代表不能经过的地区,0代表可以经过的地区,请返回方案数,保证一定存在合法路径。保证矩阵的长宽都小于等于10。

    include "stdafx.h" #include<iostream> #include<vector> #include<algorithm&g ...

  5. Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds...

    仰天长啸   Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds... 当启动tomcat时候出现 ...

  6. Dom4j把xml转换成Map(非固定格式)

    将xml转换成Map,能够应对不用结构的xml,而不是只针对固定格式的xml.转换规则:1.主要是Map与List的互相嵌套2.同名称的节点会被装进List 示例: import java.util. ...

  7. java.util.ConcurrentModificationException --map

    key:3-key key:/v1.02-key Exception in thread "main" java.util.ConcurrentModificationExcept ...

  8. std::map用法

    STL是标准C++系统的一组模板类,使用STL模板类最大的好处就是在各种C++编译器上都通用.    在STL模板类中,用于线性数据存储管理的类主要有vector, list, map 等等.本文主要 ...

  9. map集合键值对存储,键值不重复,值可以重复

    import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; import java.util.Li ...

随机推荐

  1. hdu_4547_CD操作(在线LCA)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4547 题意:中文,不解释 题解:很裸的LCA,注意父目录打开子目录一次就够了,这里我才用倍增在线LCA ...

  2. Soj题目分类

    -----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...

  3. 第13章 Swing程序设计----JFrame窗体

    JFrame窗体是一个容器,它是Swing程序中各个组件的载体,可以将JFrame看作是承载这些Swing组件的容器. 在开发应用程序时可以通过继承java.swing.JFrame类创建一个窗体,在 ...

  4. weka对数据进行预测

    1.注意待预测数据集和训练用数据集各个属性的设置必须是一致的.即使你没有待预测数据集的Class属性的值,你也要添加这个属性,可以将该属性在各实例上的值均设成缺失值.比如你可以将欲预测的类别设为?即缺 ...

  5. 经典.net面试题目(3)

    1. asp.net中web应用程序获取数据的流程: A.Web Page B.Fill  C.Sql05  D.Data Sourse  E.DataGrid  F.DataSet  G.Selec ...

  6. IE10以下的placeholder不兼容问题

    $(function(){ if(!placeholderSupport()){   // 判断浏览器是否支持 placeholder     $('[placeholder]').focus(fun ...

  7. sqldeveloper连接mysql

    sqldeveloper连接mysql 腑镜诏 钌d 汁叁ㄧ勋 佚蔡弥噙 不仅仅是越南帮和戴爷的事情还有谢婉莹的条 炔验遒其 倒扇油┣ 砹笄谤 几句话孟飞的信心大增兴奋不已换句话说 谠诂k厝 ...

  8. MyEclipse10.6、Maven、svn集成

    这几天整理maven,根据开发部署需要,需要把原先的myeclipse下的普通javaWeb工程用maven管理,弄了2天,主要还是在jar包的引入上,总是少- -! 好了,下面记录一下我的安装过程: ...

  9. redhat 安装GCC-4.8.3

    1.下载gcc-4.8.3安装包 gcc各版本浏览地址:http://ftp.gnu.org/gnu/gcc/ yum install gccyum install gcc-c++ 2.将gcc-4. ...

  10. iOS NSString类中获取子字符串

    NSString类中提供了这样三个方法用于获取子字符串: – substringFromIndex://取字符串长度从0开始,当index=str.length时字符串为空"" – ...