传送门

Ice Cream Parlor

Authored by dheeraj on Mar 21 2013

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]表示icost数组中首次出现的位置。

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的更多相关文章

  1. 【HackerRank】Ice Cream Parlor

    Sunny and Johnny together have M dollars which they intend to use at the ice cream parlour. Among N ...

  2. 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 ...

  3. 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 ...

  4. Ice Cream Tower

    2017-08-18 21:53:38 writer:pprp 题意如下: Problem D. Ice Cream Tower Input file: Standard Input Output f ...

  5. 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心

    /** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...

  6. E. Sonya and Ice Cream(开拓思维)

    E. Sonya and Ice Cream time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  7. 【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 ...

  8. 【dfs+理解题意+构造】【待重做】codeforces E. Ice cream coloring

    http://codeforces.com/contest/805/problem/E [题意] 染色数是很好确定,最少染色数是max(si)(最小为1,即使所有的si都为0,这样是单节点树形成的森林 ...

  9. codeforces 686A A. Free Ice Cream(水题)

    题目链接: A. Free Ice Cream //#include <bits/stdc++.h> #include <vector> #include <iostre ...

随机推荐

  1. docker中清理冗余的image,container

    1) 首先进入超级用户模式 [root@docker ~]# sudo su2) 删除container ( container运行时是不能删除的 )首先停止container [root@docke ...

  2. [tools]tcp/udp连通性测试

    一 端口连通性测试意义 测试网络端口可达性,确保给某些使用特定端口的app做链路连通性检测.使它们能够正常的运行起来.   二 法1 使用newclient发包,彼端tcpdump抓包观察是否能收到包 ...

  3. C#中事件的继承

    C#中的子类无法调用父类的事件,可以通过在父类中创建一个方法来调用父类的事件,而子类通过调用父类的方法来触发事件. class parent { protected string name; publ ...

  4. Jdeveloper 太慢 slowly

    https://blogs.oracle.com/shay/entry/is_your_jdeveloper_slow_it_sho http://bexhuff.com/2012/09/jdevel ...

  5. &10 基本数据结构——栈,队列和链表

    #1,栈(stack) 定义[来自百度]:栈(stack)又名堆栈,它是一种运算受限的线性表.其限制是仅允许在表的一端进行插入和删除运算.这一端被称为栈顶,相对地,把另一端称为栈底.向一个栈插入新元素 ...

  6. HTTP协议简介1

    概念 HTTP协议:超文本传输协议,用于服务端传输超文本到客户端的传输协议.是一个应用层协议. 工作流程 一次http请求就是一个事务.过程可分为四步: 1.客户端与服务器建立链接.页面上单击某个链接 ...

  7. 自动化测试: sikuli,一个基于界面图像的gui测试框架

    http://www.sikuli.org/ license: MIT script language: Python 下面是他的一个hello world的例子,看看也挺有意思的. 开源的世界里有很 ...

  8. RocEDU.阅读.写作《你的灯亮着吗?》

    <你的灯亮着吗?> 一.对本书的认识 这本书的作者就如何训练思维能力指点迷津.书中提及的观点包括"问题是理想状态和现实状态之间的差别",以及"无论表面上表现的 ...

  9. Openwrt Uboot烧写

    Openwrt 烧uboot 需要慎重,一般买一个带不死uboot的路由器再折腾会比较安全,因为 openwrt firmware对uboot分区进行了保护,而且带有不死uboot的路由器可以通过we ...

  10. 如何区分 OpenStack Neutron Extension 和 Plugin

    Neutron 里面的 extension 和 plugin 是非常相似的两个概念,我花了好久才貌似搞懂了两者的区别,还不一定完全正确. 在OpenStack 的官网wiki中,可以找到它们两个的定义 ...