cesium廣告牌_公路廣告牌

cesium廣告牌

Description:

描述:

This is a standard dynamic programing problem of finding maximum profits with some constraints. This can be featured in any interview coding rounds.

這是在某些約束條件下找到最大利潤的標準動態編程問題。 這可以在任何采訪編碼回合中體現。

Problem statement:

問題陳述:

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 < ... < x(n-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. The constraint is that no two billboards can be placed within t miles or less than it.

考慮一條M英里的高速公路。 任務是在高速公路上放置廣告牌,以使收入最大化。 廣告牌的可能位置由數字x 1 <x 2 <... <x (n-1) <x n給出,指定從道路的一端算起的英里位置。 如果將廣告牌放置在x i位置,我們的收入r i > 0 。 約束條件是在t英里以內或小于t英里內不能放置兩個廣告牌。

    Input:
M=15, n=5
x[n]= {6,8,12,14,15}
revenue[n] = {3,6,5,3,5}
t = 5
Output:
11

Explanation with example

舉例說明

So, we have to maximize the revenue by placing the billboards where the gap between any two billboard is t.

因此,我們必須通過將廣告牌放置在任意兩個廣告牌之間的距離為t的位置來最大化收入。

Here,

這里,

The corresponding distances of the billboards with respect to the origin are,

廣告牌相對于原點的相應距離為

    x[5]= {6,8,12,14,15}

We can augment the origin, so the augmented array becomes: (no need to augment if origin revenue exists)

我們可以擴充原點,因此擴充后的數組將變為:(如果存在原點收入,則無需擴充)


x[6]= {0,6,8,12,14,15}
t=5

The graphical interpretation of the billboards is like following:

廣告牌的圖形解釋如下:

Highway billboard


Figure 1: Billboards

圖1:廣告牌

The augmented revenue array:

擴充收益陣列:

    revenue[6] = {0,3,6,5,3,5}

Now, the brute force approach can be to check all the possible combination of billboards and updating the maximum revenue accumulated from each possible combinations.

現在,暴力破解方法可以是檢查廣告牌的所有可能組合,并更新每個可能組合所累積的最大收益。

Few of such possible combinations can be:

這樣的可能組合很少是:

Highway billboard

So, maximum revenue that can be collected is 11.

因此,可以收取的最大收入為11

For any billboard bi we have three choices

對于任何廣告牌b 都有三種選擇

  1. Don't pick bi

    不要選擇

  2. Start from bi

    開始

  3. Pick bi and other billboards accordingly

    相應地選擇b i和其他廣告牌

Say,

說,

    M(i)=maximum revenue upto first i billboards

So,

所以,

    M(0)=0
if bi is not picked, M(i)=M(i-1)
if billboard placement starts from bi M(i)=r[i]
If we place bi then need to pace bj where d[i]>d[j]-t such that revenue maximizes
So,

Highway billboard

Problem Solution approach

問題解決方法

We convert the above recursion to dynamic programing as there will many overlapping sub problems solving the recursion. (Try to generate the recursion tree yourself)

我們將上述遞歸轉換為動態編程,因為將會有許多重疊的子問題解決遞歸。 (嘗試自己生成遞歸樹)

    1) Construct DP[n] for n billboards
// considering billboard placing starts from it
2) Fill the array with base value which is r[i] 
3) for  i=1 to n-1
// recursion case 1 and 2
// either not picking ith billboard or starting 
// from ith billboard which ever is maximum
dp[i] = max(dp[i-1],dp[i]);
// recursion case 3
// picking ith billboard
for j=0 to i-1
if(a[j]< a[i]-k)//feasible placing
dp[i]= max(dp[i],dp[j]+r[i]);
end for
end for
Result is dp[n-1]

C++ implementation:

C ++實現:

