Created
June 4, 2019 17:26
-
-
Save harshraj22/62413e0161ae9a85fd495ccbaa336f96 to your computer and use it in GitHub Desktop.
Contains details of solution and how to seek for help.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Your file with changes | |
#include<bits/stdc++.h> | |
using namespace std; | |
//no need to pass the whole array every time as : | |
// you are not changing the array | |
// rather make the array global . | |
int i,j,k,c=0,n,en=0,on=0,b; | |
int f(int b,int i,int c,int a[]){ | |
if(i==n)//if reached the end of array | |
return c; | |
if(a[i]%2) | |
on++; | |
else | |
en++; | |
if(en==on && (i+1)!=n && abs(a[i+1]-a[i])<=b){ | |
//if even count =odd count and the index is not last (we are not allowed | |
//to cut at the end and the cost of cut is affordable) | |
en=0;on=0;//start counting again | |
// c=max(c,max(f(b-abs(a[i+1]-a[i]),i+1,c+1,a),f(b,i+1,c,a))); | |
// better way to take max of more numbers | |
c=max({c, f(b-abs(a[i+1]-a[i]),i+1,c+1,a), f(b,i+1,c,a) }); | |
/*The point to note here is that function 'f' doesn't return a negative value. | |
It either adds something to the value of c it received, or doesn't do anything. | |
so, instead of c=max({c,....}); you can write c+=max(f(), f()); and make function | |
return just the excess value of c. | |
*/ | |
//update max number of cuts as maximum of (current value of cuts, | |
//value of current cut is applied, current cut is not applied) | |
} | |
else | |
c=f(b,i+1,c,a); | |
//if even count!=odd count or cost is not affordable (note we already checked for | |
//reaching end of array) | |
return c; | |
} | |
int main(){ | |
cin>>n>>b; | |
int a[n]; | |
for(i=0;i<n;i++) | |
cin>>a[i]; | |
cout<<f(b,0,0,a)<<"\n"; | |
return 0; | |
} | |
/* | |
6 100 | |
1 2 3 4 5 6 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Accepted solution | |
#include<bits/stdc++.h> | |
using namespace std; | |
int a[1000004]; | |
int dp[102][102]; | |
int i,j,k,c=0,n,en=0,on=0,b; | |
//see how we reduced the number of reqired parameters,these are what we call states | |
//in dp, and these decide the dimention of dp array | |
// https://qr.ae/TWGZ9O read this answer for more details | |
// | |
int f(int b,int i){ | |
// cout<<" b and i "<<b<<" "<<i<<"\n"; | |
int c=0; | |
if(i==n || b<=0)//if reached the end of array | |
return c; | |
else if(dp[b][i]!=-1)//if we have calculated the same thing earlier | |
return dp[b][i];//don't recompute, return it else compute it,store and then return | |
if(a[i]%2) | |
on++; | |
else | |
en++; | |
if(en==on && (i+1)!=n && abs(a[i+1]-a[i])<=b){ | |
//if even count =odd count and the index is not last (we are not allowed | |
//to cut at the end and the cost of cut is affordable) | |
en=0;on=0;//start counting again | |
// c=max(c,max(f(b-abs(a[i+1]-a[i]),i+1,c+1,a),f(b,i+1,c,a))); | |
// better way to take max of more numbers | |
c=max( 1+f(b-abs(a[i+1]-a[i]),i+1), f(b,i+1) ); | |
// max of (if cut is made, cut isn't made) | |
} | |
else | |
c=f(b,i+1); | |
//if even count!=odd count or cost is not affordable (note we already checked for | |
//reaching end of array) | |
return dp[b][i]=c; | |
} | |
int main(){ | |
memset(dp,-1,sizeof(dp)); | |
cin>>n>>b; | |
for(i=0;i<n;i++) | |
cin>>a[i]; | |
cout<<f(b,0)<<"\n"; | |
return 0; | |
} | |
/* | |
6 100 | |
1 2 3 4 5 6 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Please learn to write commments as much as possible, this will fasten the helping process a lot. | |
Also go through the comments added in both codes carefully as this will help you to understand better. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
link to accepted solution