最大化切割段

Description:

描述:

In this article we are going to review classic dynamic programing problem which has been featured in interview rounds of amazon.

在本文中,我们将回顾在亚马逊的采访轮次中已经介绍的经典动态编程问题。

Problem statement:

问题陈述:

Given an integer N denoting the Length of a rod, one needs to cut the rod in such a way that the cut length of the rod segment each time is integer either x, y or z and after performing all cutting operation the total number of cut segments must be maximum. You need to find the maximum number of segments possible to cut.

给定一个表示杆长度的整数N ,则需要以这样一种方式切割杆,即杆段的切割长度每次都是xyz的整数,并且在执行所有切割操作后,切割的总数细分必须最大。 您需要找到可能要切割的最大段数。

Example:

例:

    Input1
Rod length: 6
Segment's length:
x=3,y=2,z=1
Output1
Maximum number of cut segments: 6
Input2
Rod length: 5
Segment's length:
x=5,y= 3,z= 2
Output2
Maximum number of cut segments: 2

Example with explanation:

带有说明的示例:

    For the first example,
Length of the rod is: 4
Three cut segments’ length is 3, 2, 1 respectively. 
To get maximum cuts we would cut in segments of length 
1 which will have outcome 6.
For the second example,
Length of the rod is: 5
Three cut segments’ length is 5, 3, 2 respectively. 
To get maximum cuts we would cut in segments of 
length 2 & length 3 which will have outcome 2. 

Solution Approach:

解决方法:

This problem can be solved recursively.

此问题可以递归解决。

    f(n) = number of cut segments for rod with length n
f(n) = 1+maximum(f(n-x),f(n-y),f(n-z))

With base case,

在基本情况下

    f(negative number) = INT_MIN
f(0) = 0

So, the recursive function definition would be:

因此,递归函数定义为:

Maximize the cut segments

Solving the above recursive function would give the result.

解决上述递归函数将得到结果。

The function is defined below,

该函数定义如下,

    Function cutTheRod(n):
If n<0
return INT_MIN
If n==0
return 0;
return 1+maximum(f(n-x),f(n-y),f(n-z));
End Function

If you draw any recursion tree, you will find many overlapping subproblems and hence we need to store them and use dynamic Programing to solve.

如果您绘制任何递归树,您会发现许多重叠的子问题,因此我们需要存储它们并使用动态编程来解决。

Before going to a dynamic programming solution, let’s talk about one observation. The thing is as we need to find the maximum number of cuts, we would like to use the minimum long segment as much as possible. It seems that greedy would have worked by choosing minimum long segments and then upgrading to other segments. If greedy works for a test case, the solution is guaranteed to be optimal. Earlier we have seen that greedy may not be optimal, but it’s not in this case. In this case greedy is optimal. The issue is greedy doesn’t work for all test cases. Think of a test case such that rod length is 9:

在使用动态编程解决方案之前,让我们谈谈一个观察。 事情是因为我们需要找到最大的切割数量,因此我们希望尽可能地使用最小的长段。 贪婪似乎可以通过选择最小的长段然后升级到其他段来起作用。 如果贪婪适用于测试用例,则可以保证解决方案是最佳的。 之前我们已经看到贪婪可能不是最佳选择,但在这种情况下并非如此。 在这种情况下,贪婪是最优的。 问题是贪心并不适用于所有测试用例。 考虑一个测试用例,其杆长度为9:

And segment lengths are: 5, 3, 2

段长度为:5、3、2

Greedy would have cut 4 segments of length 4 and then fails eventually as no segment of length 1. That’s why we need DP, but there is one greedy technique that can be added along to optimize. Since, we need to find maximum cuts, find out the minimum segment and check whether the rod length is divisible by the minimum length or not. If it’s divisible that ensures that rod length/minimum cut length is the maximum number of cut segments. You can check for example 1 there are 4 cut segments, which is rod length(4)/ minimum segment length(1) since rod length was divisible by minimum segment length.

贪婪本来会切掉4个长度为4的片段,然后由于没有长度为1的片段而最终失败。这就是为什么我们需要DP的原因,但是可以添加一种贪婪技术来进行优化。 因为,我们需要找到最大的切口,找出最小的段,并检查杆的长度是否可被最小长度整除。 如果可以整除,则确保杆长度/最小切割长度为最大切割段数。 例如,您可以检查1个有4个切割段,这是杆长度(4)/最小段长度(1),因为杆长度可以被最小段长度整除。

The Dynamic programing solution can be found below:

