-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVa-01203.cpp
More file actions
65 lines (52 loc) · 1.98 KB
/
UVa-01203.cpp
File metadata and controls
65 lines (52 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h> // here we have all the STL we need, including istringstream and ostringstream
#define ALL(x) x.begin(), x.end()
#define FAST std::cin.tie(0); ios::sync_with_stdio(false); std::cout.tie(0);
using namespace std;
#define min(a,b) (a < b) ? (a) : (b)
#define max(a,b) (a > b) ? (a) : (b)
#define vii vector<pair<int,int>>
#define ii pair<int, int>
int main() {
string trash;
int id, val;
string s;
priority_queue<ii> pq;
unordered_map<int, int> periods;
while (getline(cin, s) , s[0] != '#') {
istringstream iss (s);
iss >> trash >> id >> val;
periods[id] = val;
ii p = make_pair(-val, id); // pushing the negative value to make it act as a min_heap.
pq.emplace(p);
}
int k;
cin >> k;
int temp;
vector<int> equals; // if more than one process has the same value, we sort them by id and get what we want.
while (true) {
temp = pq.top().first; // (period initially), we add period to it each time we pop it, and then push it back.
//cout << "First " << temp << endl;
id = pq.top().second; // the id, doesn't change.
//cout << "Second " << val << endl;
equals.emplace_back(id); // put the id in there.
pq.pop();
while ( !pq.empty() && pq.top().first == temp ) {
equals.push_back(pq.top().second);
pq.pop();
}
sort( ALL( equals ) ); // more than one id has same val, we sort them and print as much as we want, decreasing k.
int y = min(equals.size(), k); // to loop that much, we don't want to go out of bounds of the vector.
for (int i = 0; i < y; i++) {
k--;
cout << equals[i] << endl;
}
if (k == 0) {
equals.clear();
break;
}
for (int i = 0; i < equals.size(); i++) {
pq.push(make_pair(temp-periods[equals[i]], equals[i]));
}
equals.clear();
}
}