1014. Waiting in Line (30)

按照原本的思想,代码后面两个用例一直不通过,

本题的思想是:队伍满的时候,找出最先处理完第一个人的队伍用时t,其他队伍第一个人的处理时间需要减掉t

错误出在两个地方,1.有可能有多个队伍的第一个人用时是一样的,此时需要把这些人都从队伍中出列total时间不能忘记更新。

                                    2.在最后判断此人是否能在下班前办理业务时,可能处理时间已经在前面修改了,因此会出错

#define  _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <string.h>
#include <vector>  
using namespace std;
struct Window
{
int ID;
};
struct Customer
{
int total, time;
Customer()
{
total = 0; time = 0;
}
};
vector<int> win[21];
Customer cust[10001] ;
Customer cust1[10001];
int FindMin(int N, int &iMark)
{
int min = 99999;
iMark = 0;
for (int i = 0; i < N; i++)
{
if (win[i].size() < min)
{
min = win[i].size();
iMark = i;
}
}
return min;
}
int findFast(int N)
{
int size=win[0].size();
int min = cust[win[0][0]].time;
int iMark = 0;
for (int i = 0; i < N; i++)
{
if (cust[win[i][0]].time<min)
{
min = cust[win[i][0]].time;
iMark = i;
}
}
return iMark;
}
int main()
{
int N, M, K, Q;
cin >> N >> M >> K >> Q;

int i = 0;
for (i = 0; i < K&&i < N*M; i++)
{
cin >> cust[i].time; cust1[i].time = cust[i].time;
win[i%N].push_back(i);
}
int total = 0;
for (; i < K;i++)
{
cin >> cust[i].time; cust1[i].time = cust[i].time;
int imark=0;
if (FindMin(N, imark) < M)
{
win[imark].push_back(i);
continue;
}
 
imark = findFast(N);
 
int tmp_time = cust[win[imark][0]].time;
total += tmp_time;
cust[win[imark][0]].total = total;

win[imark].erase(win[imark].begin());
win[imark].push_back(i);
for (int j = 0; j < N; j++)
{
if (j != imark)
{
cust[win[j][0]].time -= tmp_time;

if (cust[win[j][0]].time < 1)
{
cust[win[j][0]].total = total;
win[j].erase(win[j].begin());
}
}
}
}
for (int i = 0; i < N; i++)
{
int tmp_total = total;
for (vector<int>::iterator it = win[i].begin(); it != win[i].end(); it++)
{
tmp_total += cust[*it].time;
cust[*it].total = tmp_total;
}

for (int i = 0; i < Q; i++)
{
int cc;
cin >> cc;
int ttt = cust[cc - 1].total;
if ( ttt - cust1[cc - 1].time >= 540)
cout << "Sorry\n";
else
{
printf("%02d:%02d\n", 8 + ttt/60,ttt%60);
}
}
return 0;

}


使用其他思想版本的代码,这个版本代码不容易出错,更简洁

#include<cstdio>  
#include<cstring>  
#include<cmath>  
#include<string>  
#include<vector>  
#include<map>  
#include<iostream>  
#include<queue>  
#include<algorithm>  
using namespace std;
struct Customer
{
int begin_time, end_time, use_time;
Customer()
{
begin_time = 0; end_time = 0; use_time=0;
}
};
struct Window
{
int end_time;
queue <Customer>q;
Window()
{
end_time = 0;
}
};
int findEarliest(Window *win,int N)
{
int mark = 0;
int time = 99999;
for (int i = 0; i < N; i++)
{
if (win[i].q.size()>0 && win[i].q.front().end_time < time)
{
time = win[i].q.front().end_time;
mark = i;
}
}
return mark;
}
int main()
{
int N, M, K, Q;
cin >> N >> M >> K >> Q;
Customer *cust = new Customer[K];
Window *win = new Window[N];
int i = 0;
for (i = 0; i < K&&i<N*M; i++)
{
cin >> cust[i].use_time;
cust[i].begin_time = win[i%N].end_time;
cust[i].end_time = cust[i].begin_time + cust[i].use_time;
win[i%N].end_time = cust[i].end_time;
win[i%N].q.push(cust[i]);
}
for (; i < K; i++)
{
cin >> cust[i].use_time;
int early = findEarliest(win, N);
win[early].q.pop();
cust[i].begin_time = win[early].end_time;
cust[i].end_time = cust[i].begin_time + cust[i].use_time;
win[early].end_time = cust[i].end_time;
win[early].q.push(cust[i]);
}
for (int j = 0; j < Q; j++)
{
int k=0;
cin >> k;
k--;
if (cust[k].begin_time >= 540)
cout << "Sorry\n";
else
printf("%02d:%02d\n", 8 + cust[k].end_time / 60, cust[k].end_time % 60);
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值