-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek1network.cpp
93 lines (77 loc) · 2.33 KB
/
week1network.cpp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <queue>
#include <vector>
struct Request {
Request(int arrival_time, int process_time):
arrival_time(arrival_time),
process_time(process_time)
{}
int arrival_time;
int process_time;
};
struct Response {
Response(bool dropped, int start_time):
dropped(dropped),
start_time(start_time)
{}
bool dropped;
int start_time;
};
class Buffer {
public:
Buffer(int size):
size_(size),
finish_time_()
{}
Response Process(const Request &request) {
// write your code here
while (!finish_time_.empty()) {
if (finish_time_.front() <= request.arrival_time)
finish_time_.pop();
else
break;
}
if (finish_time_.size() == size_)
return Response(true, -1);
if (finish_time_.empty()){
finish_time_.push(request.arrival_time + request.process_time);
return Response(false, request.arrival_time);
}
int last_element = finish_time_.back();
finish_time_.push(last_element + request.process_time);
return Response(false, last_element);
}
private:
int size_;
std::queue <int> finish_time_;
};
std::vector <Request> ReadRequests() {
std::vector <Request> requests;
int count;
std::cin >> count;
for (int i = 0; i < count; ++i) {
int arrival_time, process_time;
std::cin >> arrival_time >> process_time;
requests.push_back(Request(arrival_time, process_time));
}
return requests;
}
std::vector <Response> ProcessRequests(const std::vector <Request> &requests, Buffer *buffer) {
std::vector <Response> responses;
for (int i = 0; i < requests.size(); ++i)
responses.push_back(buffer->Process(requests[i]));
return responses;
}
void PrintResponses(const std::vector <Response> &responses) {
for (int i = 0; i < responses.size(); ++i)
std::cout << (responses[i].dropped ? -1 : responses[i].start_time) << std::endl;
}
int main() {
int size;
std::cin >> size;
std::vector <Request> requests = ReadRequests();
Buffer buffer(size);
std::vector <Response> responses = ProcessRequests(requests, &buffer);
PrintResponses(responses);
return 0;
}