漢子編碼比字母編碼長_字母/博客作者編碼問題(使用動態編程)

漢子編碼比字母編碼長

Problem statement:

問題陳述:

Shivang is a blog writer and he is working on two websites simultaneously. He has to write two types of blogs which are:

Shivang是一位博客作家,他同時在兩個網站上工作。 他必須寫兩種類型的博客:

  • Technical blogs for website_1: X of this type can be written in an hour. (X will be the input)

    website_1的技術博客 :此類X可以在一小時內編寫。 ( X將作為輸入)

  • Fun blogs for website_2: Y of this type can be written in an hour. (Y will be input)

    網站_2的趣味博客 :可以在一個小時內編寫此類Y。 (將輸入Y )

You are to help him to save time. Given N number of total blogs, print the minimum number of hours he needs to put for combination of both the blogs, so that no time is wasted.

您是要幫助他節省時間。 給定總數為N的博客,請打印他將兩個博客組合在一起所需最少小時數 ,以免浪費時間。

    Input:
N: number of total blogs
X: number of Technical blogs for website_1 per hour
Y: number of Fun blogs for website_2 per hour
Output:
Print the minimum number of hours Shivang needs to write the 
combination of both the blogs (total N). 
If it is NOT possible, print "?1". 

Example:

例:

    Input:
N: 33
X: 12
Y: 10
Output:
-1 
Input:
N: 36
X: 10
Y: 2
Output:
6 (10*3+2*3)

Explanation:

說明:

    For the first case,
No combination is possible that's why output is -1.
For the second test case,
Possible combinations are 30 technical blogs (3 hours) + 6 fun blogs (3 hours)
20 technical blogs (2 hours) + 16 fun blogs (8 hours)
10 technical blogs (1 hours) + 26 fun blogs (13 hours)
0 technical blogs (0 hours) + 36 fun blogs (18 hours)
So, best combination is the first one which takes total 6 hours

The problem is basically solving equation,

問題基本上是解決方程式,

aX + bY = N where we need to find the valid integer coefficients of X and Y. Return a+b if there exists such else return -1.

aX + bY = N ,我們需要找到XY的有效整數系數。 如果存在則返回a + b ,否則返回-1

We can find a recursive function for the same too,

我們也可以找到相同的遞歸函數,

Say,

說,

    f(n) = minimum hours for n problems
f(n) = min(f(n-x) + f(n-y)) if f(n-x), f(n-y) is solved already

We can convert the above recursion to DP.

我們可以將上述遞歸轉換為DP。

Solution Approach:

解決方法:

Converting the recursion into DP:

將遞歸轉換為DP:

    1)  Create DP[n] to store sub problem results
2)  Initiate the DP with -1 except DP[0], DP[0]=0
3)  Now in this step we would compute values for DP[i]
4)  for i=1 to n
if i-x>=0 && we already have solution for i-x,i.e.,DP[i-x]!=-1 
DP[i]=DP[i-x]+1;
end if
if i-y>=0 && we already have solution for i-y,i.e.,DP[i-y]!=-1)
if DP[i]!=-1
DP[i]=min(DP[i],DP[i-y]+1);
else
DP[i]=DP[i-y]+1;
End if
End if
End for
5)  Return DP[n]

C++ Implementation:

C ++實現:

#include <bits/stdc++.h>
using namespace std;
int minimumHour(int n, int x, int y)
{
int a[n + 1];
a[0] = 0;
for (int i = 1; i <= n; i++)
a[i] = -1;
for (int i = 1; i <= n; i++) {
if (i - x >= 0 && a[i - x] != -1) {
a[i] = a[i - x] + 1;
}
if (i - y >= 0 && a[i - y] != -1) {
if (a[i] != -1)
a[i] = min(a[i], a[i - y] + 1);
else
a[i] = a[i - y] + 1;
}
}
return a[n];
}
int main()
{
int n, x, y;
cout << "Enter total number of blogs, N:\n";
cin >> n;
cout << "Enter number of techical blogs, X:\n";
cin >> x;
cout << "Enter number of fun blogs, Y:\n";
cin >> y;
cout << "Minimum hour to be dedicated: " << minimumHour(n, x, y) << endl;
return 0;
}

