Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int n;
- int niza[105];
- int dp[105];
- int LIS(int i) {
- if(dp[i] != -1) {
- return dp[i];
- }
- int longest_sequence = 0;
- for(int j = i + 1; j < n; j++) {
- if(niza[i] <= niza[j]) {
- longest_sequence = max(longest_sequence, LIS(j) + 1);
- }
- }
- return dp[i] = longest_sequence;
- }
- int main() {
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> niza[i];
- dp[i] = -1; // znaci deka nemame nisto presmetano vo dinamickoto programiranje
- }
- int longest_sequence = 0;
- for(int i = 0; i < n; i++) {
- longest_sequence = max(longest_sequence, LIS(i) + 1);
- }
- cout << longest_sequence << endl;
- }
- // 4 3 1 3 5 6 2 10 11
- // LIS(0) = max(LIS(4) + 1, LIS(5) + 1, LIS(7) + 1, LIS(8) + 1) = 4
- // LIS(4) = max(LIS(5) + 1, LIS(7) + 1, LIS(8) + 1) = max(3, 2, 1) = 3
- // LIS(5) = max(LIS(7) + 1, LIS(8) + 1) = max(2, 1) = 2
- // LIS(7) = max(LIS(8) + 1) = 0 + 1 = 1
- // LIS(8) = 0
- // LIS(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement