自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

乒乒乓乓:這點小事兒,算什么?

開發(fā) 前端
對于任意一對球員,當他們到達時如果有多個球臺可用,那么他們就會被安排到編號較小的那個球臺上打球。

[[418167]]

本文轉(zhuǎn)載自微信公眾號「Piper蛋窩」,作者Piper蛋。轉(zhuǎn)載本文請聯(lián)系Piper蛋窩公眾號。

一道 PAT 原題,被稱為「PAT史上最麻煩題目」:

  • PAT 原題英文鏈接:https://pintia.cn/problem-sets/994805342720868352/problems/994805472333250560
  • AcWing 翻譯:https://www.acwing.com/problem/content/1505/

原題

一個乒乓球俱樂部共有K張乒乓球臺,編號為1~K 。

對于任意一對球員,當他們到達時如果有多個球臺可用,那么他們就會被安排到編號較小的那個球臺上打球。

如果所有球臺都被占用了,他們就只能排隊等待了。

假設每對球員最多只允許打兩小時球。

你需要計算每個排隊等候的人的等待時間以及每個球臺當天有多少對球員在上面打球。

另外,讓這個事情變得復雜的是,俱樂部為 VIP 球員保留了一些球臺。

當一個 VIP 球臺空出時,等待隊伍中的第一對 VIP 球員將優(yōu)先使用這個球臺。

如果此時等待隊伍中沒有 VIP,則排在等待隊伍的第一對球員可以使用這個球臺。

另一方面,當輪到一對 VIP 球員打球時,如果沒有 VIP 球臺可用,那么他們將被視作普通球員處理。

補充:

1、當?shù)却犖橹杏?VIP 球員并且有空閑 VIP 球臺時,必須優(yōu)先分配 VIP 球員,并且必須分配他們 VIP 球臺(優(yōu)先分配編號較小的),直至 VIP 用戶或 VIP 球臺分配完為止。

2、期望打球時間超過兩小時的,只能允許打兩小時。

3、當多對球員的開始打球時間相同時,先輸出到達時間早的球員的信息。

4、當?shù)却騿T中沒有 VIP 時,VIP 球臺視作普通球臺處理,當可用球臺中沒有 VIP 球臺時,VIP 球員視作普通球員處理。

輸入格式

第一行包含整數(shù)N,表示共有N對球員。

接下來N行,每行包含兩個時間以及一個 VIP 標識,HH:MM:SS----到達時間,p----打球時間(單位:分鐘),tag----如果是 ,說明這是一對 VIP,如果是 ,說明不是 VIP。

保證到達時間在 08:00:00 至 21:00:00 之間,這是俱樂部的營業(yè)時間。

保證每對球員的到達時間都不相同。

再一行包含兩個整數(shù)K和M,表示球臺數(shù)量以及 VIP 球臺數(shù)量。

最后一行包含M個整數(shù),表示 VIP 球臺的編號。

輸出格式

首先輸出每對球員的到達時間,開始打球時間,等待時間。

每對球員的信息占一行,按開始打球時間從早到晚的順序依次輸出。

等待時間必須四舍五入為整數(shù)分鐘。

如果一對球員在 21:00:00 之前(不包括 21:00:00)不能得到一張球臺,那么無需輸出他們的信息。

再輸出一行,K個整數(shù),表示每個球臺服務的球員對數(shù)。

數(shù)據(jù)范圍

輸入樣例:

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

輸出樣例:

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

1026 Table Tennis (30 point(s))

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 privilege 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.

思路:

  • 把 Player 和 Table 分別放到四個優(yōu)先隊列里面
    • VIP Player
    • VIP Table
    • normal Player
    • normal Table
  • 把所有 Player 放入相應的優(yōu)先隊列,被分配了就把 Player 彈出來,被分配到的 Table 也彈出來,更新結(jié)束時間,再放入 Table 對應的隊列
  • 關鍵時間點全靠堆頂輸出
  • 因此退出循環(huán)的依據(jù)有兩個:Player 空了,或者沒有 Table 在 21:00 前結(jié)束
  • 桌子分配原則:
    • 如果輪到 VIP Player 并且 VIP Table 有空缺,則分配;
    • 如果輪到 normal Player 并且只有 VIP Table 有空缺,分配;
    • 如果輪到 VIP Player 并且只有 VIP Table 有空缺,分配;
    • 其他情況分配 normal Player

注意事項:

  • 往各個優(yōu)先隊列輸入 INF 的時間原始,且保證其永遠不會到達堆頂,這樣不用判斷是不是空節(jié)點
  • 每次四個優(yōu)先隊列都出隊堆頂,方便書寫
  • 對于每個玩家關鍵時間點 cur_time ,如果有桌子的 end_time 比這個小,則更新 end_time 為這個 cur_time ,防止后面漏判空余桌子
  1. #include <iostream> 
  2. #include <cstring> 
  3. #include <algorithm> 
  4. #include <queue> 
  5. #include <vector> 
  6. #include <cmath> 
  7.  
  8. using namespace std; 
  9.  
  10. const int N = 1e4 + 10, M = 1e2 + 10, INF = 2e9; 
  11. int n, k, m; 
  12.  
  13. struct Player 
  14.     int arrive_time, serve_time; 
  15.     int start_time, waiting_time; 
  16.      
  17.     // 給 sort 排 
  18.     const bool operator< (const Player& t) const 
  19.     { 
  20.         if (start_time != t.start_time) return start_time < t.start_time; 
  21.         return arrive_time < t.arrive_time; 
  22.     } 
  23.      
  24.     // 給 priority_queue 排 
  25.     const bool operator> (const Player& t) const 
  26.     { 
  27.         return arrive_time > t.arrive_time; 
  28.     } 
  29. }; 
  30.  
  31. struct Table 
  32.     int id; 
  33.     int end_time; 
  34.      
  35.     const bool operator> (const Table& t) const 
  36.     { 
  37.         if (end_time != t.end_time) return end_time > t.end_time; 
  38.         return id > t.id; 
  39.     } 
  40. }; 
  41.  
  42. bool is_vip_table[M]; 
  43. int table_count[M]; 
  44.  
  45. // 最終輸出的玩家及順序 
  46. vector<Player> players; 
  47.  
  48. // 注意 `&` 很重要 
  49. void assign(priority_queue<Player, vector<Player>, greater<Player>>& ps, 
  50.             priority_queue<Table, vector<Table>, greater<Table>>& ts) 
  51.     auto p = ps.top(); 
  52.     ps.pop(); 
  53.     auto t = ts.top(); 
  54.     ts.pop(); 
  55.     int start_time = t.end_time; 
  56.     int end_time = start_time + p.serve_time; 
  57.     ts.push({t.id, end_time}); 
  58.     table_count[t.id] ++; 
  59.     p.start_time = start_time; 
  60.     p.waiting_time = round((start_time - p.arrive_time) / 60.0); 
  61.     players.push_back(p); 
  62.  
  63. string time_to_string(int sec) 
  64.     char str[20]; 
  65.     sprintf(str, "%02d:%02d:%02d", sec / 3600, sec % 3600 / 60, sec % 60); 
  66.     return str; 
  67.  
  68. int main() 
  69.     // 輸入玩家 
  70.     priority_queue<Player, vector<Player>, greater<Player>> normal_players; 
  71.     priority_queue<Player, vector<Player>, greater<Player>> vip_players; 
  72.      
  73.     normal_players.push({INF}); 
  74.     vip_players.push({INF}); 
  75.  
  76.     cin >> n; 
  77.     for (int i = 0; i < n; i ++ ) 
  78.     { 
  79.         int hourminute, sec; 
  80.         int serve_time, is_vip; 
  81.         scanf("%d:%d:%d %d %d", &hour, &minute, &sec, &serve_time, &is_vip); 
  82.         sec = hour * 3600 + minute * 60 + sec; 
  83.         serve_time = min(serve_time * 60, 7200); 
  84.         if (is_vip) vip_players.push({sec, serve_time}); 
  85.         else normal_players.push({sec, serve_time}); 
  86.     } 
  87.      
  88.     // 輸入桌子 
  89.     priority_queue<Table, vector<Table>, greater<Table>> normal_tables; 
  90.     priority_queue<Table, vector<Table>, greater<Table>> vip_tables; 
  91.      
  92.     normal_tables.push({-1, INF}); 
  93.     vip_tables.push({-1, INF}); 
  94.      
  95.     cin >> k >> m; 
  96.     for (int i = 0; i < m; ++ i) 
  97.     { 
  98.         int id; 
  99.         cin >> id; 
  100.         is_vip_table[id] = true
  101.     } 
  102.      
  103.     for (int i = 1; i <= k; i ++ ) 
  104.     { 
  105.         if (is_vip_table[i]) vip_tables.push({i, 8 * 3600}); 
  106.         else normal_tables.push({i, 8 * 3600}); 
  107.     } 
  108.      
  109.     // 開始分配 
  110.     while (normal_players.size() > 1 || vip_players.size() > 1) 
  111.     { 
  112.         auto np = normal_players.top(); 
  113.         auto vp = vip_players.top(); 
  114.          
  115.         int cur_time = min(np.arrive_time, vp.arrive_time); 
  116.          
  117.         while (normal_tables.top().end_time < cur_time) 
  118.         { 
  119.             auto t = normal_tables.top(); 
  120.             normal_tables.pop(); 
  121.             t.end_time = cur_time; 
  122.             normal_tables.push(t); 
  123.         } 
  124.          
  125.         while (vip_tables.top().end_time < cur_time) 
  126.         { 
  127.             auto t = vip_tables.top(); 
  128.             vip_tables.pop(); 
  129.             t.end_time = cur_time; 
  130.             vip_tables.push(t); 
  131.         } 
  132.          
  133.         auto nt = normal_tables.top(); 
  134.         auto vt = vip_tables.top(); 
  135.         int end_time = min(nt.end_time, vt.end_time); 
  136.  
  137.         if (end_time >= 21 * 3600) break; 
  138.  
  139.         if (vp.arrive_time <= end_time && vt.end_time == end_time) assign(vip_players, vip_tables); 
  140.         else if (np.arrive_time < vp.arrive_time) 
  141.         { 
  142.             if (nt > vt) assign(normal_players, vip_tables); 
  143.             else assign(normal_players, normal_tables); 
  144.         } 
  145.         else 
  146.         { 
  147.             if (nt > vt) assign(vip_players, vip_tables); 
  148.             else assign(vip_players, normal_tables); 
  149.         } 
  150.     } 
  151.      
  152.     sort(players.begin(), players.end()); 
  153.      
  154.     for (auto p: players) 
  155.     { 
  156.         cout << time_to_string(p.arrive_time) << " " << time_to_string(p.start_time) << " " << p.waiting_time << endl; 
  157.     } 
  158.  
  159.     for (int i = 1; i <= k; i ++ ) 
  160.     { 
  161.         cout << table_count[i]; 
  162.         if (i + 1 <= k) cout << " "
  163.         else cout << endl; 
  164.     } 

 

 

責任編輯:武曉燕 來源: Piper蛋窩
相關推薦

2018-10-29 16:15:09

MySQL數(shù)據(jù)庫緩存

2018-10-30 15:40:15

MySQL緩存Tomcat

2010-07-27 16:21:25

程序員

2009-12-21 17:00:22

Linux操作系統(tǒng)

2022-12-28 10:13:40

云計算云原生

2017-12-13 15:30:55

2013-06-04 09:49:48

游戲設計

2017-08-10 16:36:43

Android導航標簽

2019-03-25 21:11:35

華為

2023-02-28 08:29:01

MySQL主鍵索引

2013-04-17 10:12:43

數(shù)據(jù)分析大數(shù)據(jù)

2013-06-03 09:16:26

云計算

2021-03-13 11:23:51

源碼邏輯框架

2015-07-22 11:35:55

MongoDB數(shù)據(jù)暴露數(shù)據(jù)庫安全

2022-09-09 19:01:02

接口Reader?Spark

2014-08-08 15:34:53

安全漏洞漏洞防護安全防守

2011-06-30 11:23:32

Python

2023-05-06 10:28:14

云計算邊緣計算

2015-09-09 14:47:39

魅族

2015-03-25 10:56:10

點贊
收藏

51CTO技術棧公眾號