标签:fir min ++ ios http hose 大于 cout sequence
银行有 N 个窗口,窗口前有一条黄线。并且有以下规则
两种情况:
#include <iostream>
#include <string.h>
#include <queue>
#include <map>
using namespace std;
int main() {
int N, M, K, Q;
int Time[1000]; // 存储每个顾客进店之后到办理结束后的时间,无法办理的值为 -1
// int yellow[10][20]; // 存储黄线内第一行的窗口处理时间
map<int, queue<int>> yellow;
int wait_time; // 存储每次输入的顾客办理业务时间
int sequence[1000], seq_index = 0; // 存储黄线之外顾客进入窗口的顺序
int number = 0; // 存储进入顾客的编号
int min_time = 999999, min_index = 0; // 存储最短办理时间 进入队列的顺序
int windows_time[20]; // 记录每个窗口的时间,用于计算每个顾客办理业务的时间
int min_length = 99999, min_len_index = 0; // 记录窗口前队伍的最短长度
cin >> N >> M >> K >> Q;
if (M == 0)
M = 1;
memset(windows_time, 0, sizeof(windows_time));
memset(Time, 0, sizeof(Time));
int circle = N*M < K ? N*M : K; // 处理办理业务的人数小于黄线内等待人数的情况
// 存储黄线内顾客的等待时间
for (int i = 0; i < circle; ++i) {
cin >> wait_time;
if (yellow.count(i % N) == 0) {
queue<int> temp;
temp.push(wait_time);
yellow.insert(pair<int, queue<int>>(i % N, temp));
} else {
map<int, queue<int>>::iterator iter = yellow.find(i % N);
iter->second.push(wait_time);
}
if (windows_time[i % N] >= 540)
Time[number++] = -1;
else {
windows_time[i % N] += wait_time;
Time[number++] = windows_time[i % N];
}
}
// 计算所有顾客的等待时间
for (int i = 0; i < K - N * M; ++i) {
cin >> wait_time;
for (map<int, queue<int>>::iterator iter = yellow.begin(); iter != yellow.end(); iter++) {
if (min_time > iter->second.front()) {
min_time = iter->second.front();
min_index = iter->first;
}
}
for (map<int, queue<int>>::iterator iter = yellow.begin(); iter != yellow.end(); iter++) {
if (min_index == iter->first) {
iter->second.pop();
} else {
iter->second.front() -= min_time;
}
if(min_length > iter->second.size()){
min_length = iter->second.size();
min_len_index = iter->first;
}
}
yellow.find(min_len_index)->second.push(wait_time);
if (windows_time[min_index] >= 540)
Time[number++] = -1;
else {
windows_time[min_index] += wait_time;
Time[number++] = windows_time[min_index];
}
min_length = min_time = 999999;
}
// 根据输入的序列输出时间
for (int i = 0; i < Q; ++i) {
int hour = 8, minutes = 0; // 定义时间,方便输出
cin >> wait_time; // 这里不想定义额外的变量,就使用这个了
minutes = Time[wait_time - 1];
hour += (minutes / 60);
minutes %= 60;
if (Time[wait_time - 1] == -1) {
cout << "Sorry";
} else {
if (hour < 10)
cout << "0" << hour << ":";
else
cout << hour << ":";
if (minutes < 10)
cout << "0" << minutes;
else
cout << minutes;
}
if (i < Q - 1)
cout << endl;
}
}
PAT 甲级测试题目 -- 1014 Waiting in Line
标签:fir min ++ ios http hose 大于 cout sequence
原文地址:https://www.cnblogs.com/Breathmint/p/10339731.html