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;
    }
  • 相关阅读:
    scrapy 多个爬虫运行
    scrapy不抓取重复的网页解决办法
    centos7 安装mysql
    20个免费和开源数据可视化工具
    转载 CSDN 谈谈我对证券公司一些部门的理解(前、中、后台)
    scrapy 教程
    django 常见错误汇总
    python 常见错误
    python 基础知识整理
    使用SQL语法来查询Elasticsearch:Elasticsearch-SQL插件
  • 原文地址:https://www.cnblogs.com/fatlyz/p/3678836.html
Copyright © 2011-2022 走看看