@@ -5,40 +5,34 @@ Problem : Longest Increasing Subsequence
55
66**/
77
8- /* *Which of the favors of your Lord will you deny ?**/
8+
9+ /* * Which of the favors of your Lord will you deny ? **/
910
1011#include < bits/stdc++.h>
1112using namespace std ;
1213
1314#define LL long long
1415#define PII pair<int ,int >
1516#define PLL pair<LL,LL>
16- #define MP make_pair
1717#define F first
1818#define S second
19- #define INF INT_MAX
20-
21- #define ALL (x ) (x).begin(), (x).end()
22- #define DBG (x ) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
2319
24- #include < ext/pb_ds/assoc_container.hpp >
25- #include < ext/pb_ds/tree_policy.hpp >
26- using namespace __gnu_pbds ;
20+ #define ALL ( x ) (x).begin(), (x).end()
21+ #define READ freopen ( " alu.txt " , " r " , stdin)
22+ # define WRITE freopen ( " vorta.txt " , " w " , stdout)
2723
28- template <class TIn >
29- using indexed_set = tree<
30- TIn, null_type, less<TIn>,
31- rb_tree_tag, tree_order_statistics_node_update>;
24+ #ifndef ONLINE_JUDGE
25+ #define DBG (x ) cout << __LINE__ << " says: " << #x << " = " << (x) << endl
26+ #else
27+ #define DBG (x )
28+ #endif
3229
33- /*
34- PBDS
35- -------------------------------------------------
36- 1) insert(value)
37- 2) erase(value)
38- 3) order_of_key(value) // 0 based indexing
39- 4) *find_by_order(position) // 0 based indexing
40-
41- */
30+ template <class T1 , class T2 >
31+ ostream &operator <<(ostream &os, pair<T1,T2>&p);
32+ template <class T >
33+ ostream &operator <<(ostream &os, vector<T>&v);
34+ template <class T >
35+ ostream &operator <<(ostream &os, set<T>&v);
4236
4337inline void optimizeIO ()
4438{
@@ -49,60 +43,100 @@ inline void optimizeIO()
4943const int nmax = 2e5 +7 ;
5044const LL LINF = 1e17 ;
5145
52- string to_str (LL x)
53- {
54- stringstream ss;
55- ss<<x;
56- return ss.str ();
57- }
58-
59- // bool cmp(const PII &A,const PII &B)
60- // {
61- //
62- // }
63-
6446/* * 1 based indexing **/
6547
6648/* *
49+
6750LIS[i] = LIS ending at index i
51+
6852**/
69- int LIS[nmax];
70- vector<int >SEQ[nmax];
7153
7254int main ()
7355{
7456 optimizeIO ();
7557
76- vector<int >ara = {-1 ,6 ,2 ,5 ,1 ,7 ,4 ,8 ,3 };
77- int n=ara.size ()-1 ;
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];
65+
66+ vector<int >LIS (n,1 ); /* * length of longest increasing sequence ending at i **/
67+ vector<vector<int >>SEQ (n); /* * longest increasing sequence ending at i **/
7868
79- for (int i=1 ;i<= n;i++)
69+ for (int i=0 ;i<n;i++)
8070 {
81- LIS[i] = 1 ;
82- for (int j=1 ;j<=i;j++)
71+ for (int j=0 ;j<i;j++)
8372 {
84- if (ara [i]>ara [j])
73+ if (v [i]>v [j])
8574 {
86- LIS[i] = max (LIS[i],LIS[j]+1 ); /* * **/
87- if (SEQ[j].size ()+1 > SEQ[i].size ()) SEQ[i] = SEQ[j];
75+ if (LIS[j]+1 > LIS[i])
76+ {
77+ LIS[i] = LIS[j]+1 ;
78+ SEQ[i] = SEQ[j];
79+ }
8880 }
8981 }
90- SEQ[i].push_back (ara[i]);
82+ SEQ[i].push_back (v[i]);
83+ DBG (SEQ[i]);
9184 }
9285
93- int lis_len = *max_element (LIS+1 ,LIS+n+1 );
86+ int lis_len = *max_element (ALL (LIS));
87+ DBG (lis_len);
9488
95- vector<int >lis = SEQ[1 ];
89+ int mx = 0 ;
90+ vector<int >MX;
9691
97- for (int i=2 ;i<=n;i++)
98- if (SEQ[i].size ()>lis.size ())
99- lis = SEQ[i];
92+ for (int i=0 ; i<n; i++)
93+ {
94+ if (LIS[i]>mx)
95+ {
96+ mx = LIS[i];
97+ MX = SEQ[i];
98+ }
99+ }
100100
101- for (auto x:lis )
101+ for (auto x:MX )
102102 cout<<x<<" " ;
103103 cout<<endl;
104104
105105 return 0 ;
106106}
107+ /* *
108+ 5
109+ 1 6 2 3 5
110+ **/
111+
112+ template <class T1 , class T2 >
113+ ostream &operator <<(ostream &os, pair<T1,T2>&p)
114+ {
115+ os<<" {" <<p.first <<" , " <<p.second <<" } " ;
116+ return os;
117+ }
118+ template <class T >
119+ ostream &operator <<(ostream &os, vector<T>&v)
120+ {
121+ os<<" [ " ;
122+ for (int i=0 ; i<v.size (); i++)
123+ {
124+ os<<v[i]<<" " ;
125+ }
126+ os<<" ]" ;
127+ return os;
128+ }
129+
130+ template <class T >
131+ ostream &operator <<(ostream &os, set<T>&v)
132+ {
133+ os<<" [ " ;
134+ for (T i:v)
135+ {
136+ os<<i<<" " ;
137+ }
138+ os<<" ]" ;
139+ return os;
140+ }
107141
108142
0 commit comments