zoukankan      html  css  js  c++  java
  • 1026 Table Tennis (30)

    A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the smallest number. If all the tables are occupied, they will have to wait in a queue. It is assumed that every pair of players can play for at most 2 hours.

    Your job is to count for everyone in queue their waiting time, and for each table the number of players it has served for the day.

    One thing that makes this procedure a bit complicated is that the club reserves some tables for their VIP members. When a VIP table is open, the first VIP pair in the queue will have the priviledge to take it. However, if there is no VIP in the queue, the next pair of players can take it. On the other hand, if when it is the turn of a VIP pair, yet no VIP table is available, they can be assigned as any ordinary players.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains an integer N (<=10000) - the total number of pairs of players. Then N lines follow, each contains 2 times and a VIP tag: HH:MM:SS - the arriving time, P - the playing time in minutes of a pair of players, and tag - which is 1 if they hold a VIP card, or 0 if not. It is guaranteed that the arriving time is between 08:00:00 and 21:00:00 while the club is open. It is assumed that no two customers arrives at the same time. Following the players' info, there are 2 positive integers: K (<=100) - the number of tables, and M (< K) - the number of VIP tables. The last line contains M table numbers.

    Output Specification:

    For each test case, first print the arriving time, serving time and the waiting time for each pair of players in the format shown by the sample. Then print in a line the number of players served by each table. Notice that the output must be listed in chronological order of the serving time. The waiting time must be rounded up to an integer minute(s). If one cannot get a table before the closing time, their information must NOT be printed.

    Sample Input:

    9
    20:52:00 10 0
    08:00:00 20 0
    08:02:00 30 0
    20:51:00 10 0
    08:10:00 5 0
    08:12:00 10 1
    20:50:00 10 0
    08:01:30 15 1
    20:53:00 10 1
    3 1
    2

    Sample Output:

    08:00:00 08:00:00 0
    08:01:30 08:01:30 0
    08:02:00 08:02:00 0
    08:12:00 08:16:30 5
    08:10:00 08:20:00 10
    20:50:00 20:50:00 0
    20:51:00 20:51:00 0
    20:52:00 20:52:00 0
    3 3 2

    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    using namespace std;
    const int K = 110;
    const int INF = 100000000;
    struct Player{
        int arriveTime,startTime,serveTime;
        bool isVIP;
    }newPlayer;
    struct Table{
        int endTime,numServe;
        bool isVIP;
    }table[K];
    vector<Player> player;
    
    int convertTime(int h,int m,int s){
        return h*3600+m*60+s;
    }
    
    bool cmpArriveTime(Player a,Player b){
        return a.arriveTime < b.arriveTime;
    }
    
    bool cmpStartTmie(Player a,Player b){
        return a.startTime < b.startTime;
    }
    
    int nextVIPPlayer(int VIPi){
        VIPi++;
        while(VIPi < player.size() && player[VIPi].isVIP != 1){
            VIPi++;
        }
        return VIPi;
    }
    
    void allotTable(int pID,int tID){
        if(player[pID].arriveTime <= table[tID].endTime){
            player[pID].startTime = table[tID].endTime;
        }else{
            player[pID].startTime = player[pID].arriveTime;
        }
        table[tID].endTime = player[pID].startTime + player[pID].serveTime;
        table[tID].numServe++;
    }
    
    int main(){
        int n,m,k,vipTable;
        int stTime = convertTime(8,0,0);
        int edTime = convertTime(21,0,0);
        scanf("%d",&n);
        for(int i = 0 ; i < n; i ++){
            int h,m,s,trainTime,isVIP;
            scanf("%d:%d:%d %d %d",&h,&m,&s,&trainTime,&isVIP);
            newPlayer.arriveTime = convertTime(h,m,s);
            if(newPlayer.arriveTime >= edTime) continue;
            newPlayer.startTime = edTime;
            newPlayer.serveTime = trainTime <= 120 ? trainTime*60 : 7200;
            newPlayer.isVIP = isVIP;
            player.push_back(newPlayer);
        }
        scanf("%d%d",&k,&m);
        for(int i = 1 ; i <= k; i++){
            table[i].endTime = stTime;
            table[i].isVIP = table[i].numServe = 0;
        }
        for(int i = 0 ; i < m ; i++){
            scanf("%d",&vipTable);
            table[vipTable].isVIP = 1;
        }
        sort(player.begin(),player.end(),cmpArriveTime);
        int i = 0,VIPi = -1;
        VIPi = nextVIPPlayer(VIPi);
        while(i < player.size()){
            int idx = -1,minEndTime = INF;
            for(int j = 1; j <= k; j++){
                if(table[j].endTime < minEndTime){
                    idx = j;
                    minEndTime = table[j].endTime;
                }
            }
            if(table[idx].endTime >= edTime) break;
            if(player[i].isVIP == 1 && i < VIPi){
                i++;
                continue;
            }
            if(table[idx].isVIP == 1){//桌子是VIP 
                if(player[i].isVIP == 1){ //人是VIP 
                    allotTable(i,idx); 
                    if(VIPi == i) VIPi = nextVIPPlayer(VIPi);
                    i++;
                }else{    //人不是VIP 
                    if(VIPi < player.size() && player[VIPi].arriveTime <= table[idx].endTime){
    
                        allotTable(VIPi,idx);
                        VIPi = nextVIPPlayer(VIPi);
                    }else{
                        allotTable(i,idx);
                        i++;
                    }
                }
            }else{ //桌子不是VIP 
                if(player[i].isVIP == 0){ //人不是VIP 
                    allotTable(i,idx);
                    i++;
                }else{ //人是VIP 
                    int VIPidx = -1,minVIPEndTime = INF; //先找最先空闲的VIP桌 
                    for(int j = 1; j <= k; j++){
                        if(table[j].isVIP == 1 && table[j].endTime < minVIPEndTime){
                            VIPidx = j;
                            minVIPEndTime = table[j].endTime;
                        }
                    }
                    //如果有空闲的VIP桌,并且该VIP队员先到(处于队首) 
                    if(VIPidx != -1 &&table[VIPidx].endTime <= player[i].arriveTime){
                        allotTable(i,VIPidx);
                        if(VIPi == i) VIPi = nextVIPPlayer(VIPi);
                        i++;
                    }else{
                        allotTable(i,idx);
                        if(VIPi == i) VIPi = nextVIPPlayer(VIPi);
                        i++;
                    }
                }
            }
        }
        sort(player.begin(),player.end(),cmpStartTmie);
        for(int i = 0 ; i < player.size() && player[i].startTime < edTime; i++){
            int t1 = player[i].arriveTime;
            int t2 = player[i].startTime;
            printf("%02d:%02d:%02d ",t1/3600,t1%3600/60,t1%60);
            printf("%02d:%02d:%02d ",t2/3600,t2%3600/60,t2%60);
            printf("%.0f
    ",round((t2-t1)/60.0));
        }
        for(int i = 1; i <= k; i++){
            printf("%d",table[i].numServe);
            if(i < k) printf(" ");
        }
        return 0;
    } 
  • 相关阅读:
    Collections.unmodifiableMap,Collections.unmodifiableList,Collections.unmodifiableSet作用及源码解析
    Mybatis源码解析,一步一步从浅入深(五):mapper节点的解析
    Mybatis源码解析,一步一步从浅入深(四):将configuration.xml的解析到Configuration对象实例
    Mybatis源码解析,一步一步从浅入深(三):实例化xml配置解析器(XMLConfigBuilder)
    Mybatis源码解析,一步一步从浅入深(二):按步骤解析源码
    Cannot find class: com.mysql.jdbc.Driver错误及解决办法。
    Mybatis源码解析,一步一步从浅入深(一):创建准备工程
    Maven 创建项目之简单示例
    常用注解记录
    jmeter性能测试工具
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/9475083.html
Copyright © 2011-2022 走看看