考查点:模拟题
思路:用优先队列可提高效率,关键在于维护每个窗口的结束时间数组,每次遍历排队用户时更新结束时间,这里更新时多了个+号结果调了半天
#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#define FOR(i, x, y) for(int i = x; i <= y; i++)
#define rFOR(i, x, y) for(int i = x; i >= y; i--)
#define MAXN 10010
#define oo 0x3f3f3f3f
using namespace std;
struct customer{
int start,process;
}person[10010];
bool cmp(customer a,customer b){
return a.start<b.start;
}
int wait[110];
int main()
{
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif // LOCAL
int n,k;
int hh,mm,ss,pp;
scanf("%d%d",&n,&k);
int num=0;
FOR(i,0,n-1)
{
scanf("%d:%d:%d %d",&hh,&mm,&ss,&pp);
int tmp=hh*3600+mm*60+ss;
if(tmp<=17*3600){
person[num].process=(pp<60?pp*60:3600);
person[num].start=tmp;
num++;
}
}
double sum=0;
sort(person,person+num,cmp);
FOR(i,0,k-1)
wait[i]=8*3600;
FOR(i,0,num-1)
{
int wmin=oo;int id;
FOR(j,0,k-1)
{
if(wait[j]<wmin){
wmin=wait[j];
id=j;
}
}
if(person[i].start<wait[id]){
sum+=(wait[id]-person[i].start);
wait[id]+=person[i].process;
}else{
wait[id]=(person[i].start+person[i].process);
}
}
if(num==0) printf("0.0");
else{
printf("%.1f",sum/60.0/num);
}
return 0;
}
优先队列:由于默认最大值。所以要重载运算符
#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#define FOR(i, x, y) for(int i = x; i <= y; i++)
#define rFOR(i, x, y) for(int i = x; i >= y; i--)
#define MAXN 10010
#define oo 0x3f3f3f3f
using namespace std;
struct customer{
int start,process;
bool operator<(const customer& a)const{
if(a.start<start)return true;
else return false;
}
}person;
struct window{
int time;
bool operator<(const window& a)const{
if(time>a.time)return true;
else return false;
}
}w;
bool cmp(customer a,customer b){
return a.start<b.start;
}
priority_queue<customer> cu;
priority_queue<window> ww;
int main()
{
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif // LOCAL
int n,k;
int hh,mm,ss,pp;
scanf("%d%d",&n,&k);
int num=0;
FOR(i,0,n-1)
{
scanf("%d:%d:%d %d",&hh,&mm,&ss,&pp);
int tmp=hh*3600+mm*60+ss;
if(tmp<=17*3600){
person.process=(pp<60?pp*60:3600);
person.start=tmp;
cu.push(person);
}
}
double sum=0;
num=cu.size();
FOR(i,0,k-1)
{
w.time=8*3600;
ww.push(w);
}
while(!cu.empty())
{
w=ww.top();
int endt=w.time;
person=cu.top();
cu.pop();
if(endt<=person.start){
endt=person.start+person.process;
ww.pop();
w.time=endt;
ww.push(w);
}else{
sum+=(endt-person.start);
endt+=person.process;
ww.pop();
w.time=endt;
ww.push(w);
}
}
if(num==0) printf("0.0");
else{
printf("%.1f",sum/60.0/num);
}
return 0;
}