汉子编码比字母编码长_字母/博客作者编码问题(使用动态编程)

汉子编码比字母编码长

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.mzph.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…