#include <bits/stdc++.h>
using namespace std;
int max(int x, int y)
{
return (x > y) ? x : y;
}
int billboard(vector<int> a, vector<int> r, int n, int k)
{
int dp[n];
for (int i = 0; i < n; i++)
dp[i] = r[i];
//base value
dp[0] = r[0];
for (int i = 1; i < n; i++) {
//first two recursion case
int mxn = max(dp[i - 1], dp[i]);
//picking ith billboard, third recursion case
for (int j = 0; j < i; j++) {
if (a[j] < a[i] - k)
mxn = max(mxn, dp[j] + r[i]);
}
dp[i] = mxn;
}
return dp[n - 1];
}
int main()
{
int n, k, item, m;
cout << "Enter highway length:\n";
cin >> m;
cout << "Enter number of billboards\n";
cin >> n;
cout << "Enter minimum distance between any two billboards\n";
cin >> k;
vector<int> a;
vector<int> r;
cout << "Enter billboard distances one by one from origin\n";
for (int i = 0; i < n; i++) {
cin >> item;
a.push_back(item);
}
cout << "Enter revenues for the respective billboards\n";
for (int i = 0; i < n; i++) {
cin >> item;
r.push_back(item);
}
if (a[0] == 0) {
a.insert(a.begin(), 0);
r.insert(r.begin(), 0);
n = n + 1;
}
cout << "Maximum revenue that can be collected is: " << billboard(a, r, n, k) << endl;
return 0;
}

Output

輸出量

RUN 1:
Enter highway length:
20
Enter number of billboards
5
Enter minimum distance between any two billboards
5
Enter billboard distances one by one from origin
3 7 12 13 14
Enter revenues for the respective billboards
15 10 1 6 2
Maximum revenue that can be collected is: 21
RUN 2:
Enter highway length:
15
Enter number of billboards
5
Enter minimum distance between any two billboards
5
Enter billboard distances one by one from origin
6 8 12 14 15
Enter revenues for the respective billboards
3 6 5 3 5
Maximum revenue that can be collected is: 11

翻譯自: https://www.includehelp.com/icp/highway-billboard.aspx

cesium廣告牌

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540573.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540573.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540573.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

你和大牛差了啥

mmp。無時無刻不在想和大牛差在哪里了。別人為什么可以那么牛逼而你tmd那么菜&#xff01;整個人頓時都頹廢了。啥事兒不想干。后來想了想感情就是他比較黑吧。

[轉載] python數組的使用

參考鏈接&#xff1a; Python中整數的最大可能值是多少&#xff1f; 原文地址為&#xff1a; python數組的使用 python數組的使用 python數組的使用 2010-07-28 17:17 1、Python的數組分三種類型&#xff1a; (1) list 普通的鏈表&#xff0c;初始化后可以通過特定方法…

scala中循環守衛_Scala中的循環

scala中循環守衛Scala中的循環 (Loops in Scala) In programming, many times a condition comes when we need to execute the same statement or block of code more than one time. It could be difficult to write the same code multiple times, so programing language d…

50個必備基礎命令

1.tar創建一個新的tar文件$ tar cvf archive_name.tar dirname/解壓tar文件$ tar xvf archive_name.tar查看tar文件$ tar tvf archive_name.tar2. grep在文件中查找字符串(不區分大小寫)$ grep -i "the" demo_file輸出成功匹配的行&#xff0c;以及該行之后的三行$ g…

NM的完整形式是什么?

NM&#xff1a;無消息 (NM: No Message) NM is an abbreviation of "No Message". NM是“無消息”的縮寫。 It is an expression, which is commonly used in the Gmail platform. It is also written as N/M or n/m or *n/m*. It is written in the subject of the…

[轉載] python中全局變量和局部變量解析

參考鏈接&#xff1a; Python中的全局變量和局部變量 python函數中可以訪問全局變量但是不能給全局變量賦值&#xff0c;除非進行顯式聲明global a 比如定義了全局變量 a 在函數my_fun()中可以直接訪問a的值&#xff0c;而不需要global全局變量申明。下圖為上面代碼運行輸出 …

【iCore4 雙核心板_FPGA】例程十六:基于雙口RAM的ARM+FPGA數據存取實驗

實驗現象&#xff1a; 核心代碼&#xff1a; int main(void) {/* USER CODE BEGIN 1 */int i;int address,data;char error_flag 0;char receive_data[50];char buffer[8];char *p;/* USER CODE END 1 *//* MCU Configuration-----------------------------------------------…

[轉載] Python中TFTP的理解

參考鏈接&#xff1a; Python中的打包pack和拆包unpack參數 Num01–>TFTP協議介紹 TFTP&#xff08;Trivial File Transfer Protocol,簡單文件傳輸協議&#xff09; 是TCP/IP協議族中的一個用來在客戶端與服務器之間進行簡單文件傳輸的協議 特點&#xff1a; 1,簡單 2…

gn fast-gn_GN的完整形式是什么?

