
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximize the number of segments of length p, q and r in C++
Problem statement
Given a rod of length L, the task is to cut the rod in such a way that the total number of segments of length p, q and r is maximized. The segments can only be of length p, q, and r
If l = 15, p = 2, q = 3 and r = 5 then we can make 7 segments as follows −
{2, 2, 2, 2, 2, 2, 3}
Algorithm
We can solve this problem using dynamic programming
1. Initialize dp[] array to 0 2. Iterate till the length of the rod. For every i, a cut of p, q and r if possible is done. 3. Initialize ans[i+p] = max( ans[i+p], 1 + ans[i]), ans[i+q] = max(ans[i+q], 1 + ans[i]) and ans[i+r] = max(ans[i+r], 1 + ans[i]) for all the possible cuts. 4. ans[i] will be 0 if a cut at i-th index is not possible. ans[l] will give the maximum number of cuts possible
Example
#include <bits/stdc++.h> using namespace std; int getMaximumSegments(int l, int p, int q, int r){ int dp[l + 1]; memset(dp, -1, sizeof(dp)); dp[0] = 0; for (int i = 0; i <= l; ++i) { if (dp[i] == -1) { continue; } if (i + p <= l) { dp[i + p] = max(dp[i + p], dp[i] + 1); } if (i + q <= l) { dp[i + q] = max(dp[i + q], dp[i] + 1); } if (i + r <= l) { dp[i + r] = max(dp[i + r], dp[i] + 1); } } return dp[l]; } int main(){ int l = 15, p = 2, q = 3, r = 5; cout << "Number of segments = " << getMaximumSegments(l, p, q, r) << endl; return 0; }
Output
When you compile and execute the above program. It generates the following output−
Number of segments = 7
- Related Articles
- Name the line segments determined by the three collinear points P, Q and R.
- If $p=-2,\ q=-1$ and $r=3$, find the value of $p-q-r$.
- In the figure, \( O Q: P Q=3: 4 \) and perimeter of \( \Delta P O Q=60 \mathrm{~cm} \). Determine \( P Q, Q R \) and \( O P \)."\n
- A triangle \( P Q R \) is drawn to circumscribe a circle of radius \( 8 \mathrm{~cm} \) such that the segments \( Q T \) and \( T R \), into which \( Q R \) is divided by the point of contact \( T \), are of lengths \( 14 \mathrm{~cm} \) and \( 16 \mathrm{~cm} \) respectively. If area of \( \Delta P Q R \) is \( 336 \mathrm{~cm}^{2} \), find the sides \( P Q \) and \( P R \).
- If $a = xy^{p-1}, b = xy^{q-1}$ and $c = xy^{r-1}$, prove that $a^{q-r} b^{r-p} c^{p-q} = 1$.
- If $p=-2,\ q=-1$ and $r=3$, find the value of $p^{2}+q^{2}-r^{2}$.
- Out of three metals P, Q and R, P is less reactive than Q and R is more reactive than P and Q both. Suggest an activity to arrange P, Q and R in order of their decreasing reactivity.
- If p,q and r are in proportion and q =17, r=289, find p.
- In \( \Delta P Q R \), right-angled at \( Q, P Q=3 \mathrm{~cm} \) and \( P R=6 \mathrm{~cm} \). Determine \( \angle P \) and \( \angle R \).
- Construct a triangle \( P Q R \) with side \( Q R=7 \mathrm{~cm}, P Q=6 \mathrm{~cm} \) and \( \angle P Q R=60^{\circ} \). Then construct another triangle whose sides are \( 3 / 5 \) of the corresponding sides of \( \triangle P Q R \).
- If $p,\ q,\ r$ are in A.P., then show that $p^2( p+r),\ q^2( r+p),\ r^2( p+q)$ are also in A.P.
- The corresponding sides of triangles ABC and PQR are in the same ratio, that is, \( A B: P Q=A C: P R \). What is the length of \( A C? \)
- If $p=-2,\ q=-1$ and $r=3$, find the value of $2 p^{2}-q^{2}+3 r^{2}$.
- Simplify the following.a) \( (l^{2}-m^{2})(2 l+m)-m^{3} \)b) \( (p+q+r)(p-q+r)+p q-q r \)
- If p, q, and r are in continued proportion, find p if q=17 and r =289.

Advertisements