Output

輸出量

RUN 1:
Enter total number of blogs, N:
36
Enter number of techical blogs, X:
10
Enter number of fun blogs, Y:
2
Minimum hour to be dedicated: 6
RUN 2:
Enter total number of blogs, N:
33
Enter number of techical blogs, X:
12
Enter number of fun blogs, Y:
10
Minimum hour to be dedicated: -1

翻譯自: https://www.includehelp.com/icp/letter-blog-writer-coding-problem-using-dynamic-programming.aspx

漢子編碼比字母編碼長

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

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

相關文章

php parent報錯,mac brew 安裝php擴展報錯:parent directory is world writable but not sticky

$ brew install php70-mcrypt報錯&#xff1a;Error: parent directory is world writable but not sticky搜索到github的答案https://github.com/Homebrew/legacy-homebrew/issues/40345原因&#xff1a;/tmp目錄權限不對$ ls -ld /private/tmp打印出來 /private/tmp 被標黃了…

在cordova中使用HTML5的多文件上傳

2019獨角獸企業重金招聘Python工程師標準>>> 我們先看看linkface給開放的接口&#xff1a; 字段類型必需描述api_idstring是API 賬戶api_secretstring是API 密鑰selfie_filefile見下方注釋需上傳的圖片文件 1&#xff0c;上傳本地圖片進行檢測時選取此參數selfie_ur…

python dataframe切片_python pandas dataframe 行列選擇,切片操作方法

SQL中的select是根據列的名稱來選取&#xff1b;Pandas則更為靈活&#xff0c;不但可根據列名稱選取&#xff0c;還可以根據列所在的position&#xff08;數字&#xff0c;在第幾行第幾列&#xff0c;注意pandas行列的position是從0開始&#xff09;選取。相關函數如下&#xf…

php根據設備判斷訪問,PHP判斷設備訪問來源