gn fast-gnGN&#xff1a;晚安 (GN: Good Night) GN is an abbreviation of "Good Night". GN是“ Good Night”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Facebook, Yahoo Messenge…

從零開始編寫自己的C#框架(27)——什么是開發框架

前言 做為一個程序員&#xff0c;在開發的過程中會發現&#xff0c;有框架同無框架&#xff0c;做起事來是完全不同的概念&#xff0c;關系到開發的效率、程序的健壯、性能、團隊協作、后續功能維護、擴展......等方方面面的事情。很多朋友在學習搭建自己的框架&#xff0c;很多…

[轉載] Python 遞歸 深入理解遞歸 Python遞歸剖析,絕對讓你看懂!

參考鏈接&#xff1a; Python | print()中的結束參數 目錄 遞歸剖析 遞歸的兩個過程 return 返回值 詳解 遞歸思路二分法和遞歸尾遞歸遞歸練習題 遞歸剖析 遞歸真的很重要&#xff0c;之前學的時候&#xff0c;學的一知半解&#xff0c;以為真正了解&#xff0c;每次想到遞歸…

laravel 項目遷移_在Laravel遷移

laravel 項目遷移Before moving forward we need to know some facts about it, 在繼續前進之前&#xff0c;我們需要了解一些事實&#xff0c; Resources: In these directories, we have already a js, lang, sass and view page. Where, sass and js file holf their uncom…

Python之list對應元素求和

本次分享將講述如何在Python中對多個list的對應元素求和&#xff0c;前提是每個list的長度一樣。比如&#xff1a;a[1,2,3], b[2,3,4], c[3,4,5], 對a,b,c的對應元素求和&#xff0c;輸出應為[6,9,12].    方法一&#xff1a;   直接求解&#xff0c;按照對應元素相加的…

[轉載] Python中str跟int的轉換

參考鏈接&#xff1a; Python中的類型轉換 字符串str轉換成int: int_value int(str_value) int轉換成字符串str: str_value str(int_value) a100 b666 #int轉str類型 print(int轉str類型) print(int轉str&#xff1a; str(a)) #str轉int類型 print(str轉int類型…

ot協議是什么_OT的完整形式是什么?

ot協議是什么OT&#xff1a;主題外 (OT: Off Topic) OT is an abbreviation of "Off Topic". OT是“ Off Topic”的縮寫 。 It is an expression, which is commonly used in Gmail or messaging platform. It shows that the email that has been sent is irrelev…

[轉載] python中字符串編碼形式及其所占字節

參考鏈接&#xff1a; Python中的字節對象與字符串 1.常見字符串編碼錯誤 在使用Python讀文件時經常遇到編碼問題引起的錯誤&#xff0c;比如&#xff1a; UnicodeDecodeError: gbk codec cant decode byte 0x80 in position 30: illegal multibyte sequence 遇到這種異…

[AtCoder-ARC073F]Many Moves

題目大意&#xff1a;   有一排n個格子和2枚硬幣。   現在有q次任務&#xff0c;每一次要你把其中一枚硬幣移到x的位置上&#xff0c;移動1格的代價是1。   兩枚硬幣不能同時移動&#xff0c;任務必須按次序完成。   現在告訴你兩枚硬幣初始狀態所在的位置a和b&#xf…

ScalavsKotlin

Is Scala better that Kotlin? No..., Is Kotlin better than Scala? No... Scala比Kotlin更好嗎&#xff1f; 不...&#xff0c;Kotlin勝過Scala嗎&#xff1f; 沒有... Both programming languages have their own profits and are for a specific set of development. It…

工業智能相機與基于PC的機器視覺的區別比較

隨著科技的日漸成熟&#xff0c;機器視覺得到了飛速發展。由于嵌入式技術的發展,近幾年智能相機性能顯著提高&#xff0c;越來越多必須依賴于PC處理的應用開始向智能相機平臺傾斜。低成本、高可靠性及易于安裝維護等優勢&#xff0c;使得機器視覺在制造業上的規模性應用越來越普…

[轉載] python skimage在圖像處理中的用法

參考鏈接&#xff1a; 在Python中打印單變量和多變量 基于python腳本語言開發的數字圖片處理包&#xff0c;比如PIL,Pillow, opencv, scikit-image等。 PIL和Pillow只提供最基礎的數字圖像處理&#xff0c;功能有限&#xff1b;opencv實際上是一個c庫&#xff0c;只是提供了py…