1. 程式人生 > >銀行排隊問題之單佇列多視窗加VIP服務

銀行排隊問題之單佇列多視窗加VIP服務

#include <bits/stdc++.h>
using namespace std;
typedef struct Window
{
   int time;
   int num;
}Winow;
typedef struct Person
{
    int atime, wtime, ntime, isvip;
}Person;
Winow win[15];
Person per[1010];
int N, K, vipwin, who_possess_vipwin = 0;
int vipper[1010];
int vipnum = 0, curvippos = 0;
int VipGetPos(Person p)
{
    int pos, min_t = 100000;
    for(int i = 0; i < K; ++i)
    {
        if(p.atime >= win[i].time)
        {
            pos = i;
            break;
        }
        if(win[i].time < min_t)
        {
           min_t =  win[i].time;
           pos = i;
        }
    }
    if(win[pos].time == win[vipwin].time || win[vipwin].time <= p.atime)
        pos = vipwin;
    if(pos == vipwin)
        who_possess_vipwin = 1;
    return pos;
}
int BeggarGetPos(Person p)//哈哈 惡搞一下蝦米音樂
{
    int pos, min_t, pos1, pos2, min_t1 = 100000, min_t2 = 100000;
    for(int i = 0; i < K; ++i)
    {
        if(p.atime > win[i].time)
        {
            pos1 = i;
            return pos1;
        }
        if(win[i].time < min_t1)
        {
           min_t2 = min_t1;
           pos2 = pos1;
           min_t1 =  win[i].time;
           pos1 = i;
        }
        else
        {
            if(win[i].time < min_t2)
            {
                min_t2 = win[i].time;
                pos2 = i;
            }
        }
    }
    if(pos1 == vipwin)
    {
        //int flag;
        if(per[vipper[curvippos]].atime > win[pos1].time || vipnum == 0)
            pos1 = pos1;
        else
        pos1 = pos2;
    }
    pos = pos1;
    if(pos == vipwin)
        who_possess_vipwin = 0;
    return pos;
}
int main()
{
    scanf("%d", &N);
    for(int i = 1; i <= N; ++i)
    {
        scanf("%d %d %d", &per[i].atime, &per[i].ntime, &per[i].isvip);
        if(per[i].ntime > 60)
            per[i].ntime = 60;
        if(per[i].isvip == 1)
            vipper[vipnum++] = i;
    }
    scanf("%d %d", &K, &vipwin);
    for(int i = 0; i < K; ++i)
    {
        win[i].time = 0;
        win[i].num = 0;
    }
    for(int i = 1; i <= N; ++i)
    {

          int winpos;
          if(per[i].isvip == 1)
          {
            winpos = VipGetPos(per[i]);
            curvippos++;
          }
          else
          {
            winpos = BeggarGetPos(per[i]);
          }
//          for(int j = 0; j < K; ++j)
//            printf("win[%d]的時間為 %d num為 %d\n", j, win[j].time, win[j].num);
//          printf("要處理的是%d到達時間為 %d 事物時間為%d", i, per[i].atime, per[i].ntime);
//          if(!per[i].isvip)
//            printf("不");
//          cout << "是會員" << endl;
//          cout << "選擇視窗為" << winpos << endl;
          per[i].wtime = win[winpos].time - per[i].atime > 0?win[winpos].time - per[i].atime:0;
          win[winpos].time = per[i].ntime + per[i].atime + per[i].wtime;
          win[winpos].num++;

    }
    int sum = 0;
    int max_w = -1;
    int max_t = -1;
    for(int i = 1; i <= N; ++i)
    {
        //printf("id = %d wtime = %d\n", i, per[i].wtime);
        sum += per[i].wtime;
        if(max_w < per[i].wtime)
            max_w = per[i].wtime;
    }
    for(int i = 0; i < K; ++i)
    {
        if(win[i].time > max_t)
            max_t = win[i].time;
    }
    double ave = (sum * 1.0) / (N * 1.0);
    printf("%.1lf %d %d\n", ave, max_w, max_t);
    for(int i = 0; i < K; ++i)
    {
      if(i != 0)
            printf(" ");
      printf("%d", win[i].num);
    }
}