/*** 判斷用戶請求設備是否是移動設備* return bool*/function isMobile() {//如果有HTTP_X_WAP_PROFILE則一定是移動設備if (isset($_SERVER[HTTP_X_WAP_PROFILE])) {return true;}//如果via信息含有wap則一定是移動設備,部分服務商會屏蔽該信息if (isset($_SERVER[HTTP_VIA])…

機器學習 深度學習 ai_如何學習機器學習和人工智能?

機器學習 深度學習 aiSTRATEGY 戰略 Learn theory practical aspects. 學習理論和實踐方面的知識。 (At first get an overview of what you are going to learn). (首先獲得要學習的內容的概述)。 Gain a good hold/insight on each concept. 掌握/理解每個概念。 If you …

linux常用命令和配置

2019獨角獸企業重金招聘Python工程師標準>>> 啟動php&#xff1a; /etc/init.d/php-fpm restart 查看PHP運行目錄&#xff1a; which php /usr/bin/php 查看php-fpm進程數&#xff1a; ps aux | grep -c php-fpm 查看運行內存 /usr/bin/php -i|grep mem iptables如…

centos7時間同步_centos 8.x系統配置chrony時間同步服務

centos 8.x系統配置chrony時間同步服務CentOS 7.x默認使用的時間同步服務為ntp服務&#xff0c;但是CentOS 8開始在官方的倉庫中移除了ntp軟件&#xff0c;換成默認的chrony進行時間同步的服務&#xff0c;chrony既可以作為客戶端向其他時間服務器發送時間同步請求&#xff0c;…

php可以用scanf,C/C++中 使用scanf和printf如何讀入輸出double型數據。

黃舟2017-04-17 13:47:232樓注意scanf函數和printf函數是不同尋常的函數&#xff0c;因為它們都沒有將函數的參數限制為固定數量。scanf函數和printf函數又可變長度的參數列表。當調用帶可變長度參數列表的函數時&#xff0c;編譯器會安排float參數自動轉換成為double類型&…

ICWAI和ICWA的完整形式是什么?

ICWAI / ICWA&#xff1a;印度成本與工程會計師協會/印度兒童福利法 (ICWAI / ICWA: Institute of Cost and Works Accountants of India / Indian Child Welfare Act) 1)ICWAI&#xff1a;印度成本與工程會計師協會 (1) ICWAI: Institute of Cost and Works Accountants of In…

深入研究java.lang.Runtime類【轉】

轉自&#xff1a;http://blog.csdn.net/lastsweetop/article/details/3961911 目錄(?)[-] javalang 類 RuntimegetRuntimeexitaddShutdownHookremoveShutdownHookhaltrunFinalizersOnExitexecexecexecexecexecexecavailableProcessorsfreeMemorytotalMemorymaxMemorygcrunFina…

java隊列實現限流,java中應對高并發的兩種策略

目的&#xff1a;提高可用性通過ExecutorService實現隊列泄洪//含有20個線程的線程池private ExecutorService executorService Executors.newFixedThreadPool(20);將有并發壓力的下游代碼放入到線程池的submit方法中&#xff0c;如下&#xff1a;//同步調用線程池的submit方法…

crontab 日志_liunx 中定時清理過期日志文件

問題描述經常遇到日志文件過多&#xff0c;占用大量磁盤空間&#xff0c;需要定期刪除過期日志。問題涉及方面刪除過期日志的腳本。定時任務刪除任務腳本先查詢到過期的日志文件&#xff0c;然后刪除。語法find path -option [ -print ] [ -exec -ok command ] …

JavaScript | 數組的常用屬性和方法

JavaScript的通用屬性和數組方法 (Common properties and methods of array in JavaScript ) Properties/MethodsDescriptionsarray.lengthReturns the length of the array/total number of elements of the array array[index]Returns the item name stored at “index” pos…

php dbutils 使用,dbutilsapi

相對lisp?而?言,可以使?用.net的強?大api,實現各種酷炫功能。相對c#及arx?...文件utils.py的模塊名分別是mycompany.utils和 mycompany.web.utils。 mycompany......CouchDB -b 關閉后臺運行的 CouchDB : CouchDB -d web 訪問:http://127.0.0.1:5984/_utils/index.html E-…

html 導航欄

<!DOCTYPE html> <html> <head> <meta charset"utf-8"> <title>lvnian學習(http://lvnian.blog.51cto.com/)</title> <style> ul {list-style-type:none;margin:0;padding:0; }a:link,a:visited{display:block;font-weigh…

將搜索二叉樹轉換為鏈表_將給定的二叉樹轉換為雙鏈表(DLL)

將搜索二叉樹轉換為鏈表Given a Binary tree and we have to convert it to a Doubly Linked List (DLL). 給定二叉樹&#xff0c;我們必須將其轉換為雙鏈表(DLL)。 Algorithm: 算法&#xff1a; To solve the problem we can follow this algorithm: 為了解決這個問題&#…

cuda編程_CUDA刷新器:CUDA編程模型

CUDA刷新器&#xff1a;CUDA編程模型 CUDA Refresher: The CUDA Programming Model CUDA&#xff0c;CUDA刷新器&#xff0c;并行編程 這是CUDA更新系列的第四篇文章&#xff0c;它的目標是刷新CUDA中的關鍵概念、工具和初級或中級開發人員的優化。 CUDA編程模型提供了GPU體系結…

php curl_error源碼,PHP curl_error函數

PHP curl_error函數(PHP 4 > 4.0.3, PHP 5)curl_error — 返回一個保護當前會話最近一次錯誤的字符串說明string curl_error ( resource $ch )返回一條最近一次cURL操作明確的文本的錯誤信息。參數ch由 curl_init() 返回的 cURL 句柄。返回值返回錯誤信息或 (空字符串) 如果…

SQL中Where與Having的區別

“Where” 是一個約束聲明&#xff0c;使用Where來約束來之數據庫的數據&#xff0c;Where是在結果返回之前起作用的&#xff0c;且Where中不能使用聚合函數。“Having”是一個過濾聲明&#xff0c;是在查詢返回結果集以后對查詢結果進行的過濾操作&#xff0c;在Having中可以使…

java 邏輯表達式 布爾_使用基本邏輯門實現布爾表達式

java 邏輯表達式 布爾將布爾表達式轉換為邏輯電路 (Converting Boolean Expression to Logic Circuit) The simplest way to convert a Boolean expression into a logical circuit is to follow the reverse approach in which we start from the output of the Boolean expre…