【九度】题目1326:Waiting in Line

题目地址:http://ac.jobdu.com/problem.php?pid=1326
题目描述:

Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are:

  • The space inside the yellow line in front of each window is enough to contain a line with M customers. Hence when all the N lines are full, all the customers after (and including) the (NM+1)st one will have to wait in a line behind the yellow line.
  • Each customer will choose the shortest line to wait in when crossing the yellow line. If there are two or more lines with the same length, the customer will always choose the window with the smallest number.
  • Customer[i] will take T[i] minutes to have his/her transaction processed.
  • The first N customers are assumed to be served at 8:00am.

Now given the processing time of each customer, you are supposed to tell the exact time at which a customer has his/her business done.

For example, suppose that a bank has 2 windows and each window may have 2 custmers waiting inside the yellow line. There are 5 customers waiting with transactions taking 1, 2, 6, 4 and 3 minutes, respectively. At 08:00 in the morning, customer1 is served at window1 while customer2 is served at window2. Customer3 will wait in front of window1 and customer4 will wait in front of window2. Customer5 will wait behind the yellow line.

At 08:01, customer1 is done and customer5 enters the line in front of window1 since that line seems shorter now. Customer2 will leave at 08:02, customer4 at 08:06, customer3 at 08:07, and finally customer5 at 08:10.

输入:

Each input file contains one test case. Each case starts with a line containing 4 positive integers: N (<=20, number of windows), M (<=10, the maximum capacity of each line inside the yellow line), K (<=1000, number of customers), and Q (<=1000, number of customer queries).

The next line contains K positive integers, which are the processing time of the K customers.

The last line contains Q positive integers, which represent the customers who are asking about the time they can have their transactions done. The customers are numbered from 1 to K.

输出:

For each of the Q customers, print in one line the time at which his/her transaction is finished, in the format HH:MM where HH is in [08, 17] and MM is in [00, 59]. Note that since the bank is closed everyday after 17:00, for those customers who cannot be served before 17:00, you must output "Sorry" instead.

样例输入:
2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7
样例输出:
08:07
08:06
08:10
17:00
Sorry
答疑:
解题遇到问题?分享解题心得?讨论本题请访问: http://t.jobdu.com/thread-8049-1-1.html
模拟题。
题目大意:银行有n个窗口,每个窗口的黄线内可以站m个人。
现在有k个人,编号从1到k,对于每个人来说,如果黄线内人没有满,
则依次选择窗口。否则的话,选择队伍短的那一个。
可以选用队列。n个窗口,就n个队列。
如果队列人数不满m个,就按照编号依次循环入队。
当人数大于N*m的时候,就选择每个队伍的第一个人最短时间办完业务的那一队。
入队出队,同时统计花费的时间。
pat平台是只要17点之前开始业务,就算ok。
Jobdu是,即使是17点之前开始办业务,17点之后仍然没有办完就是sorry。
C++ AC
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
const int maxk = 1002;
const int maxn = 22;
int n,m,k,q;
int i,j;
int taskArr[maxk];
queue<int> qu[maxn];
int allTime[maxk]; 
int main(){
    while(scanf("%d%d%d%d",&n,&m,&k,&q) != EOF){
        for(i = 1; i < k+1; i++){
            scanf("%d",&taskArr[i]);
        }
        for(i = 0; i < n; i++){
            while(!qu[i].empty()){
                qu[i].pop();
            }
        }
        memset(allTime,0,sizeof(allTime));
        int count = 0;
        for(i = 0; i < m; i++){
            for(j = 0; j < n; j++){
                count++;
                if(count > k){
                    break;
                }
                allTime[count] = taskArr[count];
                if(!qu[j].empty()){
                    allTime[count] += allTime[qu[j].back()];
                }
                qu[j].push(count);
            }
            if(count > k){
                break;
            }
        }         
        while(count < k){
            count++;
            int minTime = allTime[qu[0].front()];
            int groupId = 0;
            for(i = 1; i < n; i++){
                int tempTime = allTime[qu[i].front()];
                if(tempTime < minTime){
                    minTime = tempTime;
                    groupId = i;
                }   
            }
            allTime[count] = taskArr[count];
            if(!qu[groupId].empty()){
                allTime[count] += allTime[qu[groupId].back()];
            }
            qu[groupId].pop();
            qu[groupId].push(count);
        }
        int rank;
        for(i = 0; i < q; i ++){
            scanf("%d",&rank);
            if(allTime[rank] > 540){
                printf("Sorry\n");
            }else{
                printf("%02d:%02d\n",8 + allTime[rank]/60,allTime[rank]%60);
            }
        }
    }
    return 0;
} 
/**************************************************************
    Problem: 1326
    User: wangzhenqing
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1064 kb
****************************************************************/

Java AC

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class Main {
    /*
     * 1326
     */
    public static void main(String[] args) throws Exception {
        StreamTokenizer st = new StreamTokenizer(new BufferedReader(
                new InputStreamReader(System.in)));
        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            int n = (int)st.nval;
            st.nextToken();
            int m = (int)st.nval;
            st.nextToken();
            int k = (int)st.nval;
            st.nextToken();
            int q = (int)st.nval;
            int tArray[] = new int[k+1];
            for (int i = 1; i < k+1; i++) {
                st.nextToken();
                tArray[i] = (int)st.nval;
            }
            Map<Integer, Customer> resultMap = new HashMap<Integer, Customer>();
            List<Customer> allList[] = new ArrayList[n];
            int array[] = new int[n];
            int count = 0;
            int newM = m;
            boolean flag = true;
            while (newM > 0) {
                newM--;
                for (int i = 0; i < n; i++) {
                    count++;
                    if (count > k ) {
                        flag = false;
                        break;
                    }
                    if (allList[i] == null) {
                        allList[i] = new ArrayList<Customer>();
                    }
                    int size = allList[i].size();
                    int allTime = 0;
                    if (size != 0) {
                        allTime = allList[i].get(size-1).allTime; 
                    }
                    Customer cu = new Customer(i, tArray[count], allTime+tArray[count]);
                    allList[i].add(cu);
                    resultMap.put(count, cu);
                }
                if (!flag) {
                    break;
                }
            }
            while (count < k) {
                count++;
                Customer minCus = allList[0].get(array[0]);
                int min = minCus.allTime;
                int groupId = minCus.groupId;
                for (int i = 1; i < n; i++) {
                    Customer tempCus = allList[i].get(array[i]);
                    if (tempCus.allTime < min) {
                        min = tempCus.allTime;
                        groupId = tempCus.groupId;
                    }
                }
                int tempAllTime = allList[groupId].get(allList[groupId].size()-1).allTime;
                array[groupId]++;
                Customer finalCustomer = new Customer(groupId,tArray[count], tempAllTime+tArray[count]);
                allList[groupId].add(finalCustomer);
                resultMap.put(count, finalCustomer);
            }
//          for (int i = 0; i < array.length; i++) {
//              System.out.println(array[i]);
//          }
            for (int i = 0; i < q; i++) {
                st.nextToken();
                int rank = (int)st.nval;
                Customer finalCustomer = resultMap.get(rank);
                if (finalCustomer == null) {
                    System.out.println("Sorry");
                }else {
                    int minutes = finalCustomer.allTime;
                    if (minutes > 540) {
                        System.out.println("Sorry");
                        continue;
                    }
                    int hour = minutes/60;
                    int newMi = minutes%60;
                    System.out.println(((hour+8) < 10 ? "0"+(hour+8) : (hour+8))+":"+ (newMi < 10 ? "0"+newMi : newMi));
                }
            }
        }
    }
    private static class Customer{
        private int groupId;
        private int taskTime;
        private int allTime;
        public Customer(int groupId,int taskTime, int allTime) {
            super();
            this.groupId = groupId;
            this.taskTime = taskTime;
            this.allTime = allTime;
        }
    }
}
/**************************************************************
    Problem: 1326
    User: wangzhenqing
    Language: Java
    Result: Accepted
    Time:510 ms
    Memory:20404 kb
****************************************************************/

你可能感兴趣的:(【九度】题目1326:Waiting in Line)