zoukankan      html  css  js  c++  java
  • Kick Start 2020. Round F. ATM Queue

    题目描述

    Problem
    There are N people numbered from 1 to N, standing in a queue to withdraw money from an ATM. The queue is formed in ascending order of their number. The person numbered i wants to withdraw amount Ai. The maximum amount a person can withdraw at a time is X. If they need more money than X, they need to go stand at the end of the queue and wait for their turn in line. A person leaves the queue once they have withdrawn the required amount.
    You need to find the order in which all the people leave the queue.

    Input
    The first line of the input gives the number of test cases T. T test cases follow.
    The first line of each test case gives two space separated integers: the number of people standing in the queue, N and the maximum amount X that can be withdrawn in one turn.
    The next line contains N space separated integers Ai.

    Output
    For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the space separated list of integers that denote the order in which the people leave the queue.

    Limits
    Time limit: 20 seconds per test set.
    Memory limit: 1GB.
    1 ≤ T ≤ 100.

    Test Set 1
    1 ≤ N ≤ 100.
    1 ≤ Ai ≤ 100.
    1 ≤ X ≤ 100.

    Test Set 2
    1 ≤ N ≤ 105 for at most 10 test cases. For the remaining cases, 1 ≤ N ≤ 100
    1 ≤ Ai ≤ 109.
    1 ≤ X ≤ 109.

    Sample
    Input

    2
    3 3
    2 7 4
    5 6
    9 10 4 7 2
    

    Output

    Case #1: 1 3 2
    Case #2: 3 5 1 2 4
    

    In Sample Case #1, there are 3 people and the limit to withdraw in one turn is 3. Below is step-by-step description of how the process will look like:

    1. The queue initially looks like [1, 2, 3]. The first person withdraws an amount of 2 in their first attempt and leaves the queue.
    2. The queue now looks like [2, 3]. The second person wants to withdraw an amount of 7, but they can withdraw only 3 in their first turn. Since they still need to withdraw an amount of 4, they have to rejoin the queue at the end of the line.
    3. The queue now looks like [3, 2]. The third person needs to withdraw an amount of 4 but they can only withdraw 3 in their first turn so, they rejoin the queue at the end of the line to withdraw amount of 1 later.
    4. The queue now looks like [2, 3]. The second person still needs to withdraw an amount of 4. They withdraw an amount of 3 in their second turn and waits for their next turn to arrive to withdraw the remaining amount of 1.
    5. The queue now looks like [3, 2]. The third person withdraws the remaining amount of 1 and leaves the queue.
    6. The queue now looks like [2]. The second person withdraws the remaining amount of 1 and leaves the queue.
    7. The queue is now empty.
      The order in which people leave the queue is [1, 3, 2].

    In Sample Case #2, there are 5 people and the limit to withdraw in one turn is 6. Below is step-by-step description of how the process will look like:

    1. The queue initially looks like [1, 2, 3, 4, 5]. The first person withdraws an amount of 6, and joins at the end again to withdraw the remaining amount of 3 later.
    2. The queue looks like [2, 3, 4, 5, 1]. The second person similarly withdraws an amount of 6 and waits for his next turn to withdraw an amount of 4.
    3. The queue looks like [3, 4, 5, 1, 2]. The third person withdraws an amount of 4 and leaves the queue.
    4. The queue now looks like [4, 5, 1, 2]. The fourth person withdraws 6 and waits for his next turn.
    5. The queue looks like [5, 1, 2, 4]. The fifth person withdraws amount of 2 and leaves the queue.
    6. The queue looks like, [1, 2, 4]. All other people now leave the queue after their second turn one by one.
      The order in which people leave the queue is [3, 5, 1, 2, 4].

    思路解析

    对数组进行排序,若某人需要取出的钱为(M),一次最大可取出的钱为(X),则需要取钱的次数为(lceil frac{M}{X} ceil)
    若两人需要取钱的次数相等,则先来的人先完成取钱。

    代码实现

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    using namespace std;
    
    bool cmp(pair<int, int> person1, pair<int, int> person2) {
        if(person1.first == person2.first)
            return person1.second < person2.second;
        return person1.first < person2.first;
    }
    
    int main(int argc, char** argv) {
        int nCases;
        cin >> nCases;
        for(int cc = 1; cc <= nCases; cc++) {
            int np, mac;
            cin >> np;
            cin >> mac;
            vector<pair<int, int>> ATM;
            for(int i = 0; i < np; i++) {
                int tpmoney;
                cin >> tpmoney;
                int tpround = tpmoney / mac;
    			int tpmod = tpmoney % mac;
    			if (tpmod == 0)
    				tpround--;
                ATM.push_back(pair<int, int>(tpround, i + 1));
            }
            sort(ATM.begin(), ATM.end(), cmp);
            cout << "Case #" << to_string(cc) << ":";
            for(auto person : ATM)
                cout << " " << person.second;
            cout << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    穿越之我是码农 1024 篇
    误删文件机房停电黑客入侵_你最怕什么?
    AI觉醒进行时:程序员你怕了吗?
    未来已来!阿里小蜜AI技术揭秘
    千人千面智能淘宝店铺背后的算法研究登陆人工智能顶级会议AAAI 2017
    CDN缓存不命中排查
    现实需求巨大_技术尚未成熟_学界与业界思维大碰撞
    围观阿里云最会赚钱的人!价值2万元邀请码不限量发送
    今晚19:30直播阿里巴巴大规模持续集成的技术演进之路_欢迎免费观看
    工作压力山大?码农这么减压最有效
  • 原文地址:https://www.cnblogs.com/xqmeng/p/13825836.html
Copyright © 2011-2022 走看看