-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmaximum_advertisement_revenue.cpp
More file actions
31 lines (24 loc) · 1.12 KB
/
maximum_advertisement_revenue.cpp
File metadata and controls
31 lines (24 loc) · 1.12 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
/**
* Given two sequences a_1, a_2, ..., a_n (a_i is the profit per click of
* the i-th ad) and b_1, b_2, ..., b_n (b_i is the average number of clicks
* per day of the i-th slot), we need to partition them into n pairs (a_i, b_j)
* such that the sum of their products is maximized.
*/
#include "maximum_advertisement_revenue.hpp"
#include <algorithm>
#include <stdexcept>
using namespace std;
long long maximum_advertisement_revenue(vector<int> const& profit_per_click, vector<int> const& average_clicks) {
if (profit_per_click.size() != average_clicks.size()) {
throw invalid_argument("lists are not of equal size");
}
vector<int> profit_per_click_sorted = profit_per_click;
sort(profit_per_click_sorted.begin(), profit_per_click_sorted.end());
vector<int> average_clicks_sorted = average_clicks;
sort(average_clicks_sorted.begin(), average_clicks_sorted.end());
long long revenue = 0;
for (size_t index = 0; index < profit_per_click_sorted.size(); ++index) {
revenue += (long long)profit_per_click_sorted[index] * (long long)average_clicks_sorted[index];
}
return revenue;
}