zoukankan      html  css  js  c++  java
  • Google Code Jam Africa 2010 Qualification Round Problem A. Store Credit

    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 = 0; i < item_n; i++) {
            price_existance[price[i]] = i;
        }
        
        for (int i = 0; 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 + 1);
                    result.push_back(i + 1);
                    return result;
                } else {
                    result.push_back(i + 1);
                    result.push_back(index0 + 1);
                    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(0);
        }
        
        // Read input set
        int C, item_n;
        for (int case_n = 1; case_n <= t_case_num; case_n++) {
            scanf("%d", &C);
            scanf("%d", &item_n);
            vector<int>price;
            
            for (int i = 0; i < item_n; i++) {
                int p = 0;
                cin >> p;
                price.push_back(p);
            }
            
            auto result = solve1(C, item_n, price);
            printf("Case #%d: %d %d
    ", case_n, result[0], result[1]);
        }
        
        fclose(stdin);
        fclose(stdout);
        return 0;
    }
  • 相关阅读:
    python搭建开发环境
    django初探
    linux下安装Composer
    文件记录追加 file_put_content
    自定义导出表格
    异步处理接口 fsockopen
    appcache checking update
    js pix
    Event Aggregator
    ko list and css gradient
  • 原文地址:https://www.cnblogs.com/fatlyz/p/3678836.html
Copyright © 2011-2022 走看看