核心部分为三段for循环。
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
struct Bank{
int time, num;
queue<int> q;
}bank[30];
int process[1010];
bool cmp(Bank a, Bank b)
{
int x1 = a.time + process[a.q.front()], x2 = b.time + process[b.q.front()];
if(x1 != x2) return x1 < x2;
else return a.num < b.num;
}
int main()
{
int n, m, k, q, x = 1, result[1010] = {};
scanf("%d%d%d%d", &n, &m, &k, &q);
for(int i = 0; i < n; i++){
bank[i].time = 8 * 60;
bank[i].num = i;
}
for(int i = 1; i <= k; i++){
scanf("%d", &process[i]);
}
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(x <= k) bank[j].q.push(x++);
}
}
for(int i = n * m + 1; i <= k; i++){
sort(bank, bank + n, cmp);
if(bank[0].time >= 17 * 60) break;
result[bank[0].q.front()] = bank[0].time + process[bank[0].q.front()];
bank[0].time = result[bank[0].q.front()];
bank[0].q.pop();
bank[0].q.push(i);
}
for(int i = 0; i < n; i++){
while(bank[i].q.size() != 0){
if(bank[i].time >= 17 * 60) break;
result[bank[i].q.front()] = bank[i].time + process[bank[i].q.front()];
bank[i].time = result[bank[i].q.front()];
bank[i].q.pop();
}
}
for(int i = 0; i < q; i++){
scanf("%d", &x);
if(result[x] == 0) printf("Sorry\n");
else printf("%02d:%02d\n", result[x] / 60, result[x] % 60);
}
return 0;
}