Google Code Jam Qualification Round Africa 2010 Problem A. Store Credit

https://code.google.com/codejam/contest/351101/dashboard#s=p0

Problem

You receive a credit C at a local store and would like to buy two items. You first walk through the store and create a list L of all available items. From this list you would like to buy two items that add up to the entire value of the credit. The solution you provide will consist of the two integers indicating the positions of the items in your list (smaller number first).

Input

The first line of input gives the number of cases, NN test cases follow. For each test case there will be:

  • One line containing the value C, the amount of credit you have at the store.
  • One line containing the value I, the number of items in the store.
  • One line containing a space separated list of I integers. Each integer P indicates the price of an item in the store.
  • Each test case will have exactly one solution.

Output

For each test case, output one line containing "Case #x: " followed by the indices of the two items whose price adds up to the store credit. The lower index should be output first.

Limits

5 ≤ C ≤ 1000
1 ≤ P ≤ 1000

Small dataset

N = 10
3 ≤ I ≤ 100

Large dataset

N = 50
3 ≤ I ≤ 2000

Sample

Input  Output 
3
100
3
5 75 25
200
7
150 24 79 50 88 345 3
8
8
2 1 9 4 4 56 90 3
Case #1: 2 3
Case #2: 1 4
Case #3: 4 5
 

Solution:

vector<int> solve1(double C, double item_n, vector<int>price)
{ map<int, int> price_existance;
for (int i = ; i < item_n; i++) {
price_existance[price[i]] = i;
} for (int i = ; i < item_n; i++) {
if (price_existance.count(C - price[i])) {
int index0 = price_existance.at(C - price[i]); if (index0 == i)
continue; vector<int> result;
if (i > index0) {
result.push_back(index0 + );
result.push_back(i + );
return result;
} else {
result.push_back(i + );
result.push_back(index0 + );
return result;
}
}
}
return vector<int>();
} int main() {
freopen("in", "r", stdin);
freopen("out", "w", stdout); int t_case_num;
scanf("%d", &t_case_num);
if (!t_case_num) {
cerr << "Check input!" << endl;
exit();
} // Read input set
int C, item_n;
for (int case_n = ; case_n <= t_case_num; case_n++) {
scanf("%d", &C);
scanf("%d", &item_n);
vector<int>price; for (int i = ; i < item_n; i++) {
int p = ;
cin >> p;
price.push_back(p);
} auto result = solve1(C, item_n, price);
printf("Case #%d: %d %d\n", case_n, result[], result[]);
} fclose(stdin);
fclose(stdout);
return ;
}

最新文章

  1. 用十条命令在一分钟内检查Linux服务器性能
  2. sax/dom/jdom/dom4j的区别
  3. Android studio 多渠道打包
  4. hibernate Criteria查询多对多(Set集合)条件筛选
  5. 复习后台代码(与前面clentHttp连接网络结合)
  6. 寻找“水王”--c++
  7. mysql修改数据库表权限
  8. mMathf -》 Unity3d通用脚本
  9. Node.js v0.10.31API手工-DNS
  10. 实现AOP功能的封装与配置的小框架
  11. Android基础知识02—安卓日志工具LogCat的五种方法
  12. Bootstrap学习笔记(一)
  13. ASP.Net Core的Code Fist代码先行操作方法
  14. Codeforces Round #352 (Div. 2) (A-D)
  15. 洛谷P5108 仰望半月的夜空(后缀数组)
  16. 虚拟机系统安装Messenger和Server
  17. win10系统开机输入密码黑屏解决方法
  18. tensorflow can not find libcusolver.so.8.0
  19. CentOS7下Docker的安装与使用
  20. js跨域调用mvc ActionResult扩展

热门文章

  1. 【Python学习】字符编码
  2. Android快速入门(转自 农民伯伯: http://www.cnblogs.com/over140/)
  3. vsftpd.log内容的意义
  4. [笔记]Linux NTP命令 (ESX适用)
  5. leetcode 之Longest Consecutive Sequence(六)
  6. CSS&amp;&amp;xpath
  7. 使用OpenSSL自签发服务器https证书
  8. beego与websocker的集成
  9. vim 图册
  10. JavaScript学习之事件原理和实践