动态编程解决方案可以在下面找到:

  1. Initialize dp[n+1] in the following way.

    通过以下方式初始化dp [n + 1]。

        dp[0]=0
    for  i=1 to n
    dp[i]=INT_MIN
    
    
  2. Fill the table

    填表

        for i=1 to i++)
    if(i-x>=0 && dp[i]<1+dp[i-x])
    dp[i]=1+dp[i-x];
    if(i-y>=0 && dp[i]<1+dp[i-y])
    dp[i]=1+dp[i-y];
    if(i-z>=0 && dp[i]<1+dp[i-z])
    dp[i]=1+dp[i-z];
    end for
    
    
  3.     If dp[n]<=0
    Cutting not possible
    Else
    That's the result.
    
    

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
int cuttingrod(int n, int x, int y, int z)
{
int minimum;
if (x < y && x < z)
minimum = x;
if (y < x && y < z)
minimum = y;
if (z < y && z < x)
minimum = z;
if (minimum != 0 && n % minimum == 0)
return (n / minimum);
int dp[n + 1];
for (int i = 1; i <= n; i++)
dp[i] = INT_MIN;
dp[0] = 0;
for (int i = 1; i <= n; i++) {
if (i - x >= 0 && dp[i] < 1 + dp[i - x]) {
dp[i] = 1 + dp[i - x];
}
if (i - y >= 0 && dp[i] < 1 + dp[i - y]) {
dp[i] = 1 + dp[i - y];
}
if (i - z >= 0 && dp[i] < 1 + dp[i - z]) {
dp[i] = 1 + dp[i - z];
}
}
return dp[n];
}
int main()
{
int t, n, x, y, z;
cout << "Original rod length:\n";
cin >> n;
cout << "First cut segment length,x:\n";
cin >> x;
cout << "Second cut segment length,y:\n";
cin >> y;
cout << "Third cut segment length,z:\n";
cin >> z;
int ans = cuttingrod(n, x, y, z);
if (ans > 0)
cout << "Max number of cut segments: " << ans << endl;
else
cout << "Can't be cut down\n";
return 0;
}

Output

输出量

RUN 1:
Original rod length:
6
First cut segment length,x:
3 2 1
Second cut segment length,y:
Third cut segment length,z:
Max number of cut segments: 6
RUN 2:
Original rod length:
17
First cut segment length,x:
11
Second cut segment length,y:
13
Third cut segment length,z:
9
Can't be cut down

翻译自: https://www.includehelp.com/icp/maximize-the-cut-segments.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540714.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

响应数据传出(springMVC)

1. SpringMVC 输出模型数据概述 提供了以下几种途径输出模型数据&#xff1a; ModelAndView: 处理方法返回值类型为 ModelAndView 时, 方法体即可通过该对象添加模型数据 Map 及 Model: 入参为 org.springframework.ui.Model、 org.springframework.ui.ModelMap 或 java.uti…

微信网页扫码登录的实现

为了让用户登录网站的门槛更低&#xff0c;微信扫一扫登录变得越来越广泛&#xff0c;所以最近加紧赶制的项目中有用到这个功能&#xff0c;此篇文字的出发点基于微信开放平台已经配置好域名&#xff08;80端口&#xff09;并且认证成功获得app_id和secret并有权限调用微信的接…

希尔密码_希尔密码| 网络安全

希尔密码Now, Hill Cipher is a very basic cryptographic technique which is used to convert a string into ciphertext. This technique was invented by an American Mathematician "Lester Sanders Hill". This is a polygraphic substitution cipher because …

Android 那些年,处理getActivity()为null的日子

在日常开发中的时候&#xff0c;我们经常会使用ViewPagerFragment进行视图滑动&#xff0c;在某些部分逻辑也许我们需要利用上下文Context&#xff08;例如基本的Toast&#xff09;&#xff0c;但是由于Fragment只是衣服在Activity容器的一个试图&#xff0c;如果需要拿到当前的…

设计模式状态模式uml_UML的完整形式是什么?

设计模式状态模式umlUML&#xff1a;统一建模语言 (UML: Unified Modeling Language) UML is an abbreviation of Unified Modeling Language. In the field of software engineering, it is a visual modeling language that is standard in quality. It makes it available t…

vqa mcb_MCB的完整形式是什么?

vqa mcbMCB&#xff1a;微型断路器 (MCB: Miniature Circuit Breaker) MCB is an abbreviation of "Miniature Circuit Breaker". MCB是“微型断路器”的缩写 。 It is an automatically operated electronics switch. It is designed to detect the fault in the e…

CentOS忘记普通用户密码解决办法

普通用户忘记密码 1.使用root用户登录系统&#xff0c;找到/etc/shadow文件。 2.找到用户名开头的那一行&#xff0c;例如我的用户名为pds,&#xff0c;以冒号为分割符&#xff0c;红色部分是密码加密部分 pds:$1$CivopRgF$ajWQ54W1XJbifFjm05Jk/1:15353:0:99999:7::: 3.pds是我…

