Untitled
unknown
c_cpp
2 years ago
1.4 kB
5
Indexable
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int max_requests_1(int total_bandwidth, const vector<int>& bandwidth, const vector<int>& request) {
int n = bandwidth.size();
// Create a vector of pairs (bandwidth, request)
vector<pair<int, int>> endpoints;
for (int i = 0; i < n; ++i) {
endpoints.push_back(make_pair(bandwidth[i], request[i]));
}
// Sort endpoints by the request in descending order
sort(endpoints.begin(), endpoints.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
return a.second > b.second;
});
int max_requests_served = 0;
for (int i = 0; i < n; ++i) {
int bw = endpoints[i].first;
int req = endpoints[i].second;
int bandwidth_allocated = min(bw, total_bandwidth);
if (bw <= total_bandwidth) {
int requests_served = req;
max_requests_served += requests_served;
total_bandwidth -= bandwidth_allocated;
}
}
return max_requests_served;
}
int main() {
int total_bandwidth = 500;
vector<int> bandwidth = {200, 100, 350, 50, 100};
vector<int> request = {270, 142, 450, 124, 189};
int max_requests_value = max_requests_1(total_bandwidth, bandwidth, request);
cout << "Total number of requests that can be served: " << max_requests_value << endl;
return 0;
}
Editor is loading...