-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCF_1206_B.cpp
More file actions
49 lines (38 loc) · 1.23 KB
/
CF_1206_B.cpp
File metadata and controls
49 lines (38 loc) · 1.23 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
#include <iostream>
#include <vector>
using namespace std;
int udp_network_protocol(vector<vector<int>> requests, int max_packets, int rate) {
int dropped_packets = 0;
vector<int> packets_in_queue;
int front_ptr = 0;
int back_ptr = 0;
for (int i = 0; i < requests.size(); i++) {
// Add the new packets to the queue.
int num_packets = requests[i][1];
while (packets_in_queue.size() < max_packets && num_packets > 0) {
packets_in_queue.push_back(1);
back_ptr++;
num_packets--;
}
// Remove the oldest packets from the queue.
while (packets_in_queue.size() > max_packets) {
front_ptr++;
}
// Deliver the packets to the client.
for (int j = 0; j < rate && front_ptr < back_ptr; j++) {
front_ptr++;
}
// Count the number of dropped packets.
dropped_packets = packets_in_queue.size() - (back_ptr - front_ptr);
}
// Return the number of dropped packets.
return dropped_packets;
}
int main() {
vector<vector<int>> requests = {{1, 8}, {4, 9}, {6, 7}};
int max_packets = 10;
int rate = 2;
int dropped_packets = udp_network_protocol(requests, max_packets, rate);
cout << "The total number of dropped packets is: " << dropped_packets << endl;
return 0;
}