Jobdu -1326-Waiting in Line-模拟题 | Acm之家


九度-1326-Waiting in Line-模拟题 | Acm之家
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
题目大意:
有K个客户去银行办理业务,黄线内可以排队M个人。黄线外的人会优先选择人数少的队去排。 每个人花费的时间是不一样的。问每个人业务结束的时间。稍微有些繁琐的模拟。即使是17点之前开始办业务,17点之后仍然没有办完就是sorry。




PAT 1014. Waiting in Line
  1. typedef struct customer  
  2. {  
  3.     int begin;  
  4.     int end;  
  5.     int trans;  
  6. }Customer;  
  7.   
  8. Customer c[MAX];  
  9.   
  10. int main(int argc,char *argv[])  
  11. {  
  12.     int n,m,k,q;  
  13.     int i,j;  
  14.     queue<int> windows[20];  
  15.     int now,shut_time;  
  16.     int count,to_finish;  
  17.     scanf("%d%d%d%d",&n,&m,&k,&q);  
  18.     for(i=1;i<=k;i++)  
  19.     {  
  20.         scanf("%d",&c[i].trans);  
  21.         c[i].begin=-1;  
  22.         c[i].end=-1;  
  23.     }  
  24.     now=0;  
  25.     shut_time=(17-8)*60;  
  26.     count=1;  
  27.     for(i=0;i<n&&count<=k;i++)//向N个窗口压入一个顾客,开始时间为零  
  28.     {                         //结束时间为交易时间  
  29.         windows[i].push(count);  
  30.         c[count].begin=0;  
  31.         c[count].end=c[count].trans;  
  32.         count++;  
  33.     }  
  34.     for(i=0;i<m-1&&count<=k;i++)//向N个窗口压入分别M-1个顾客,开始时间  
  35.         for(j=0;j<n&&count<=k;j++)//是上一个结束时间,结束时间是开始时间  
  36.         {                         //加上交易时间  
  37.             c[count].begin=c[windows[j].back()].end;  
  38.             c[count].end=c[count].begin+c[count].trans;  
  39.             windows[j].push(count);  
  40.             count++;  
  41.         }  
  42.     while(count<=k&&now<shut_time)//将第N*M+1以及之后  
  43.     {                   //顾客插入最先离开顾客的窗口  
  44.         to_finish=0;//其为即将完成交易的顾客的窗口编号  
  45.         for(i=1;i<n;i++)  
  46.          if(c[windows[i].front()].end<c[windows[to_finish].front()].end)  
  47.              to_finish=i;//找到最先完成的窗口号  
  48.         now=c[windows[to_finish].front()].end;  
  49.         windows[to_finish].pop();  
  50.         c[count].begin=c[windows[to_finish].back()].end;  
  51.         c[count].end=c[count].begin+c[count].trans;  
  52.         windows[to_finish].push(count);  
  53.         count++;  
  54.     }  
  55.     for(i=0;i<q;i++)  
  56.     {  
  57.         int query;  
  58.         scanf("%d",&query);  
  59.         if(c[query].end==-1||c[query].begin>=shut_time)//从某个顾客开始  
  60.             printf("Sorry\n");                     //还没服务就要关门了  
  61.         else  
  62.             printf("%.2d:%.2d\n",c[query].end/60+8,c[query].end%60);  
  63.     }  
  64.   
  65.     return 0;  
  66. }  
http://www.acmerblog.com/jiudu-1326-waiting-in-line-3761.html
http://blog.csdn.net/u013027996/article/details/17475331
  1. int main(){  
  2.     while(scanf("%d%d%d%d",&n,&m,&k,&q) != EOF){  
  3.         for(i = 1; i < k+1; i++){  
  4.             scanf("%d",&taskArr[i]);  
  5.         }  
  6.         for(i = 0; i < n; i++){  
  7.             while(!qu[i].empty()){  
  8.                 qu[i].pop();  
  9.             }  
  10.         }  
  11.         memset(allTime,0,sizeof(allTime));  
  12.         int count = 0;  
  13.         for(i = 0; i < m; i++){  
  14.             for(j = 0; j < n; j++){  
  15.                 count++;  
  16.                 if(count > k){  
  17.                     break;  
  18.                 }  
  19.                 allTime[count] = taskArr[count];  
  20.                 if(!qu[j].empty()){  
  21.                     allTime[count] += allTime[qu[j].back()];  
  22.                 }  
  23.                 qu[j].push(count);  
  24.             }  
  25.             if(count > k){  
  26.                 break;  
  27.             }  
  28.         }           
  29.         while(count < k){  
  30.             count++;  
  31.             int minTime = allTime[qu[0].front()];  
  32.             int groupId = 0;  
  33.             for(i = 1; i < n; i++){  
  34.                 int tempTime = allTime[qu[i].front()];  
  35.                 if(tempTime < minTime){  
  36.                     minTime = tempTime;  
  37.                     groupId = i;  
  38.                 }     
  39.             }  
  40.             allTime[count] = taskArr[count];  
  41.             if(!qu[groupId].empty()){  
  42.                 allTime[count] += allTime[qu[groupId].back()];  
  43.             }  
  44.             qu[groupId].pop();  
  45.             qu[groupId].push(count);  
  46.         }  
  47.         int rank;  
  48.         for(i = 0; i < q; i ++){  
  49.             scanf("%d",&rank);  
  50.             if(allTime[rank] > 540){  
  51.                 printf("Sorry\n");  
  52.             }else{  
  53.                 printf("%02d:%02d\n",8 + allTime[rank]/60,allTime[rank]%60);  
  54.             }  
  55.         }  
  56.     }  
  57.     return 0;  
  58. }   
