# Highway Billboard Problem

Consider a highway of M miles. The task is to place billboards on the highway such that revenue is maximized. The possible sites for billboards are given by number x1 < x2 < ….. < xn-1 < xn, specifying positions in miles measured from one end of the road. If we place a billboard at position xi, we receive a revenue of ri > 0. There is a restriction that no two billboards can be placed within t miles or less than it.

Note : All possible sites from x1 to xn are in range from 0 to M as need to place billboards on a highway of M miles.

Examples:

```Input : M = 20
x[]       = {6, 7, 12, 13, 14}
revenue[] = {5, 6, 5,  3,  1}
t = 5
Output: 10
By placing two billboards at 6 miles and 12
miles will produce the maximum revenue of 10.

Input : M = 15
x[] = {6, 9, 12, 14}
revenue[] = {5, 6, 3, 7}
t = 2
Output : 18
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Let maxRev[i], 1 <= i <= M, be the maximum revenue generated from beginning to i miles on the highway. Now for each mile on the highway, we need to check whether this mile has the option for any billboard, if not then the maximum revenue generated till that mile would be same as maximum revenue generated till one mile before. But if that mile has the option for billboard then we have 2 options:
1. Either we will place the billboard, ignore the billboard in previous t miles, and add the revenue of the billboard placed.
2. Ignore this billboard. So maxRev[i] = max(maxRev[i-t-1] + revenue[i], maxRev[i-1])

Below is C++ implementation of this approach:

```// C++ program to find maximum revenue by placing
// billboard on the highway with given constarints.
#include<bits/stdc++.h>
using namespace std;

int maxRevenue(int m, int x[], int revenue[], int n,
int t)
{
// Array to store maximum revenue at each miles.
int maxRev[m+1];
memset(maxRev, 0, sizeof(maxRev));

// actual minimum distance between 2 billboards.
int nxtbb = 0;
for (int i = 1; i <= m; i++)
{
// check if all billboards are already placed.
if (nxtbb < n)
{
// check if we have billboard for that particular
// mile. If not, copy the previous maximum revenue.
if (x[nxtbb] != i)
maxRev[i] = maxRev[i-1];

// we do have billboard for this mile.
else
{
// We have 2 options, we either take current
// or we ignore current billboard.

// If current position is less than or equal to
// t, then we can have only one billboard.
if (i <= t)
maxRev[i] = max(maxRev[i-1], revenue[nxtbb]);

// Else we may have to remove previously placed
// billboard
else
maxRev[i] = max(maxRev[i-t-1]+revenue[nxtbb],
maxRev[i-1]);

nxtbb++;
}
}
else
maxRev[i] = maxRev[i - 1];
}

return maxRev[m];
}

// Driven Program
int main()
{
int m = 20;
int x[] = {6, 7, 12, 13, 14};
int revenue[] = {5, 6, 5, 3, 1};
int n = sizeof(x)/sizeof(x[0]);
int t = 5;
cout << maxRevenue(m, x, revenue, n, t) << endl;
return 0;
}
```

Output:

```10
```

Time Complexity: O(M), where M is distance of total Highway.
Auxiliary Space: O(M).

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
3.6 Average Difficulty : 3.6/5.0
Based on 16 vote(s)

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.