Блог пользователя ABhinav2003

Автор ABhinav2003, история, 6 лет назад, По-английски
#include<bits/stdc++.h>
using namespace std;
int in=0 , ex;
int sub(int a[] , int len)
{
//	if(len<1)
//		return 0;
	 if(len <= 0  )
		return 0;

	  ex = sub(a,len-1);
	  cout<<"EX "<<ex<<"\n";

	 if(a[len-1] >a[len-2])
	 {
		int t = a[len-1]>a[len-2];cout<<"T "<<t<<"\n";
	 	 in = 1+sub(a,len-1);
	 }
	 cout<<"IN "<<in<<"\n";

	int max1 = max(in,ex);
	cout<<"MAX "<<max1<<"\n";

	return max1;
}

int main()
{
	int len;
	cin>>len;
	 int a[len];
	for(int i = 0;i<=len-1;i++)
	{
		cin>>a[i];
	}
	int m = sub(a , len);
	cout<<m;
}
[**HERE IS THE PROBLEM**](https://codeforces.me/problemset/problem/702/A)
  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

There are many flaws in this code.

  1. Base case is len <= 0 so, when len = 1, the index of a[len-2] will become negative.
  2. in is a global variable, you'll have to add in = 0 before the if statement.

Even if you correct these flaws, the basic flaw is that this method will give you a subsequence instead of a subarray.

For example, for the input 1 2 3 10 4 5 6, your output will be 6 (after correcting the mentioned 2 points).

  • »
    »
    6 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    PLZ CAN U CORRECT MY FLAW AND GIVE IT TO ME I CAN"T UNDERSTAND IT PROPERLY BRO PLZ HELP I AM struggling for about 2-3 days now and not able to do this

    actually i have just started DP PLZ UNDERSTAND AND HELP ME JUST REWRITE THE FUNCTION WITH REMOVING ALL FLAWS

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится +6 Проголосовать: не нравится
      Recursive code which will give TLE
      Memoized version of the same code
      No dp required

      I'd strictly recommend you to learn dp first or you might not understand my solution.