Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement