diff --git a/AdvancedLevel_C++/1017. Queueing at Bank (25) .cpp b/AdvancedLevel_C++/1017. Queueing at Bank (25) .cpp index 6e9359c..6add625 100644 --- a/AdvancedLevel_C++/1017. Queueing at Bank (25) .cpp +++ b/AdvancedLevel_C++/1017. Queueing at Bank (25) .cpp @@ -1,46 +1,36 @@ #include -#include +#include #include 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 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 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, greater > 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; } \ No newline at end of file