Skip to content

Commit ec42d41

Browse files
committed
Update with LCIS and miscellaneous
1 parent 5bc0ae2 commit ec42d41

6 files changed

+621
-24
lines changed

Dynamic Programming/05 LIS.cpp

+27-23
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,16 @@ LIS[i] = LIS ending at index i
5151
5252
**/
5353

54-
int main()
54+
vector<int> solveLIS(vector<int>&v)
5555
{
56-
optimizeIO();
57-
58-
int n;
59-
cin>>n;
60-
61-
vector<int> v(n);
62-
63-
for(int i=0;i<n;i++)
64-
cin>>v[i];
56+
int n = v.size();
6557

6658
vector<int>LIS(n,1); /** length of longest increasing sequence ending at i **/
6759
vector<vector<int>>SEQ(n); /** longest increasing sequence ending at i **/
6860

69-
for(int i=0;i<n;i++)
61+
for(int i=0; i<n; i++)
7062
{
71-
for(int j=0;j<i;j++)
63+
for(int j=0; j<i; j++)
7264
{
7365
if(v[i]>v[j])
7466
{
@@ -80,27 +72,39 @@ int main()
8072
}
8173
}
8274
SEQ[i].push_back(v[i]);
83-
DBG(SEQ[i]);
75+
// DBG(SEQ[i]);
8476
}
8577

8678
int lis_len = *max_element(ALL(LIS));
8779
DBG(lis_len);
8880

89-
int mx = 0;
90-
vector<int>MX;
81+
vector<int>vLIS;
9182

9283
for(int i=0; i<n; i++)
9384
{
94-
if(LIS[i]>mx)
85+
if(LIS[i]==lis_len)
9586
{
96-
mx = LIS[i];
97-
MX = SEQ[i];
87+
vLIS = SEQ[i];
9888
}
9989
}
10090

101-
for(auto x:MX)
102-
cout<<x<<" ";
103-
cout<<endl;
91+
return vLIS;
92+
}
93+
94+
int main()
95+
{
96+
optimizeIO();
97+
98+
int n;
99+
cin>>n;
100+
101+
vector<int> v(n);
102+
103+
for(int i=0; i<n; i++)
104+
cin>>v[i];
105+
106+
vector<int>ans = solveLIS(v);
107+
DBG(ans);
104108

105109
return 0;
106110
}
@@ -119,9 +123,9 @@ template <class T>
119123
ostream &operator <<(ostream &os, vector<T>&v)
120124
{
121125
os<<"[ ";
122-
for(int i=0; i<v.size(); i++)
126+
for(T i:v)
123127
{
124-
os<<v[i]<<" " ;
128+
os<<i<<" " ;
125129
}
126130
os<<" ]";
127131
return os;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
2+
/**
3+
4+
Longest Increasing Subsequnce (Optimized using Binary Search)
5+
-----------------------------
6+
7+
Complexity : O( NlogN )
8+
9+
Details : https://cp-algorithms.com/sequences/longest_increasing_subsequence.html
10+
11+
**/
12+
13+
/** Which of the favors of your Lord will you deny ? **/
14+
15+
#include<bits/stdc++.h>
16+
using namespace std;
17+
18+
#define LL long long
19+
#define PII pair<int,int>
20+
#define PLL pair<LL,LL>
21+
#define F first
22+
#define S second
23+
24+
#define ALL(x) (x).begin(), (x).end()
25+
#define READ freopen("alu.txt", "r", stdin)
26+
#define WRITE freopen("vorta.txt", "w", stdout)
27+
28+
#ifndef ONLINE_JUDGE
29+
#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl
30+
#else
31+
#define DBG(x)
32+
#define endl "\n"
33+
#endif
34+
35+
template<class T1, class T2>
36+
ostream &operator <<(ostream &os, pair<T1,T2>&p);
37+
template <class T>
38+
ostream &operator <<(ostream &os, vector<T>&v);
39+
template <class T>
40+
ostream &operator <<(ostream &os, set<T>&v);
41+
42+
inline void optimizeIO()
43+
{
44+
ios_base::sync_with_stdio(false);
45+
cin.tie(NULL);
46+
}
47+
48+
const int nmax = 2e5+7;
49+
const int INF = 1e9;
50+
51+
int solveLIS(vector<int> &v)
52+
{
53+
int n = v.size();
54+
55+
vector<int>d(n+1,INF); /// d[i] = element at which subsequence of length i terminates
56+
vector<int>LIS(n,1);
57+
d[0] = -INF;
58+
59+
for(int i=0;i<n;i++)
60+
{
61+
int len = upper_bound(ALL(d),v[i]) - d.begin(); /// finding the one and only correct position of the current element
62+
63+
if(v[i] > d[len-1] && v[i] < d[len]) /// if greater than previous element and a better option
64+
d[len] = v[i] , LIS[i] = len;
65+
}
66+
67+
return *max_element(ALL(LIS));
68+
}
69+
70+
int main()
71+
{
72+
optimizeIO();
73+
74+
int n;
75+
cin>>n;
76+
77+
vector<int>v(n);
78+
for(int i=0;i<n;i++)
79+
cin>>v[i];
80+
81+
int LIS_len = solveLIS(v);
82+
cout<<LIS_len<<endl;
83+
84+
return 0;
85+
}
86+
87+
/**
88+
10
89+
1 2 3 4 5 4 3 2 1 10
90+
**/
91+
92+
template<class T1, class T2>
93+
ostream &operator <<(ostream &os, pair<T1,T2>&p)
94+
{
95+
os<<"{"<<p.first<<", "<<p.second<<"} ";
96+
return os;
97+
}
98+
template <class T>
99+
ostream &operator <<(ostream &os, vector<T>&v)
100+
{
101+
os<<"[ ";
102+
for(T i:v)
103+
{
104+
os<<i<<" " ;
105+
}
106+
os<<" ]";
107+
return os;
108+
}
109+
110+
template <class T>
111+
ostream &operator <<(ostream &os, set<T>&v)
112+
{
113+
os<<"[ ";
114+
for(T i:v)
115+
{
116+
os<<i<<" ";
117+
}
118+
os<<" ]";
119+
return os;
120+
}
121+
122+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
2+
/**
3+
4+
LCIS(Longest Common Increasing Subsequence)
5+
===========================================
6+
7+
Straightforward DP : O(N^3)
8+
9+
**/
10+
11+
/** Which of the favors of your Lord will you deny ? **/
12+
13+
#include<bits/stdc++.h>
14+
using namespace std;
15+
16+
#define LL long long
17+
#define PII pair<int,int>
18+
#define PLL pair<LL,LL>
19+
#define F first
20+
#define S second
21+
22+
#define ALL(x) (x).begin(), (x).end()
23+
#define READ freopen("alu.txt", "r", stdin)
24+
#define WRITE freopen("vorta.txt", "w", stdout)
25+
26+
#ifndef ONLINE_JUDGE
27+
#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl
28+
#else
29+
#define DBG(x)
30+
#define endl "\n"
31+
#endif
32+
33+
template<class T1, class T2>
34+
ostream &operator <<(ostream &os, pair<T1,T2>&p);
35+
template <class T>
36+
ostream &operator <<(ostream &os, vector<T>&v);
37+
template <class T>
38+
ostream &operator <<(ostream &os, set<T>&v);
39+
40+
inline void optimizeIO()
41+
{
42+
ios_base::sync_with_stdio(false);
43+
cin.tie(NULL);
44+
}
45+
46+
const int nmax = 2e5+7;
47+
48+
template <class T>
49+
string to_str(T x)
50+
{
51+
stringstream ss;
52+
ss<<x;
53+
return ss.str();
54+
}
55+
56+
int n1,n2;
57+
vector<int>v1,v2;
58+
// int dp[505][505][505];
59+
60+
map<string,int>dp;
61+
62+
int LCIS(int i,int j,int prev)
63+
{
64+
if(i==n1 || j==n2) return 0;
65+
66+
string state = to_str(i) + "|" + to_str(j) + to_str(prev);
67+
68+
if(dp.find(state)!=dp.end()) return dp[state];
69+
int &ret = dp[state];
70+
71+
if(v1[i]==v2[j] && v1[i]>prev)
72+
{
73+
// DBG(i), DBG(v1[i]);
74+
ret = max(1 + LCIS(i + 1, j + 1, v1[i]), LCIS(i+1,j+1,prev));
75+
}
76+
else
77+
{
78+
ret = max(LCIS(i,j+1,prev), LCIS(i+1,j,prev));
79+
}
80+
81+
return ret;
82+
}
83+
84+
void solveLCIS(int i,int j,int prev,vector<int>&vLCS)
85+
{
86+
if(i==n1 || j==n2) return;
87+
88+
if(v1[i]==v2[j] && v1[i]>prev)
89+
{
90+
int way1 = 1 + LCIS(i + 1, j + 1, v1[i]) , way2 = LCIS(i+1,j+1,prev);
91+
92+
if(way1 > way2)
93+
{
94+
vLCS.push_back(v1[i]);
95+
return solveLCIS(i+1,j+1,v1[i],vLCS);
96+
}
97+
else
98+
{
99+
return solveLCIS(i+1,j+1,prev,vLCS);
100+
}
101+
}
102+
else
103+
{
104+
int way1 = LCIS(i+1,j,prev) , way2 = LCIS(i,j+1,prev);
105+
106+
if(way1 > way2) return solveLCIS(i+1,j,prev,vLCS);
107+
else return solveLCIS(i,j+1,prev,vLCS);
108+
}
109+
}
110+
111+
int main()
112+
{
113+
optimizeIO();
114+
115+
116+
cin>>n1;
117+
v1 = vector<int>(n1);
118+
for(int i=0;i<n1;i++)
119+
cin>>v1[i];
120+
121+
cin>>n2;
122+
v2 = vector<int>(n2);
123+
for(int i=0;i<n2;i++)
124+
cin>>v2[i];
125+
126+
int len = LCIS(0,0,-1);
127+
vector<int>vLCIS;
128+
solveLCIS(0,0,-1,vLCIS);
129+
130+
cout<<len<<endl;
131+
for(int x:vLCIS) cout<<x<<" ";
132+
cout<<endl;
133+
134+
135+
return 0;
136+
}
137+
138+
/**
139+
140+
**/
141+
142+
template<class T1, class T2>
143+
ostream &operator <<(ostream &os, pair<T1,T2>&p)
144+
{
145+
os<<"{"<<p.first<<", "<<p.second<<"} ";
146+
return os;
147+
}
148+
template <class T>
149+
ostream &operator <<(ostream &os, vector<T>&v)
150+
{
151+
os<<"[ ";
152+
for(T i:v)
153+
{
154+
os<<i<<" " ;
155+
}
156+
os<<" ]";
157+
return os;
158+
}
159+
160+
template <class T>
161+
ostream &operator <<(ostream &os, set<T>&v)
162+
{
163+
os<<"[ ";
164+
for(T i:v)
165+
{
166+
os<<i<<" ";
167+
}
168+
os<<" ]";
169+
return os;
170+
}
171+
172+
173+

0 commit comments

Comments
 (0)