Solving a dp problem from codeforces named Cut Ribbon - dynamic-programming

I was trying to solve this problem but I am getting wrong answer for some input. Can anyone explain where the problem is? Here is my code:
#include<bits/stdc++.h>
using namespace std;
#define INF -999999999
int dp[4001][4001],a[3];
int Ribbon(int n,int i)
{
if(i==3)
{
if(n==0)
return 0;
if(n>0)
return INF;
}
if(dp[n][i]!=-1)
return dp[n][i];
int include=0,exclude=0;
if(n-a[i]>=0)
include=Ribbon(n-a[i],i)+1;
exclude=Ribbon(n,i+1);
return dp[n][i]=max(include,exclude);
}
int main()
{
memset(dp,-1,sizeof dp);
int n,ans=0;
cin>>n>>a[0]>>a[1]>>a[2];
ans=Ribbon(n,2);
cout<<ans<<endl;
}
When I am giving 7 5 5 2 as input, it is giving 3 in answer whereas it should be 2.

Related

Issue With struct

I am running this simple code to be used in segment trees. Shows a lot of errors. Could anyone explain where am I going wrong here? It basically has problems with declaring a vector of nodes.
`
// C++ program to show segment tree operations like construction, query and update
#include <bits/stdc++.h>
using namespace std;
struct node{
int open, closed, full;
node(int op, int cl, int f){
open = op;
closed = cl;
full = f;
}
};
void printseg(vector<node> &v){
for(auto it: v){
cout<<it.open<<" ";
cout<<it.closed<<" ";
cout<<it.full;
}
cout<<"\n";
}
void build(int index, int low, int high, string s, vector<node> seg){
if(low==high){
seg[index] = node(s[low] == '(', s[low] == ')', 0);
return;
}
int mid = (low+high)/2;
build(2*index+1, low, mid, s, seg);
build(2*index+2, mid+1, high, s, seg);
seg[index] = merge_nodes(seg[2*index+1], seg[2*index+2]);
}
void solve4(){
string s;
cin>>s;
int n = s.size();
vector<node> seg(4*n);
build(0, 0, n-1, s, seg);
printseg(seg);
}
`

Line 1034: Char 9: runtime error: reference binding to misaligned address 0xbebebebebebebebe for type 'int', which requires 4 byte alignment

I'm getting runtime error for this problem 01 Matrix (https://leetcode.com/problems/01-matrix/description/ Can anyone check what's wrong with my code/logic?
My code:-
class Solution {
public:
int f(int i,int j,vector<vector<int>>& mat,vector<vector<int>>&dp){
if(i<0 or i>mat.size())return -1;
if(j<0 or j>mat[0].size())return -1;
if(mat[i][j]==0){
return 0;
}
if(dp[i][j]!=-1) return dp[i][j];
int l=f(i,j-1,mat,dp);
int r=f(i,j+1,mat,dp);
int u=f(i-1,j,mat,dp);
int d=f(i+1,j,mat,dp);
return dp[i][j]=1+min({l,d,r,u});
}
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
int n=mat.size();
int m=mat[0].size();
vector<vector<int>> v(n+1,vector<int>(m+1,0));
vector<vector<int>> dp(n+1,vector<int>(m+1,-1));
for(int i=0;i<mat.size();i++){
for(int j=0;j<mat[0].size();i++){
v[i][j]=f(i,j,mat,dp);
}
}
return v;
}
};
I tried running the code and I was getting an error(as pasted above) Can someone help me?

Able to get result on vscode but not on leetcode 198. House Robber

This is my soln can anyone help me. I am getting heap-buffer-overflow error on leetcode 198. House Robber.
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int solve(vector<int>& nums,int n,vector<int>& dp){
if(n<0){
return 0;
}
if(n==0){
return nums[0];
}
if(dp[n]!=-1){
return dp[n];
}
int incl=solve(nums,n-2,dp)+nums[n];
int excl=solve(nums,n-1,dp)+0;
dp[n]=max(incl,excl);
return dp[n];
}
int rob(vector<int>& nums) {
int n=nums.size();
vector<int> dp(n+1,-1);
return solve(nums,n,dp);
}
int main(){
vector<int> t={2,7,9,3,1};
cout<<rob(t);
return 0;
}
I have written this code in vscode and its working fine;

Using references to make a function return more than one value in c++

I wrote a code to calculate area and circumference of a circle using references.I am getting error message as
" unresolved external symbol "void c_decl circle" " AND
" unresolved externals"AND
"more than one instance of overloaded function"
I have given the code below
#include<stdafx.h>
#include<iostream>
void circle(int,float,float);
using namespace std;
int main()
{
int r;
float a=0.0,c=0.0;
cout<<"Enter the radius:"<<endl;
cin>>r;
circle(r,a,c);
cout<<a<<"\t"<<c<<endl;
return 0;
}
void circle(const int &i,float &j,float &k)
{
j=3.14*i*i;
k=2*3.14*i;
}
Please help.Thanks
This might be what you wanted.
The Circle function needs to be above the main method so the compiler knows it exists when you're calling it.
The j and k parameters of the Circle function are points. Pointers are declared with the '' . The '' is also used to get the value from the pointer.
circle(r,&a,&c), This methods takes in the memory locations of both a and c. The memory locations are then given to the pointers. The & gets the memory location while the * gets the actual value.
Anyway it seems to work this way.
#include<iostream>
void circle(int,float,float);
using namespace std;
void circle( int i,float *j,float *k)
{
float s;
*j=3.14*i*i;
*k=2*3.14*i;
}
int main()
{
int r;
float a=0.0,c=0.0;
cout<<"Enter the radius:"<<endl;
cin>>r;
circle(r,&a,&c);
cout<<a<<"\t"<<c<<endl;
return 0;
}

Error handling failure

I'm new and a novice programmer and trying to learn.. I've been trying to do a library program using structures, with the following functions I've created. add a new customer, find number of customers, print details of a customer,borrow book,reserve book, return book
what I failed is that ; when I add a new customer my program asks for name, address and Id, and, and I want my program to give an error message when I try to register a new customer with an already existing id, I'm also going to post my codes.
I'm not asking for codes from you, I just want to know, what I've done wrong and how I can fix it, any hints will be appreciated thanks
My Codes:
#include <iostream>
using namespace std;
const int maxx=100; //max 100 users
const int maxborrow=5; //maxx borrow books
int bi=0; //counter for books
int i=0; //counter for users
int number_of_customers=0;
//initialize numebr of users to 0
struct loanreserved
{
int loan; // 1 indicates true 0 indicates false if a book is reserved for example it's 1 if available 0
int reserved;
};
struct duedate
{
int day;
int month;
int year;
};
struct bookinf
{
char title[maxx];
char author[maxx];
int ISBN;
loanreserved loanorreserved;
duedate bookduedate;
};
struct userinf
{
char name[maxx];
char address[maxx];
int Id;
int number_of_reserved_books;
int number_of_loan_books;
bookinf customersbookinf[maxborrow];
};
userinf uniclibrary[maxx];
int readcustomer()
{
int uniqueid;
cout<<"Customer name: ";
cin>>uniclibrary[i].name;
cout<<"Customer address: ";
cin>>uniclibrary[i].address;
cout<<"Customer Id: ";
cin>>uniqueid; //save id to temp file;
for(int x=0;x<maxx;x++)
{
if(uniqueid!=uniclibrary[x].Id)
{
uniclibrary[i].Id=uniqueid;
cout<<"Customer registration succeeded ! \n";
number_of_customers++;
return 1; //success
}
}
cout<<"This user is already registered ! ";
return 0; //fail
system("pause");
system("cls");
You can have a static variable that keeps track of existing customers:
#include <set>
int readcustomer()
{
static std::set<std::string> existingNames;
std::string name;
cout<<"Customer name: ";
cin>>name;
if ( existingNames.find(name) != existingNames.end() )
{
//name already exists
return 0;
}
else
{
existingNames.insert(name);
}
//....
}
Of course, this is a quick fix. Better take your code to codereview. The're MUCH that can be improved.

Resources