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;
    }
  • 相关阅读:
    20年的Flash要退出舞台:当年哪个小游戏你最爱?
    一些实用但不为人知的Unix命令
    20145221 《Java程序设计》第九周学习总结
    20145221 《Java程序设计》实验报告四:Android开发基础
    20145221 《Java程序设计》实验报告三:敏捷开发与XP实践
    20145221 《Java程序设计》第八周学习总结
    Java实现:数据结构之排序
    20145221 《Java程序设计》第七周学习总结
    20145221 《Java程序设计》实验报告二:Java面向对象程序设计
    网络安全攻防学习平台
  • 原文地址:https://www.cnblogs.com/fatlyz/p/3678836.html
Copyright © 2011-2022 走看看