[Kick Start 2020] Round F 1. ATM Queue

好题目,为自己的蜜汁自信感到羞愧,这个题目陷入了自己写cmp错误发现不了的大坑

[Kick Start 2020] Round F 1. ATM Queue

1. 题目

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

Output

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

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:
The queue initially looks like [1, 2, 3]. The first person withdraws an amount of 2 in their first attempt and leaves the queue.
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.
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.
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.
The queue now looks like [3, 2]. The third person withdraws the remaining amount of 1 and leaves the queue.
The queue now looks like [2]. The second person withdraws the remaining amount of 1 and leaves the queue.
The queue is now empty.
The order in which people leave the queue is [1, 3, 2].

2. 思路

  • 链表写了个传统暴力思路,直接超时
  • 优化了下问题的本质,发现就是对排队次数进行排序
  • 时间复杂度nlogn(排序的复杂度)
  • 为什么我在比赛的时候没有A掉所有的测试例呢:因为自己写了pair的cmp【有错误,谜一般的存在】

3. 代码

#include<iostream>
#include<vector>
#include <algorithm>
#include <list>
using namespace std;

vector<int> methodOne(int peopleNum, int maxAmount, list<pair<int, int>> allAmount) {
	vector<int> res;
	while (!allAmount.empty()) {
		pair<int, int> temp = allAmount.front();
		allAmount.pop_front();
		if (temp.second <= maxAmount) {
			res.push_back(temp.first);
		}
		else {
			allAmount.push_back(make_pair(temp.first, temp.second - maxAmount));
		}
	}
	return res;
}


int main(){
	int testNum;
	cin >> testNum;
	for (int i = 0; i < testNum; i++) {
		int peopleNum;
		long long maxAmount;
		cin >> peopleNum >> maxAmount;
		vector<pair<int, int>> allAmount;
		vector<int> res;
		res.clear();
		allAmount.clear();
		for (int j = 0; j < peopleNum; j++) {
			int onePerAmount;
			cin >> onePerAmount;
			allAmount.push_back(make_pair((onePerAmount - 1) / maxAmount, j + 1));
		}
		sort(allAmount.begin(), allAmount.end());

		cout << "Case #" << i + 1 << ": ";
		for (int j = 0; j < allAmount.size(); j++) {
			cout << allAmount[j].second << " ";
		}
		cout << endl;
	}
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值