HackerRank Ice Cream Parlor
Ice Cream Parlor

Problem Statement
Sunny and Johnny together have M dollars they want to spend on ice cream. The parlor offers N flavors, and they want to choose two flavors so that they end up spending the whole amount.
You are given the cost of these flavors. The cost of the ith flavor is denoted by ci. You have to display the indices of the two flavors whose sum is M.
Input Format
The first line of the input contains T; T test cases follow.
Each test case follows the format detailed below: The first line contains M. The second line contains N. The third line contains N space-separated integers denoting the price of each flavor. Here, the ith integer denotes ci.
Output Format
Output two integers, each of which is a valid index of a 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,where i∈[1,N]
The prices of any two items may be the same and each test case has a unique solution.
Sample Input
2
4
5
1 4 5 3 2
4
4
2 2 4 3
Sample Output
1 4
1 2
Explanation
The sample input has two test cases.
For the 1st, the amount M = 4 and there are 5 flavors at the store. The flavors indexed at 1 and 4 sum up to 4.
For the 2nd test case, the amount M = 4 and the flavors indexed at 1 and 2 sum up to 4.
Solution
简单题。
利用题目给出的cost数组构造index数组,index[i]表示i在cost数组中首次出现的位置。
Implementation
#include<bits/stdc++.h>
using namespace std;
const int N(1e4+);
int idx[N];
int main(){
freopen("in", "r", stdin);
int T;
cin>>T;
for(int n, m, id1, id2; T--; memset(idx, , sizeof(idx))){
cin>>m>>n;
for(int i=, c; i<=n; i++){
cin>>c;
if(c>=m) continue; //error-prone
if(!idx[c]) idx[c]=i;
if(idx[m-c]&&idx[m-c]!=i){
id1=idx[m-c], id2=i; //error-prone
}
}
cout<<id1<<' '<<id2<<endl;
}
}
凡注释error-prone的地方都是开始写错了的。
1. 我的写法是边读入边处理的,有时会没读完就得出答案,这时很容易就来个break;
2. 针对这道题的
2.1 要预判c>=m的情况
2.2 如何处理cost相同的flavor
HackerRank Ice Cream Parlor的更多相关文章
- 【HackerRank】Ice Cream Parlor
Sunny and Johnny together have M dollars which they intend to use at the ice cream parlour. Among N ...
- How to Implement Bluetooth Low Energy (BLE) in Ice Cream Sandwich
ShareThis - By Vikas Verma Bluetooth low energy (BLE) is a feature of Bluetooth 4.0 wireless radio t ...
- Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题
A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adve ...
- Ice Cream Tower
2017-08-18 21:53:38 writer:pprp 题意如下: Problem D. Ice Cream Tower Input file: Standard Input Output f ...
- 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心
/** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...
- E. Sonya and Ice Cream(开拓思维)
E. Sonya and Ice Cream time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 【Codeforces Round #411 (Div. 1)】Codeforces 804C Ice cream coloring (DFS)
传送门 分析 这道题做了好长时间,题意就很难理解. 我们注意到这句话Vertices which have the i-th (1 ≤ i ≤ m) type of ice cream form a ...
- 【dfs+理解题意+构造】【待重做】codeforces E. Ice cream coloring
http://codeforces.com/contest/805/problem/E [题意] 染色数是很好确定,最少染色数是max(si)(最小为1,即使所有的si都为0,这样是单节点树形成的森林 ...
- codeforces 686A A. Free Ice Cream(水题)
题目链接: A. Free Ice Cream //#include <bits/stdc++.h> #include <vector> #include <iostre ...
随机推荐
- (copy)C#时间日期操作
copy to:http://www.cnblogs.com/loveme123/archive/2012/06/27/2565547.html 一.C# 日期格式 DateTime dt = Da ...
- 纯CSS3制作皮卡丘动画壁纸
前言 明天就放假了,趁着今晚的空挡时间来写这篇博客——这是我昨晚实现的一个简单的CSS3动画效果.话说还得缘起我逛了一下站酷网,然后不小心看到了一张皮卡丘的手机壁纸,觉得很可爱,然后觉得这种效果是可以 ...
- onresize方法
resize()方法可以写在当前页面包含的所有js里
- 图解HTTP看书体会(1)
MAC地址和IP地址的区别与联系 一.IP地址 对于IP地址,相信大家都很熟悉,即指使用TCP/IP协议指定给主机的32位地址.IP地址由用点分隔开的4个8八位组构成,如192.168.0.1就是一个 ...
- FormsAuthentication详解
配置安全鉴别 鉴别是指鉴定来访用户是否合法的过程.ASP.NET Framework支持三种鉴别类型: Windows鉴别: NET Passport鉴别: Forms鉴别. 对于某一特定的应用程序, ...
- php基础22:上传并且保存文件
<?php /* 文件上传的限制 && 保存被上传的文件 在这个脚本中,我们增加了对文件上传的限制.用户只能上传 .gif 或 .jpeg 文件,文件大小必须小于 20 kb: ...
- 面试准备(四)Java基本数据类型
Java语言是静态类型的(statical typed),也就是说所有变量和表达式的类型再编译时就已经完全确定.由于是statical typed,导致Java语言也是强类型(Strong typed ...
- 自动化测试: sikuli,一个基于界面图像的gui测试框架
http://www.sikuli.org/ license: MIT script language: Python 下面是他的一个hello world的例子,看看也挺有意思的. 开源的世界里有很 ...
- [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵
11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...
- Opencv step by step - 基本数据类型
CvArr,CvMat,IplImage这三者是继承的关系. 打开opencv 3.0的源码: cvArr /* CvArr* is used to pass arbitrary * array-l ...