Can we reduce the complexity using segment tree here or any other means? if yes, How?
it's a code for the finding longest chain like the example below:
if
A(x1,y1) at index i in array
B(x2,y2) at index j in array
if( i<j and x1<=x2 and y1<=y2 and !(x1==x2 and y1==y2))
then A and B are in a chain of length 2
let say
A={(0,0),(1,0),(1,1),(2,1),(2,0)}
then
longest chain = {(0,0),(1,0),(1,1),(2,1)}
so answer here is 4 elements
I have used Dynamic programming here which is O(N^2) for my code
struct Pair
{
int x;
int y;
};
int main()
{
int n;
cin>>n;
vector<Pair> arr;
int L[1000000];
Pair a;
int i;int Maxchain=0;
for(i=0;i<n;i++)
{
cin>>a.x>>a.y;
arr.push_back(a);
L[i]=1;
for (int j = i-1; j >=0; j--)
{
if ((L[j]>=L[i])&&(arr[j].x <= arr[i].x) && (arr[j].y <= arr[i].y) && !(arr[j].x == arr[i].x && arr[j].y == arr[i].y))
L[i] = L[j]+1;
}
Maxchain = L[i]>Maxchain ?L[i]:Maxchain ;
}
cout<<Maxchain;
return 0;
}