Skip to content

Commit

Permalink
Update 1017. Queueing at Bank (25) .cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchuo committed Mar 7, 2020
1 parent 477743e commit 1be430b
Showing 1 changed file with 24 additions and 34 deletions.
58 changes: 24 additions & 34 deletions AdvancedLevel_C++/1017. Queueing at Bank (25) .cpp
Original file line number Diff line number Diff line change
@@ -1,46 +1,36 @@
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
struct node {
const int maxn = 10005;
struct person {
int come, time;
} tempcustomer;
bool cmp1(node a, node b) {
return a.come < b.come;
}
} p[maxn];
int cmp(person p1, person p2) { return p1.come < p2.come; }
int n, k, cnt, total;
int main() {
int n, k;
scanf("%d%d", &n, &k);
vector<node> custom;
for(int i = 0; i < n; i++) {
int hh, mm, ss, time;
scanf("%d:%d:%d %d", &hh, &mm, &ss, &time);
int cometime = hh * 3600 + mm * 60 + ss;
if(cometime > 61200) continue;
tempcustomer = {cometime, time * 60};
custom.push_back(tempcustomer);
for (int i = 1; i <= n; i++) {
int hh, ss, mm, tt;
scanf("%d:%d:%d %d", &hh, &mm, &ss, &tt);
int sum = hh * 3600 + mm * 60 + ss;
if (sum > 61200) continue;
p[++cnt].time = tt * 60;
p[cnt].come = sum;
}
sort(custom.begin(), custom.end(), cmp1);
vector<int> window(k, 28800);
double result = 0.0;
for(int i = 0; i < custom.size(); i++) {
int tempindex = 0, minfinish = window[0];
for(int j = 1; j < k; j++) {
if(minfinish > window[j]) {
minfinish = window[j];
tempindex = j;
}
}
if(window[tempindex] <= custom[i].come) {
window[tempindex] = custom[i].come + custom[i].time;
sort(p + 1, p + 1 + cnt, cmp);
priority_queue<int, vector<int>, greater<int> > q;
for (int i = 1; i <= k; ++i) q.push(28800);
for (int i = 1; i <= cnt; ++i) {
if (q.top() <= p[i].come) {
q.push(p[i].come + p[i].time);
q.pop();
} else {
result += (window[tempindex] - custom[i].come);
window[tempindex] += custom[i].time;
total += q.top() - p[i].come;
q.push(q.top() + p[i].time);
q.pop();
}
}
if(custom.size() == 0)
printf("0.0");
else
printf("%.1f", result / 60.0 / custom.size());
(!cnt) ? printf("0.0\n") : printf("%.1lf", ((double)total/60.0)/(double) cnt);
return 0;
}

0 comments on commit 1be430b

Please sign in to comment.