Sunny and Johnny together have M dollars which they intend to use at the ice cream parlour. Among N flavors available, they have to choose two distinct flavors whose cost equals M. Given a list of cost of N flavors, output the indices of two items whose sum equals M. The cost of a flavor (ci) will be no more than 10000.

Input Format

The first line of the input contains T, T test cases follow. 
Each test case follows the format: The first line contains M. The second line contains the number N. The third line contains N single space separated integers denoting the price of each flavor ci.

Output Format

Output two integers, each of which is a valid index of the flavor. The lower index must be printed first. Indices are indexed from 1 to N.

Constraints

1 ≤ T ≤ 50 
2 ≤ M ≤ 10000 
2 ≤ N ≤ 10000 
1 ≤ ci ≤ 10000 
The prices of two items may be same and each test case has a unique solution.


又是一个求数组中两个数和正好是m的问题,类似leetcode中的Two Sum问题。

只是在这个问题中,数组中的数是有可能重复的,所以map中的value要用一个arrayList保存。

另外注意找的时候要保持i比在map中找到的坐标小(如代码26行的判断所示),以免重复。

代码如下:

 import java.util.*;

 public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t-- >0){
int m = in.nextInt();
int n = in.nextInt();
int[] prices = new int[n];
HashMap<Integer, ArrayList<Integer>> map = new HashMap<Integer, ArrayList<Integer>>(); for(int i = 0;i < n;i++){
prices[i]=in.nextInt();
if(!map.containsKey(prices[i]))
map.put(prices[i], new ArrayList<Integer>());
map.get(prices[i]).add(i);
} for(int i = 0;i < n;i++){
int has = prices[i];
int toFind = m-prices[i]; if(map.containsKey(toFind)){
for(int temp:map.get(toFind)){
if(i < temp){
System.out.printf("%d %d",i+1,temp+1);
System.out.println();
}
}
}
}
}
}
}

【HackerRank】Ice Cream Parlor的更多相关文章

  1. HackerRank Ice Cream Parlor

    传送门 Ice Cream Parlor Authored by dheeraj on Mar 21 2013 Problem Statement Sunny and Johnny together ...

  2. 【HackerRank】How Many Substrings?

    https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...

  3. 【HackerRank】Running Time of Quicksort

    题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...

  4. 【hackerrank】Week of Code 30

    Candy Replenishing Robot Find the Minimum Number 直接模拟 Melodious password dfs输出方案 Poles 题意:有多个仓库,只能从后 ...

  5. 【hackerrank】Week of Code 26

    在jxzz上发现的一个做题网站,每周都有训练题,题目质量……前三题比较水,后面好神啊,而且类型差不多,这周似乎是计数专题…… Army Game 然后给出n*m,问需要多少个小红点能全部占领 解法:乘 ...

  6. 【HackerRank】Median

    题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大 ...

  7. 【HackerRank】Coin on the Table

    题目链接:Coin on the Table 一开始想用DFS做的,做了好久都超时. 看了题解才明白要用动态规划. 设置一个三维数组dp,其中dp[i][j][k]表示在时间k到达(i,j)所需要做的 ...

  8. 【HackerRank】Pairs

    题目链接:Pairs 完全就是Two Sum问题的变形!Two Sum问题是要求数组中和正好等于K的两个数,这个是求数组中两个数的差正好等于K的两个数.总结其实就是“骑驴找马”的问题:即当前遍历ar[ ...

  9. 【HackerRank】Cut the tree

    题目链接:Cut the tree 题解:题目要求求一条边,去掉这条边后得到的两棵树的节点和差的绝对值最小. 暴力求解会超时. 如果我们可以求出以每个节点为根的子树的节点之和,那么当我们去掉一条边(a ...

随机推荐

  1. 模拟 Ajax 上传

    ----------------view----------------------------------- <form method="post" action=&quo ...

  2. php 批量插入字段

    foreach ($_POST as $key => $value){ $array[] = "add ".$key." varchar(220),"; ...

  3. 集合Set映射一对多(使用xml文件)

    如果持久化类设置了包含实体引用的对象,则需要使用一对多关联来映射集合(Set)元素. 我们可以通过任意一个Set集合来映射这个列表对象. 下面来看看看设置对象的持久化类. 在这种情况下,一个问题可以有 ...

  4. C/C++程序开发中实现信息隐藏的三种类型

    不管是模块化设计,还是面向对象设计.还是分层设计,实现子系统内部信息的对外隐藏都是最关键的内在要求.以本人浅显的经验,把信息隐藏依照程度的不同分成(1)不可见不可用(2)可见不可用(3)可见可用. 1 ...

  5. asp.net 页面延时五秒,跳转到另外的页面

    asp.net 页面延时五秒,跳转到另外的页面的实现代码. --前台 <%@ Page Language="C#" AutoEventWireup="true&qu ...

  6. Error:“const char*”类型的实参与“wchar_t”类型的形参不兼容

    MainApp\RPolarView.cpp(1571): error C2664: “ATL::CStringT<BaseType,StringTraits>::ReverseFind” ...

  7. Tomcat虚拟目录

    x先来看一段server.xml文件里的配置: <Host appBase="" autoDeploy="true" debug="0" ...

  8. PHP连接MySQL数据库操作

    (原文链接:http://www.cnblogs.com/csensix/archive/2012/05/23/2515494.html) 方法一:普通方法(面向过程) 首先,先做出如下假设(也适用与 ...

  9. 《从零开始学Swift》学习笔记http(Day1)——我的第一行Swift代码

    Swift 2.0学习笔记(Day1)——我的第一行Swift代码 原创文章,欢迎转载.转载请注明:关东升的博客 当第一次看到下面代码时我石化了,这些代码是什么东东?单词拼出来的? import Fo ...

  10. Parenthesis(前缀和+线段树)

    1809: Parenthesis Time Limit: 5 Sec     Memory Limit: 128 Mb     Submitted: 2291     Solved: 622 Des ...