Google Code jam 2012 practice-store credit

Source: Internet
Author: User

Problem

You receive a credit C at a local store and wowould like to buy two items. you first walk through the store and create a list l of all available items. from this list you wowould 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, N. N 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 shocould 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

/** Author: WXG */# include <iostream> # include <vector> # include <stdio. h> using namespace STD; void solution () {int N; vector <int> C, L, First, Second, Max; vector <int> P; int I, j; CIN> N; for (I = 0; I <n; I ++) {int CTMP, ltmp; vector <int> pTMP; cin> CTMP> ltmp; C. push_back (CTMP); L. push_back (ltmp); For (j = 0; j <ltmp; j ++) {int vtmp; CIN> vtmp; pTMP. push_back (vtmp);} p. push_back (pTMP) ;}for (I = 0; I <n; I ++) {int CTMP = C. at (I); int Np = L. at (I); int max = 0, ftmp, stmp; For (j = 0; j <NP; j ++) {for (int K = J + 1; k <NP; k ++) {int mtmp = P. at (I ). at (j) + P. at (I ). at (k); If (max <mtmp & mtmp <= CTMP) {max = mtmp; ftmp = J; stmp = K ;}} first. push_back (ftmp + 1); Second. push_back (stmp + 1) ;}for (I = 0; I <n; I ++) {cout <"case #" <I + 1 <": "<first. at (I) <"" <second. at (I) <Endl ;}int main () {freopen ("A-large-practice.in", "r", stdin ); // redirect standard input and output streams freopen ("output.txt", "W", stdout); solution (); fclose (stdin); fclose (stdout); Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.