首页 > 代码库 > Google Code Jam 2012 Practice - Store Credit

Google Code Jam 2012 Practice - Store Credit

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, 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 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

在此输入图片描述

/*
*   author:wxg
*/ 
#include<iostream>
#include<vector>
#include<stdio.h>
using namespace std;

void solution(){
    int n;       
    vector<int> c,l,first,second,max;  
    vector<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); //重定向标准输入和输出流 
    freopen("output.txt","w",stdout);    
    solution(); 
    fclose(stdin);
    fclose(stdout);
    return 0;
}