Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. // Your file with changes
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. //no need to pass the whole array every time as :
  6. // you are not changing the array
  7. // rather make the array global .
  8.  
  9. int i,j,k,c=0,n,en=0,on=0,b;
  10. int f(int b,int i,int c,int a[]){
  11. if(i==n)//if reached the end of array
  12. return c;
  13. if(a[i]%2)
  14. on++;
  15. else
  16. en++;
  17. if(en==on && (i+1)!=n && abs(a[i+1]-a[i])<=b){
  18. //if even count =odd count and the index is not last (we are not allowed
  19. //to cut at the end and the cost of cut is affordable)
  20. en=0;on=0;//start counting again
  21.  
  22. // c=max(c,max(f(b-abs(a[i+1]-a[i]),i+1,c+1,a),f(b,i+1,c,a)));
  23.  
  24. // better way to take max of more numbers
  25. c=max({c, f(b-abs(a[i+1]-a[i]),i+1,c+1,a), f(b,i+1,c,a) });
  26. /*The point to note here is that function 'f' doesn't return a negative value.
  27. It either adds something to the value of c it received, or doesn't do anything.
  28. so, instead of c=max({c,....}); you can write c+=max(f(), f()); and make function
  29. return just the excess value of c.
  30. */
  31. //update max number of cuts as maximum of (current value of cuts,
  32. //value of current cut is applied, current cut is not applied)
  33. }
  34. else
  35. c=f(b,i+1,c,a);
  36. //if even count!=odd count or cost is not affordable (note we already checked for
  37. //reaching end of array)
  38.  
  39. return c;
  40. }
  41.  
  42. int main(){
  43.  
  44. cin>>n>>b;
  45. int a[n];
  46. for(i=0;i<n;i++)
  47. cin>>a[i];
  48.  
  49. cout<<f(b,0,0,a)<<"\n";
  50. return 0;
  51. }
  52. /*
  53. 6 100
  54. 1 2 3 4 5 6
  55. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement