输入样例
9
0 20
1 15
1 61
2 10
10 5
10 3
30 18
31 25
31 2
3
关于这道题我觉得自己写得蛮有意思的
把排队的人变成一个队列,然后逐个去选择,然后pop();
选择时选择窗口空闲时间最早的那个,也就是nexttime 最小的那个
有两种情况,一种是顾客到的时候,有窗口空闲,那么窗口的下一个空闲时间变成顾客的到达时间加上完成时间
一种是顾客到的时候没空闲,那么选择时间短的,等待时间由窗口下一个空闲时间减去到达时间
先计算等待时间才能计算下一个时间,这是我踩的一个雷,代码的运行顺序会影响变量的值
为了减少判断时间,把第一个顾客安排在第一个窗口,然后弹出队头的顾客
#include <bits/stdc++.h>
using namespace std;
class guke{
public:
int arrive_time;
int work_time;
guke(int arriveTime, int workTime) : arrive_time(arriveTime), work_time(workTime) {}
};
class window{
public:
// windows should jilu nexttime , withtime ,longgestwithtime
double nexttime;
double withtime;
double lwtime;
window() {
nexttime=0;
withtime=0;
lwtime=0;
}
void jisuan(guke guketemp)
{
// guke arrive free window, needn't with
if(guketemp.arrive_time>=nexttime)
{
nexttime=guketemp.arrive_time+guketemp.work_time;
}
//guke shoule with
else
{
//nexttime = when guke finish
//guke withtime =nexttime-guke arrive time, winodw withtime +=
double gukewithtime=nexttime-guketemp.arrive_time;
nexttime+=guketemp.work_time;
withtime+=gukewithtime;
//bijiao withtime longer?
if(gukewithtime>lwtime)
lwtime=gukewithtime;
}
}
};
int lookforwhichnear(int arrivetime,window* windowlist,int windownum)
{
// look for which nearest
int nexttimenear=windowlist[0].nexttime;
int choose=0;
for (int i = 1; i <windownum ; ++i) {
//if xiaoyu nexttime,nextime change,choose=i;
if(windowlist[i].nexttime<nexttimenear) {
nexttimenear=windowlist[i].nexttime;
choose = i;
}
}
return choose;
}
int main()
{
//shuru guke chuangjian gukelist
int gukenum;
cin>>gukenum;
queue<guke> gukelist;
for (int i = 0; i < gukenum; ++i) {
int arrivetime,worktime;
cin>>arrivetime>>worktime;
guke guke1(arrivetime,worktime);
gukelist.push(guke1);
}
//shuru windownum,jianli window[]
int windownum;
cin>>windownum;
window windowlist[windownum+1];
//jiaru diyige guke;
guke guketemp=gukelist.front();
//first guke do not with,
windowlist[0].nexttime=guketemp.arrive_time+guketemp.work_time;
//jiaru wan,guke yao likai duiwu
gukelist.pop();
//one by one xuanze
while(!gukelist.empty()){
guketemp=gukelist.front();
//jiancha which windows nexttime is nearer choose
int chosewhich=lookforwhichnear(guketemp.arrive_time,windowlist,windownum);
// which near,which should change nexttime and so on;
windowlist[chosewhich].jisuan(guketemp);
//paiwandui,next one;
gukelist.pop();
}
//jiancha which windows nexttime is nearer choose
//windows should jilu nexttime , withtime ,longgestwithtime
// bijiao which windows nextime bigger
//jisuan withtimesum
double withsum=0;
double lwtime=0;
double lnexttime=0;
for (int i = 0; i <windownum ; ++i) {
withsum+=windowlist[i].withtime;
//bijiao longgestwithtime;
if(windowlist[i].lwtime>lwtime)
lwtime=windowlist[i].lwtime;
//bijiao nexttime
if(windowlist[i].nexttime>lnexttime)
lnexttime=windowlist[i].nexttime;
}
withsum/=gukenum;
cout<<fixed<<setprecision(1)<<withsum<<" ";
cout<<(int)lwtime<<" ";
cout<<(int)lnexttime<<endl;
}
第一个博客,写写写写