-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbank_queue.cpp
More file actions
80 lines (69 loc) · 1.45 KB
/
Copy pathbank_queue.cpp
File metadata and controls
80 lines (69 loc) · 1.45 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <queue>
using namespace std;
int main()
{
string line;
map <int, vector<int>> dict;
priority_queue<int> q;
int N, T;
//Read first line
getline(cin, line);
size_t pos = line.find(" ") + 1;
N = stoi(line.substr(0, pos - 1));
line.erase(0, pos);
T = stoi(line);
//Read in data, fill data structures appropriately
int i, deposit, priority;
int maxTime = 0;
for (i = 0; i < N; i++)
{
getline(cin, line);
size_t pos = line.find(" ") + 1;
deposit = stoi(line.substr(0, pos - 1));
line.erase(0, pos);
priority = stoi(line);
if (maxTime < priority)
maxTime = priority;
//Add map entry
if (dict.count(priority) == 0) //not found
{
vector<int> value;
value.push_back(deposit);
dict.insert(pair<int, vector<int>>(priority, value));
}
else //found
{
map<int, vector<int>>::iterator it = dict.find(priority);
it -> second.push_back(deposit);
}
}
int total = 0;
// Should have all input - run algorithm
for (i = maxTime; i >= 0; i--)
{
if (dict.count(i) == 0)
{
if (q.empty()) {continue;}
else
{
total += q.top();
q.pop();
}
}
else
{
vector<int> listAtPriority = dict.at(i);
for (int j = 0; j < listAtPriority.size(); j++)
{
q.push(listAtPriority[j]);
}
total += q.top();
q.pop();
}
}
cout << total << endl;
}