File tree Expand file tree Collapse file tree 4 files changed +183
-0
lines changed
DP/Maximum Sum Increasing Subsequence Expand file tree Collapse file tree 4 files changed +183
-0
lines changed Original file line number Diff line number Diff line change
1
+ // sol.cpp : Defines the entry point for the console application.
2
+ //
3
+
4
+ // #include "stdafx.h"
5
+ #include < bits/stdc++.h>
6
+ /* Dynamic Programming solution to construct Maximum Sum Increasing Subsequence */
7
+
8
+ #include < cmath>
9
+ #include < cstdio>
10
+ #include < fstream>
11
+ #include < string>
12
+ #include < iostream>
13
+ #include < vector>
14
+ #include < utility>
15
+ #include < algorithm>
16
+ using namespace std ;
17
+
18
+
19
+ typedef vector<int > VI;
20
+
21
+ // Function to construct Maximum Sum Increasing Subsequence
22
+ VI calcMaxSumIS (VI arr)
23
+ {
24
+ int n = arr.size ();
25
+
26
+ // L[i] stores the Maximum Sum Increasing Subsequence that ends with arr[i]
27
+ vector<VI> L (n);
28
+
29
+ // msis[i] stores the Maximum Sum of Increasing Subsequence that
30
+ // ends with arr[i]
31
+ VI msis (n);
32
+
33
+ // L[0] is equal to arr[0]
34
+ L[0 ].push_back (arr[0 ]);
35
+ msis[0 ] = arr[0 ];
36
+
37
+ // start from index 1
38
+ for (int i = 1 ; i < n; i++)
39
+ {
40
+ // for every j less than i
41
+ for (int j = 0 ; j < i; j++)
42
+ {
43
+ // L[i] = {MaxSum(L[j])} + arr[i] where j < i and arr[j] < arr[i]
44
+ if ((arr[i] > arr[j]) && (msis[i] < msis[j]))
45
+ {
46
+ L[i] = L[j];
47
+ msis[i] = msis[j];
48
+ }
49
+ }
50
+ // L[i] ends with arr[i]
51
+ L[i].push_back (arr[i]);
52
+ msis[i] += arr[i];
53
+
54
+ // L[i] now stores Maximum Sum Increasing Subsequence of arr[0..i]
55
+ // that ends with arr[i]
56
+ }
57
+
58
+ int maxIndex = 0 ;
59
+ int max = msis[0 ];
60
+
61
+ // find max
62
+ for (int i = 1 ; i < n; i++)
63
+ if (msis[i] > max)
64
+ {
65
+ max = msis[i];
66
+ maxIndex = i;
67
+ }
68
+
69
+ // max will contain result
70
+ return L[maxIndex];
71
+ }
72
+
73
+ int main () {
74
+ ifstream infile (" test1.txt" );
75
+ if (infile)
76
+ {
77
+ VI arr;
78
+ string s;
79
+ int a;
80
+ while (true ) {
81
+ try {
82
+ getline (infile,s);
83
+ a = stoi (s);
84
+ }
85
+ catch (...) {
86
+ break ;
87
+ }
88
+ arr.push_back (a);
89
+ }
90
+ auto res= calcMaxSumIS (arr);
91
+ for (auto r : res)
92
+ cout << r << " " ;
93
+ cout << endl;
94
+ }
95
+ }
Original file line number Diff line number Diff line change
1
+ #!/bin/python3
2
+ import sys
3
+ from copy import copy
4
+
5
+ fstd = "test2"
6
+ sys .stdin = open ('%s.txt' % fstd , 'r' )
7
+ input = sys .stdin .readline
8
+
9
+ # DP based Python
10
+ # implementation of Maximum Sum
11
+ # Increasing Subsequence (MSIS)
12
+ # problem
13
+
14
+ # constructMaxSumIS() returns the tupple:
15
+ # ( max sum of increasing subsequence, found subsequence )
16
+ def constructMaxSumIS (arr ):
17
+ N = len (arr )
18
+
19
+ # L[i] stores the Maximum Sum Increasing Subsequence that ends with arr[i]
20
+ L = [ [] for _ in range (N ) ]
21
+ # L[0] is equal to arr[0]
22
+ L [0 ].append (arr [0 ])
23
+
24
+ # msis[i] stores the Maximum Sum of Increasing Subsequence that
25
+ # ends with arr[i]
26
+ msis = [0 ]* N
27
+ # Initialize msis values
28
+ msis [0 ]= arr [0 ]
29
+
30
+ # Compute maximum sum
31
+ # values in bottom up manner
32
+ for i in range (1 , N ):
33
+ for j in range (i ):
34
+ if (arr [i ] > arr [j ]) and (msis [i ] < msis [j ]):
35
+ msis [i ] = msis [j ]
36
+ L [i ] = copy (L [j ])
37
+ L [i ].append (arr [i ])
38
+ msis [i ]+= arr [i ]
39
+
40
+ # Find max
41
+ maxIndex = max (range (N ), key = lambda i :msis [i ])
42
+ return (msis [maxIndex ],L [maxIndex ])
43
+
44
+ if __name__ == "__main__" :
45
+ print ("Input sequence:" )
46
+ A = []
47
+ while (True ):
48
+ line = input ().strip ()
49
+ try :
50
+ num = int (line )
51
+ except ValueError :
52
+ break
53
+ A .append (num )
54
+ print (A )
55
+ res = constructMaxSumIS (A )
56
+ print ("Max sum is {}" .format (res [0 ]))
57
+ print ("Found sequence is: {} " .format (res [1 ]))
58
+
Original file line number Diff line number Diff line change
1
+ 1
2
+ 101
3
+ 2
4
+ 3
5
+ 100
6
+ 4
7
+ 5
8
+
9
+
10
+ Result: 1, 2, 3, 100
Original file line number Diff line number Diff line change
1
+ 1
2
+ 101
3
+ 2
4
+ 3
5
+ 100
6
+ 4
7
+ 5
8
+ 10
9
+ 20
10
+ 15
11
+ 40
12
+ 20
13
+ 90
14
+ 100
15
+ 10
16
+ 40
17
+ 50
18
+ 60
19
+
20
+ Result: 1 2 3 4 5 10 20 40 90 100
You can’t perform that action at this time.
0 commit comments