UVA 822 Queue and A

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">  感觉UVA越来越做不动了,这个题又卡了我一整天。最初是编写代码边想思路,导致很多条件都没考虑到只好不断修改,最后程序直接崩溃连哪错了都不知道了。</span>

  我的思路:主体:讨论人,把客服循环一遍,当找到有空的客服时,再找到他要干的工作。

                        其他:1,用map<int,vector>把一个工作及其客服对应起来,遍历map对vector进行排序,以此找到每个工作对应的人。

                                    2,用map把主题与数组编号,客服与数组编号对应起来,这样查找时就方便了。

                                    3,0结束的条件是:剩余的任务为且所有客服都已执行完任务。

#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<cstdio>
using namespace std;
struct topic
{
    int tid,num,t0,t,dt,num1;
    int is_exe, time, time_between, first;
};
struct member
{
    int pid,k,last,busy,time,leixing,time1;
    vector<int>pidk;
    bool operator < (const member&a) const
     {
         if(a.last == last) return a.pid > pid;
         else return a.last > last;
     }
};
vector<topic>topics;
vector<member>staff;
map<int,int>ha;  //将任务主题与相应的tid对应
map<int,vector<member> >num; //做每个工作的人,int是tid,后面的vector是做这个工作的客服
map<int,int>ren;   //将客服与pid对应
int can_exe(int x, int t)
{
    if(t < topics[x].t0) return 0;
    if(topics[x].dt == 0) return topics[x].num > 0;
    if(topics[x].num == 0)return 0;
    if((t+topics[x].dt-topics[x].t0)/topics[x].dt == (topics[x].num1-topics[x].num)) return 0;
    return 1;
}
int main()
{
    int m, n, kase = 0, i, j, k;
    while(cin >> m && m){
        int time = 0;
        topics.clear();
        staff.clear();
        ha.clear();
        ren.clear();
        for(i = 0; i < m; i++){
            topic a;
            cin>>a.tid>>a.num>>a.t0>>a.t>>a.dt;
            a.num1 = a.num;
            topics.push_back(a);
            ha[a.tid] = i;
        }
        cin >> n;
        for(i = 0; i < n; i++){
            member a;
            a.pidk.clear();
            a.time1 = a.last = a.busy = a.time = 0;
            int x;
            cin >> a.pid >> a.k;
            ren[a.pid] = i;
            for(j = 0; j < a.k; j++){
                cin >> x;
                a.pidk.push_back(x);
            }
            staff.push_back(a);
        }
        loop:for(time = 0; time < 500000; time++){
            num.clear();
            for(i = 0; i < n; i++)   //找谁有空
                if(!staff[i].busy)
                  for(j = 0; j < staff[i].k; j++)  //找到此人能够执行的工作
                     if(can_exe(ha[staff[i].pidk[j]],time)){
                        if(!num.count(staff[i].pidk[j])) num[staff[i].pidk[j]] = vector<member>();
                        num[staff[i].pidk[j]].push_back(staff[i]);
                        break;
                     }
           map<int,vector<member> >::iterator it;
           for(it = num.begin(); it != num.end(); ++it){
              sort(it->second.begin(),it->second.end());
              int temp = ren[it->second[0].pid];
              staff[temp].busy = 1;
              staff[temp].last = time;
              staff[temp].leixing = it->first;
              topics[ha[it->first]].num--;
           }
           int all_zero = 1;
           for(i = 0; i < n; i++){
              if(staff[i].busy) staff[i].time++;
              if(staff[i].busy && staff[i].time == topics[ha[staff[i].leixing]].t) {staff[i].time = 0;staff[i].busy = 0;}
           }
           for(i = 0; i < m; i++) if(topics[i].num > 0) {all_zero = 0;break;}
           for(i = 0; i < n; i++) if(staff[i].time > 0) {all_zero = 0;break;}
           if(all_zero) break;
        }
        printf("Scenario %d: All requests are serviced within %d minutes.\n",++kase, time+1);
    }
    return 0;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值