for this topic : best video link
https://www.youtube.com/watch?v=1RpMc3fv0y4
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std;
const long maxn = 100000;
long x[maxn],i,n,length=0,increasingSub[maxn],parent[maxn],LIS[maxn],j;
long lis(long x[])
{
for(i=0; i<n; i++)
{
long low=1;
long high=length;
while(low <= high)
{
long mid =(low + high)/2;
if(x[increasingSub[mid]] < x[i])
low = mid + 1;
else
high = mid - 1;
}
long pos = low;
parent[i] = increasingSub[pos-1];
increasingSub[pos] = i;
if(pos > length)
length=pos;
}
long k=increasingSub[length];
for(j=length-1; j>=0; j--)
{
LIS[j] = x[k];
k = parent[k];
}
printf("LIS Length = ");
cout<<length<<endl;
printf("LIS Path = ");
for(i=0; i<length; i++)
{
cout<<LIS[i]<<" ";
}
}
main()
{
printf("Enter the size of array = ");
cin>>n;
for(i=0; i<n; i++)
{
cin>>x[i];
}
long long ll=lis(x);
}
https://www.youtube.com/watch?v=1RpMc3fv0y4
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std;
const long maxn = 100000;
long x[maxn],i,n,length=0,increasingSub[maxn],parent[maxn],LIS[maxn],j;
long lis(long x[])
{
for(i=0; i<n; i++)
{
long low=1;
long high=length;
while(low <= high)
{
long mid =(low + high)/2;
if(x[increasingSub[mid]] < x[i])
low = mid + 1;
else
high = mid - 1;
}
long pos = low;
parent[i] = increasingSub[pos-1];
increasingSub[pos] = i;
if(pos > length)
length=pos;
}
long k=increasingSub[length];
for(j=length-1; j>=0; j--)
{
LIS[j] = x[k];
k = parent[k];
}
printf("LIS Length = ");
cout<<length<<endl;
printf("LIS Path = ");
for(i=0; i<length; i++)
{
cout<<LIS[i]<<" ";
}
}
main()
{
printf("Enter the size of array = ");
cin>>n;
for(i=0; i<n; i++)
{
cin>>x[i];
}
long long ll=lis(x);
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন