PAT 1014. Waiting in Line (30)
http://www.patest.cn/contests/pat-a-practise/1014
1 #include<cstdio> 2 #include<map> 3 #include<queue> 4 #include<cstring> 5 #include<cstdlib> 6 7 using namespace std; 8 9 const int MAXN = 1000 + 10; 10 int t[MAXN]; 11 struct Window { 12 int id; 13 int h; 14 int m; 15 queue<int> q; 16 17 Window():h(8), m(0) {} 18 19 bool operator < (const Window &other) const { 20 int m1 = m; 21 if(!q.empty()) m1 += t[q.front()]; 22 int m2 = other.m; 23 if(!other.q.empty()) m2 += t[other.q.front()]; 24 int h1 = h + m1 / 60; 25 int h2 = other.h + m2 / 60; 26 m1 %= 60; 27 m2 %= 60; 28 if(h1 != h2) return h1 > h2; 29 if(m1 != m2) return m1 > m2; 30 return id > other.id; 31 } 32 33 }window[30]; 34 35 int customer[MAXN]; 36 priority_queue<Window> pq; 37 int n, m, k, q; 38 map<int, pair<int, int> > ans; 39 40 int main() { 41 freopen("input", "r", stdin); 42 scanf("%d%d%d%d", &n, &m, &k, &q); 43 for(int i = 1; i <= k; ++i) { 44 scanf("%d", t + i); 45 } 46 for(int i = 0; i < q; ++i) { 47 scanf("%d", customer + i); 48 } 49 int cc = 1; 50 for(int i = 0; i < m; ++i) { 51 if(cc > k) break; 52 for(int j = 0; j < n; ++j) { 53 if(cc > k) break; 54 window[j].q.push(cc++); 55 } 56 } 57 for(int i = 0; i < n; ++i) {window[i].id = i; pq.push(window[i]);} 58 while(!pq.empty()) { 59 Window window = pq.top(); 60 pq.pop(); 61 if(window.q.empty() || window.h >= 17) { 62 continue; 63 } 64 int cus = window.q.front(); 65 window.q.pop(); 66 window.m += t[cus]; 67 window.h += window.m / 60; 68 window.m %= 60; 69 ans[cus] = make_pair(window.h, window.m); 70 if(cc <= k) { 71 window.q.push(cc++); 72 } 73 pq.push(window); 74 } 75 for(int i = 0; i < q; ++i) { 76 if(ans.count(customer[i])) { 77 pair<int, int> pr = ans[customer[i]]; 78 printf("%02d:%02d\n", pr.first, pr.second); 79 } else { 80 printf("Sorry\n"); 81 } 82 } 83 return 0; 84 }

浙公网安备 33010602011771号