esp32的GPIO操作

对于任何一款芯片&#xff0c;GPIO接口是其最基本的组成部分&#xff0c;也是一款芯片入门的最基本操作&#xff0c;下面论述下 关于esp32开发版的GPIO操作&#xff0c;本文中重点讲解下 关于如何创建eclipse工程&#xff0c;并通过eclipse下载到esp32中去&#xff08;本文的工…

聚焦数据的力量——全球领先安全技术分享会在京召开

ZD至顶网安全频道 04月21日 综合消息&#xff1a; 由中国网络安全与信息化产业联盟、360共同主办的“数据的力量——全球领先安全技术分享会“今日在北京成功召开。来自政府、企业、教育、投资机构和产业联盟的300多位嘉宾参加了本次技术分享会&#xff0c;共同就安全产业发展趋…

如何设置Fedora默认从命令行启动?

2019独角兽企业重金招聘Python工程师标准>>> Sumary:因为在Fedora中没有/etc/initab文件我们不方便从这里设置它的runlevel target&#xff0c;但是Linux又给我们提供了一个强悍的工具systemd,我们可以用system来链接默认的启动级别&#xff0c;所以开始吧&#xff…

工控领域的网络攻击 食尸鬼行动深入解读Operation Ghoul

卡巴斯基于2016年6月监测到了Operation Ghoul&#xff08;食尸鬼行动&#xff09;网络攻击&#xff0c;Operation Ghoul针对30多个国家的工业、制造业和工程管理机构发起了定向渗透入侵。目前&#xff0c;卡巴斯基发现&#xff0c;有130多个机构已被确认为这类攻击的受害者。 该…

tomcat:sessionId生成机制导致tomcat启动过慢问题

为什么80%的码农都做不了架构师&#xff1f;>>> http://blog.csdn.net/u013939884/article/details/72860358 转载于:https://my.oschina.net/wii01/blog/1527731

Codeforces Round #431 (Div. 2)

A. Odds and Endstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputWhere do odds begin, and where do they end? Where does hope emerge, and will they ever break? Given an integer sequence a1, a2, ..., a…

ping/pong模式_PING的完整形式是什么?

ping/pong模式PING&#xff1a;数据包InterNet Groper (PING: Packet InterNet Groper) In the sector of networking of computers, PING is an abbreviation of Packet InterNet Groper. It is utility software or system software of administration of computer network u…

Gartner: 2017年11大信息安全技术(解读版)

在2017年6月份举办的第23届Gartner安全与风险管理峰会上&#xff0c;Gartner的Fellow——Neil McDonald发布了2017年度的11个最新最酷的信息安全技术&#xff0c;比往年的10大技术多了一项。以往都是通过互联网了解Gartner的各种信息和报告。这次&#xff0c;本人有幸亲临现场&…

博客url什么形式_URL的完整形式是什么?

博客url什么形式URL&#xff1a;统一资源定位符 (URL: Uniform Resource Locator) URL is an abbreviation of Uniform Resource Locator. Uniform Resource Locator which is informally or casually known as a web address is addressed as a resource of the web, which ca…

宝马奥迪工厂模式_宝马的完整形式是什么?

宝马奥迪工厂模式宝马&#xff1a;巴伐利亚汽车公司 (BMW: Bayerische Motoren Werke) BMW is an abbreviation of "Bayerische Motoren Werke". It is a multinational automobile and motorcycle manufacturing company whose headquarter is situated in Munich, …

艾拉物联CEO :物联网时代的到来让安全问题显得尤为突出

产品安全和嵌入式安全的理念一直都很复杂&#xff0c;不过我们至少对它们比较熟悉。但物联网&#xff08;IoT&#xff09;却对“产品”这一理念进行了颠覆&#xff0c;让联网成为了产品定义中不可或缺的一部分。 由此一来&#xff0c;仅在设备层面讨论安全已经远远不够了。不论…

jquery选择器连续选择_JQuery中的选择器

jquery选择器连续选择Its time to write some JQuery now. Do check out the introductory article on JQuery first in case you havent. Before we move to Selectors in JQuery, lets talk a bit about the general syntax first. 现在该写一些JQuery了。 如果没有&#xff…

split注意事项

为什么80%的码农都做不了架构师&#xff1f;>>> 1.特殊字符 “|”&#xff0c;“*”&#xff0c;“^”&#xff0c;"."&#xff0c;“&#xff1a;”&#xff0c;使用此字符作为分割符&#xff0c;必须用\\加以转义 2.同时存在多个特殊字符的时候&#x…