Skip to content

Commit f566fdb

Browse files
committed
Learning DP Day 9
LIS and Kadane Algorithm
1 parent 7ed466a commit f566fdb

File tree

13 files changed

+658
-30
lines changed

13 files changed

+658
-30
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define F first
4+
#define S second
5+
#define PB push_back
6+
#define PF push_front
7+
#define P push
8+
#define INC(i,a,b) for (ll i = a; i <= b; i++)
9+
#define DEC(i,b,a) for (ll i = b; i >= a ; i--)
10+
#define inf LLONG_MAX
11+
#define neginf LLONG_MIN
12+
#define mod 1000000007
13+
#define eps 1e-9
14+
typedef ostringstream OS ;
15+
typedef stringstream SS ;
16+
typedef long long ll ;
17+
typedef unsigned long long ull;
18+
typedef pair < ll , ll > PLL ;
19+
typedef pair < char,ll > PCL ;
20+
typedef deque < double > DD ;
21+
typedef deque < PCL > DCL ;
22+
typedef deque < ll > DL ;
23+
typedef deque < PLL > DLL ;
24+
typedef deque < char > DC ;
25+
typedef deque < string > DS ;
26+
typedef vector < double > VD;
27+
typedef vector < PCL > VCL ;
28+
typedef vector < ll > VL;
29+
typedef vector < PLL > VLL ;
30+
typedef vector < char > VC ;
31+
typedef vector < string > VS ;
32+
typedef map < ll ,ll > MLL ;
33+
typedef map < char,ll > MCL;
34+
typedef map < ll,char > MLC;
35+
typedef map < string,ll> MSL;
36+
typedef priority_queue < PLL > PQLL ;
37+
typedef priority_queue < ll > PQL ;
38+
typedef stack < ll > SKL ;
39+
typedef stack < PLL > SKLL ;
40+
typedef queue < ll > QL ;
41+
typedef queue < PLL > QLL ;
42+
typedef set < ll > SL ;
43+
typedef set < PLL > SLL ;
44+
typedef set < char > SC ;
45+
typedef vector <VL> graph;
46+
47+
string numtostr(ll n) {
48+
OS str1 ;
49+
str1 << n ;
50+
return str1.str();
51+
}
52+
ll strtonum(string s) {
53+
ll x ;
54+
SS str1(s);
55+
str1 >> x ;
56+
return x ;
57+
}
58+
ll GCD(ll a, ll b) {
59+
if ( b == 0 ) return a ;
60+
else return GCD(b,a%b);
61+
}
62+
ll LCM(ll a , ll b) {
63+
ll gcd = GCD(a,b);
64+
return (a/gcd)*b ;
65+
}
66+
vector < pair < ll,PLL> > boxes ;
67+
bool myfunc(pair < ll,PLL> x , pair < ll,PLL> y) {
68+
return ( (x.S.S * x.S.F) > (y.S.S * y.S.F)) ;
69+
}
70+
71+
int main() {
72+
ios::sync_with_stdio(0);
73+
cin.tie(0);
74+
ll noOfBoxes ;
75+
cin >> noOfBoxes ;
76+
INC(i,1,noOfBoxes) {
77+
ll d1,d2,d3 ;
78+
cin >> d1 >> d2 >> d3 ;
79+
boxes.PB({d1,{max(d2,d3),min(d2,d3)}});
80+
boxes.PB({d2,{max(d1,d3),min(d1,d3)}});
81+
boxes.PB({d3,{max(d1,d2),min(d1,d2)}});
82+
}
83+
sort(boxes.begin(),boxes.end(),myfunc);
84+
VL lisBoxes ;
85+
lisBoxes.resize(boxes.size()+5,0);
86+
ll maxHeight = -1 ;
87+
INC(i,0,boxes.size()-1) {
88+
lisBoxes[i] = boxes[i].F ;
89+
maxHeight = max(maxHeight,lisBoxes[i]);
90+
}
91+
92+
INC(i,1,boxes.size()-1) {
93+
INC(j,0,i-1) {
94+
if ( boxes[i].S.F < boxes[j].S.F && boxes[i].S.S < boxes[j].S.S && lisBoxes[j]+boxes[i].F > lisBoxes[i]) {
95+
lisBoxes[i] = lisBoxes[j]+boxes[i].F ;
96+
maxHeight = max(maxHeight,lisBoxes[i]);
97+
}
98+
}
99+
}
100+
cout << "Maximum Height is : " << maxHeight << endl;
101+
102+
return 0 ;
103+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Explanation
2+
3+
[Box Stacking Problem](https://www.youtube.com/watch?v=kLucR6-Q0GA&list=PLEJXowNB4kPxBwaXtRO1qFLpCzF75DYrS&index=25)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define F first
4+
#define S second
5+
#define PB push_back
6+
#define P push
7+
#define INC(i,a,b) for (ll i = a; i <= b; i++)
8+
#define DEC(i,b,a) for (ll i = b; i >= a ; i--)
9+
#define inf 999999999
10+
#define neginf -999999999
11+
typedef ostringstream OS ;
12+
typedef stringstream SS ;
13+
typedef long long ll ;
14+
typedef pair < ll , ll > PLL ;
15+
typedef pair < char,ll > PCL ;
16+
typedef vector < double > VDL;
17+
typedef vector < PCL > VCL ;
18+
typedef vector < ll > VL;
19+
typedef vector < PLL > VLL ;
20+
typedef vector < char > VC ;
21+
typedef map < ll ,ll > MLL ;
22+
typedef map < char,ll > MCL;
23+
typedef priority_queue < PLL > PQLL ;
24+
typedef priority_queue < ll > PQL ;
25+
typedef stack < ll > SKL ;
26+
typedef stack < PLL > SKLL ;
27+
typedef set < ll > SL ;
28+
typedef set < PLL > SLL ;
29+
typedef set < char > SC ;
30+
31+
string numtostr(ll n) {
32+
OS str1 ;
33+
str1 << n ;
34+
return str1.str();
35+
}
36+
ll strtonum(string s) {
37+
ll x ;
38+
SS str1(s);
39+
str1 >> x ;
40+
return x ;
41+
}
42+
ll GCD(ll a, ll b) {
43+
if ( b == 0 ) return a ;
44+
else return GCD(b,a%b);
45+
}
46+
ll LCM(ll a , ll b) {
47+
ll gcd = GCD(a,b);
48+
return (a*b)/gcd ;
49+
}
50+
51+
int main() {
52+
ios::sync_with_stdio(0);
53+
cin.tie(0);
54+
ll n ;
55+
cin >> n ;
56+
VL v ;
57+
INC(i,0,n-1) {
58+
ll a ;
59+
cin >> a ;
60+
v.PB(a);
61+
}
62+
pair < ll ,PLL > sum = {0,{0,0}};
63+
pair < ll ,PLL > best = {neginf,{-1,-1}}; /* you can use best as {0,{0,0}} if it is allowed to
64+
take empty subarray */
65+
66+
INC(i,0,n-1) {
67+
if ( v[i] >= sum.F + v[i]) {
68+
sum.S.F = i ;
69+
sum.S.S = i ;
70+
sum.F = v[i] ;
71+
if ( v[i] > best.F) {
72+
best.S.F = sum.S.F ;
73+
best.S.S = sum.S.S ;
74+
best.F = sum.F ;
75+
}
76+
}
77+
else if ( v[i]+sum.F > v[i]) {
78+
sum.S.S = i ;
79+
if ( v[i]+sum.F > best.F) {
80+
best.S.F = sum.S.F ;
81+
best.S.S = sum.S.S ;
82+
best.F = v[i] + sum.F ;
83+
}
84+
sum.F = v[i]+sum.F ;
85+
}
86+
}
87+
88+
cout << best.F << " " << best.S.F+1 << " " << best.S.S+1 << endl;
89+
90+
return 0 ;
91+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Explanation
2+
3+
Some modification so that we can print the starting and ending index of the subarray which holds maximum sum .
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define F first
4+
#define S second
5+
#define PB push_back
6+
#define PF push_front
7+
#define P push
8+
#define INC(i,a,b) for (ll i = a; i <= b; i++)
9+
#define DEC(i,b,a) for (ll i = b; i >= a ; i--)
10+
#define inf LLONG_MAX
11+
#define neginf LLONG_MIN
12+
#define mod 1000000007
13+
#define eps 1e-9
14+
typedef ostringstream OS ;
15+
typedef stringstream SS ;
16+
typedef long long ll ;
17+
typedef unsigned long long ull;
18+
typedef pair < ll , ll > PLL ;
19+
typedef pair < char,ll > PCL ;
20+
typedef deque < double > DD ;
21+
typedef deque < PCL > DCL ;
22+
typedef deque < ll > DL ;
23+
typedef deque < PLL > DLL ;
24+
typedef deque < char > DC ;
25+
typedef deque < string > DS ;
26+
typedef vector < double > VD;
27+
typedef vector < PCL > VCL ;
28+
typedef vector < ll > VL;
29+
typedef vector < PLL > VLL ;
30+
typedef vector < char > VC ;
31+
typedef vector < string > VS ;
32+
typedef map < ll ,ll > MLL ;
33+
typedef map < char,ll > MCL;
34+
typedef map < ll,char > MLC;
35+
typedef map < string,ll> MSL;
36+
typedef priority_queue < PLL > PQLL ;
37+
typedef priority_queue < ll > PQL ;
38+
typedef stack < ll > SKL ;
39+
typedef stack < PLL > SKLL ;
40+
typedef queue < ll > QL ;
41+
typedef queue < PLL > QLL ;
42+
typedef set < ll > SL ;
43+
typedef set < PLL > SLL ;
44+
typedef set < char > SC ;
45+
typedef vector <VL> graph;
46+
47+
string numtostr(ll n) {
48+
OS str1 ;
49+
str1 << n ;
50+
return str1.str();
51+
}
52+
ll strtonum(string s) {
53+
ll x ;
54+
SS str1(s);
55+
str1 >> x ;
56+
return x ;
57+
}
58+
ll GCD(ll a, ll b) {
59+
if ( b == 0 ) return a ;
60+
else return GCD(b,a%b);
61+
}
62+
ll LCM(ll a , ll b) {
63+
ll gcd = GCD(a,b);
64+
return (a/gcd)*b ;
65+
}
66+
67+
int main() {
68+
ios::sync_with_stdio(0);
69+
cin.tie(0);
70+
ll n ;
71+
cin >> n ;
72+
VL v ;
73+
v.resize(n+5,0);
74+
INC(i,1,n) cin >> v[i] ;
75+
76+
ll maxSum = neginf ;
77+
ll currSum = 0 ;
78+
79+
INC(i,1,n) {
80+
currSum = max(currSum+v[i],v[i]);
81+
maxSum = max(maxSum,currSum);
82+
}
83+
84+
cout << maxSum << endl;
85+
return 0 ;
86+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Explanation
2+
3+
[Kadane Algorithm](https://www.geeksforgeeks.org/largest-sum-contiguous-subarray/)

0 commit comments

Comments
 (0)