http://noalgo.info/697.html
http://www.hijeffrey.com/pat-advanced-1014-waiting-in-line.html
  1. typedef struct Customer{  
  2.     //开始服务的时间,为了后续判断是否在 17:00前进行服务   
  3.     int startTime;   
  4.     int endTime;  
  5.     //服务时间   
  6.     int processTime;   
  7. }Customer;   
  8. typedef struct Window{  
  9.     int head;  
  10.     int tail;  
  11.     //最大在黄线内就10人   
  12.     int winQueue[WINWAIT];  
  13.     //窗口当前服务结束时间   
  14.     int endTime;  
  15. }Win;   
  16. int main(int argc, char *argv[]) {  
  17. //  freopen("1.txt","r",stdin);  
  18.     //定义及初始化   
  19.     Customer cus[1010];  
  20.     Win win[25];   
  21.         int N,M,K,Q;  
  22.     int i,j;  
  23.     for( i = 0 ; i < 1010 ; i++ ){  
  24.         cus[i].startTime = 0;  
  25.         cus[i].endTime = 0;  
  26.         cus[i].processTime = 0;  
  27.     }  
  28.     for( i = 0 ; i < 25 ; i++ ){  
  29.         win[i].endTime = 0;  
  30.         win[i].head = 0;  
  31.         win[i].tail = 0;  
  32.     }  
  33.     //数据录入  
  34.     scanf("%d%d%d%d",&N,&M,&K,&Q);  
  35.     for( i = 1 ; i <= K ; i++) {  
  36.         scanf("%d",&cus[i].processTime);  
  37.     }  
  38.     //进行处理  
  39.     i = 1 ;   
  40.     //用 i 表示 排在黄线内的客户 ,   
  41.     //用 j 表示 当前的窗口号,从小到大为黄线内的客户服务,注意后面 j 赋值的意义   
  42.     for( j = 0 ; i <= N * M && i <= K ; i++, j=(j+1)%N ){  
  43.         //当前客户服务的开始时间就是窗口为前一个客户服务的结束时间   
  44.         cus[i].startTime = win[j].endTime ;  
  45.         //客户服务结束   
  46.         cus[i].endTime = win[j].endTime + cus[i].processTime;  
  47.         //更新当前窗口服务结束时间以便为下一位服务  
  48.         win[j].endTime += cus[i].processTime;  
  49.         //对窗口进行入队操作  
  50.         win[j].winQueue[win[j].tail] = i ;   
  51.         win[j].tail =([win[j].tail +1)%WINWAIT ; //这里也需要取模,不然会越界!  
  52.         //坑,一开始考虑是不需要的,如果取模不就覆盖第一个排队的,后来朋友说只是下标到第一个没有覆盖  
  53.         //想想也对,也不知道一开始怎么想的。  
  54.     }   
  55.     //黄线内满之后还有客户需要服务 再计算他们的服务时间   
  56.     //从黄线前找出服务时间最早结束的窗口,出队,再把一个黄线后的客户入队   
  57.     int minTime;  
  58.     int cIndex;  
  59.     int pos;  
  60.     for( ; i <= K ; i++){  
  61.         minTime = INF;  
  62.         for( j = 0 ; j < N ; j++ ){  
  63.             //排在窗口 j 的第一个客户   
  64.             cIndex = win[j].winQueue[ win[j].head ];  
  65.             if( cus[cIndex].endTime < minTime ){  
  66.                 minTime = cus[cIndex].endTime;  
  67.                 pos = j;  
  68.             }  
  69.         }  
  70.         //找到最早结束的一个窗口  
  71.         cus[i].startTime = win[pos].endTime ;  
  72.         //客户服务结束   
  73.         cus[i].endTime = win[pos].endTime + cus[i].processTime;  
  74.         //更新当前窗口服务结束时间以便为下一位服务  
  75.         win[pos].endTime += cus[i].processTime;  
  76.         //对窗口进行入队操作  
  77.         win[pos].head = (win[pos].head+1)%WINWAIT;   
  78.         win[pos].winQueue[win[pos].tail] = i ;   
  79.         win[pos].tail = (win[pos].tail+1)%WINWAIT;  
  80.     }     
  81.     //查询输出  
  82.     int hour,minutes;  
  83.     while(Q--){  
  84.         scanf("%d",&j);  
  85.         if( cus[j].startTime + 8*60 >= 17*60 ){  
  86.               printf("Sorry\n");    
  87.         }else{  
  88.              hour = (cus[j].endTime + 8*60) / 60;  
  89.              minutes = (cus[j].endTime + 8*60) % 60;  
  90.             printf("%02d:%02d\n",hour,minutes);    
  91.         }  
  92.     }   
  93.     return 0;  
Read full article from 九度-1326-Waiting in Line-模拟题 | Acm之家

Labels

LeetCode (1432) GeeksforGeeks (1122) LeetCode - Review (1067) Review (882) Algorithm (668) to-do (609) Classic Algorithm (270) Google Interview (237) Classic Interview (222) Dynamic Programming (220) DP (186) Bit Algorithms (145) POJ (141) Math (137) Tree (132) LeetCode - Phone (129) EPI (122) Cracking Coding Interview (119) DFS (115) Difficult Algorithm (115) Lintcode (115) Different Solutions (110) Smart Algorithm (104) Binary Search (96) BFS (91) HackerRank (90) Binary Tree (86) Hard (79) Two Pointers (78) Stack (76) Company-Facebook (75) BST (72) Graph Algorithm (72) Time Complexity (69) Greedy Algorithm (68) Interval (63) Company - Google (62) Geometry Algorithm (61) Interview Corner (61) LeetCode - Extended (61) Union-Find (60) Trie (58) Advanced Data Structure (56) List (56) Priority Queue (53) Codility (52) ComProGuide (50) LeetCode Hard (50) Matrix (50) Bisection (48) Segment Tree (48) Sliding Window (48) USACO (46) Space Optimization (45) Company-Airbnb (41) Greedy (41) Mathematical Algorithm (41) Tree - Post-Order (41) ACM-ICPC (40) Algorithm Interview (40) Data Structure Design (40) Graph (40) Backtracking (39) Data Structure (39) Jobdu (39) Random (39) Codeforces (38) Knapsack (38) LeetCode - DP (38) Recursive Algorithm (38) String Algorithm (38) TopCoder (38) Sort (37) Introduction to Algorithms (36) Pre-Sort (36) Beauty of Programming (35) Must Known (34) Binary Search Tree (33) Follow Up (33) prismoskills (33) Palindrome (32) Permutation (31) Array (30) Google Code Jam (30) HDU (30) Array O(N) (29) Logic Thinking (29) Monotonic Stack (29) Puzzles (29) Code - Detail (27) Company-Zenefits (27) Microsoft 100 - July (27) Queue (27) Binary Indexed Trees (26) TreeMap (26) to-do-must (26) 1point3acres (25) GeeksQuiz (25) Merge Sort (25) Reverse Thinking (25) hihocoder (25) Company - LinkedIn (24) Hash (24) High Frequency (24) Summary (24) Divide and Conquer (23) Proof (23) Game Theory (22) Topological Sort (22) Lintcode - Review (21) Tree - Modification (21) Algorithm Game (20) CareerCup (20) Company - Twitter (20) DFS + Review (20) DP - Relation (20) Brain Teaser (19) DP - Tree (19) Left and Right Array (19) O(N) (19) Sweep Line (19) UVA (19) DP - Bit Masking (18) LeetCode - Thinking (18) KMP (17) LeetCode - TODO (17) Probabilities (17) Simulation (17) String Search (17) Codercareer (16) Company-Uber (16) Iterator (16) Number (16) O(1) Space (16) Shortest Path (16) itint5 (16) DFS+Cache (15) Dijkstra (15) Euclidean GCD (15) Heap (15) LeetCode - Hard (15) Majority (15) Number Theory (15) Rolling Hash (15) Tree Traversal (15) Brute Force (14) Bucket Sort (14) DP - Knapsack (14) DP - Probability (14) Difficult (14) Fast Power Algorithm (14) Pattern (14) Prefix Sum (14) TreeSet (14) Algorithm Videos (13) Amazon Interview (13) Basic Algorithm (13) Codechef (13) Combination (13) Computational Geometry (13) DP - Digit (13) LCA (13) LeetCode - DFS (13) Linked List (13) Long Increasing Sequence(LIS) (13) Math-Divisible (13) Reservoir Sampling (13) mitbbs (13) Algorithm - How To (12) Company - Microsoft (12) DP - Interval (12) DP - Multiple Relation (12) DP - Relation Optimization (12) LeetCode - Classic (12) Level Order Traversal (12) Prime (12) Pruning (12) Reconstruct Tree (12) Thinking (12) X Sum (12) AOJ (11) Bit Mask (11) Company-Snapchat (11) DP - Space Optimization (11) Dequeue (11) Graph DFS (11) MinMax (11) Miscs (11) Princeton (11) Quick Sort (11) Stack - Tree (11) 尺取法 (11) 挑战程序设计竞赛 (11) Coin Change (10) DFS+Backtracking (10) Facebook Hacker Cup (10) Fast Slow Pointers (10) HackerRank Easy (10) Interval Tree (10) Limited Range (10) Matrix - Traverse (10) Monotone Queue (10) SPOJ (10) Starting Point (10) States (10) Stock (10) Theory (10) Tutorialhorizon (10) Kadane - Extended (9) Mathblog (9) Max-Min Flow (9) Maze (9) Median (9) O(32N) (9) Quick Select (9) Stack Overflow (9) System Design (9) Tree - Conversion (9) Use XOR (9) Book Notes (8) Company-Amazon (8) DFS+BFS (8) DP - States (8) Expression (8) Longest Common Subsequence(LCS) (8) One Pass (8) Quadtrees (8) Traversal Once (8) Trie - Suffix (8) 穷竭搜索 (8) Algorithm Problem List (7) All Sub (7) Catalan Number (7) Cycle (7) DP - Cases (7) Facebook Interview (7) Fibonacci Numbers (7) Flood fill (7) Game Nim (7) Graph BFS (7) HackerRank Difficult (7) Hackerearth (7) Inversion (7) Kadane’s Algorithm (7) Manacher (7) Morris Traversal (7) Multiple Data Structures (7) Normalized Key (7) O(XN) (7) Radix Sort (7) Recursion (7) Sampling (7) Suffix Array (7) Tech-Queries (7) Tree - Serialization (7) Tree DP (7) Trie - Bit (7) 蓝桥杯 (7) Algorithm - Brain Teaser (6) BFS - Priority Queue (6) BFS - Unusual (6) Classic Data Structure Impl (6) DP - 2D (6) DP - Monotone Queue (6) DP - Unusual (6) DP-Space Optimization (6) Dutch Flag (6) How To (6) Interviewstreet (6) Knapsack - MultiplePack (6) Local MinMax (6) MST (6) Minimum Spanning Tree (6) Number - Reach (6) Parentheses (6) Pre-Sum (6) Probability (6) Programming Pearls (6) Rabin-Karp (6) Reverse (6) Scan from right (6) Schedule (6) Stream (6) Subset Sum (6) TSP (6) Xpost (6) n00tc0d3r (6) reddit (6) AI (5) Abbreviation (5) Anagram (5) Art Of Programming-July (5) Assumption (5) Bellman Ford (5) Big Data (5) Code - Solid (5) Code Kata (5) Codility-lessons (5) Coding (5) Company - WMware (5) Convex Hull (5) Crazyforcode (5) DFS - Multiple (5) DFS+DP (5) DP - Multi-Dimension (5) DP-Multiple Relation (5) Eulerian Cycle (5) Graph - Unusual (5) Graph Cycle (5) Hash Strategy (5) Immutability (5) Java (5) LogN (5) Manhattan Distance (5) Matrix Chain Multiplication (5) N Queens (5) Pre-Sort: Index (5) Quick Partition (5) Quora (5) Randomized Algorithms (5) Resources (5) Robot (5) SPFA(Shortest Path Faster Algorithm) (5) Shuffle (5) Sieve of Eratosthenes (5) Strongly Connected Components (5) Subarray Sum (5) Sudoku (5) Suffix Tree (5) Swap (5) Threaded (5) Tree - Creation (5) Warshall Floyd (5) Word Search (5) jiuzhang (5)

Popular Posts