# Generación de la pila a from random import sample, seed seed() n = 500 # número de elementos de la pila a = sample(range(1, n+1), n) print('a:', a) #https://stackoverflow.com/questions/40732489/how-to-print-the-actual-longest-increasing-subsequence-not-just-length # Dynamic programming Python implementation of LIS problem # lis returns length of the longest increasing subsequence # in arr of size n def lis(arr): n = len(arr) # Declare the list (array) for LIS and initialize LIS # values for all indexes lis = [1]*n prev = [0]*n for i in range(0, n): prev[i] = i # Compute optimized LIS values in bottom up manner for i in range (1 , n): for j in range(0 , i): if arr[i] > arr[j] and lis[i]< lis[j] + 1: lis[i] = lis[j]+1 prev[i] = j # Initialize maximum to 0 to get the maximum of all # LIS maximum = 0 idx = 0 # Pick maximum of all LIS values for i in range(n): if maximum < lis[i]: maximum = lis[i] idx = i seq = [arr[idx]] while idx != prev[idx]: idx = prev[idx] seq.append(arr[idx]) return (maximum, reversed(seq)) # end of lis function # Driver program to test above function #arr = [10, 22, 9, 33, 21, 50, 41, 60] a = [175, 38, 151, 167, 17, 168, 26, 244, 229, 364, 137, 54, 55, 245, 403, 319, 402, 67, 51, 479, 362, 224, 482, 63, 237, 277, 268, 201, 335, 490, 239, 307, 324, 387, 34, 274, 280, 365, 162, 372, 214, 261, 27, 376, 144, 337, 248, 363, 117, 70, 193, 200, 84, 284, 108, 410, 438, 81, 177, 104, 236, 174, 264, 145, 316, 430, 71, 487, 243, 425, 19, 183, 417, 57, 406, 282, 169, 392, 266, 20, 455, 356, 291, 209, 462, 48, 346, 257, 309, 64, 59, 32, 2, 153, 303, 115, 445, 269, 196, 77, 199, 208, 333, 166, 259, 413, 344, 468, 176, 95, 400, 8, 206, 469, 381, 310, 304, 139, 379, 132, 1, 428, 110, 179, 453, 123, 442, 188, 72, 101, 475, 477, 99, 427, 447, 382, 270, 96, 407, 255, 191, 408, 133, 273, 370, 343, 112, 74, 467, 56, 149, 249, 73, 189, 138, 234, 367, 198, 212, 279, 136, 124, 491, 478, 82, 496, 287, 262, 331, 434, 79, 24, 494, 374, 106, 61, 13, 401, 290, 474, 247, 242, 298, 296, 473, 352, 397, 223, 65, 360, 495, 230, 369, 465, 97, 69, 422, 492, 122, 454, 155, 321, 353, 121, 211, 357, 21, 165, 30, 336, 318, 171, 271, 254, 451, 143, 295, 131, 500, 233, 358, 16, 449, 231, 156, 240, 493, 466, 172, 164, 405, 263, 160, 253, 202, 476, 415, 302, 210, 464, 359, 23, 481, 332, 11, 347, 39, 418, 361, 260, 246, 42, 119, 440, 10, 44, 341, 293, 94, 312, 14, 85, 299, 68, 238, 256, 301, 354, 472, 107, 486, 157, 148, 125, 329, 424, 89, 311, 380, 36, 431, 154, 215, 393, 265, 109, 334, 102, 228, 159, 22, 52, 126, 459, 75, 272, 275, 432, 116, 141, 373, 135, 60, 294, 326, 426, 53, 5, 134, 297, 29, 267, 289, 389, 18, 391, 460, 484, 340, 180, 111, 195, 173, 470, 128, 92, 207, 366, 204, 350, 441, 58, 182, 120, 25, 377, 130, 147, 411, 163, 384, 305, 281, 190, 43, 222, 375, 203, 142, 300, 93, 320, 497, 439, 194, 338, 184, 140, 308, 12, 241, 323, 216, 150, 197, 456, 258, 88, 31, 409, 420, 435, 15, 219, 100, 317, 461, 429, 419, 3, 187, 46, 414, 66, 178, 444, 226, 416, 252, 87, 383, 181, 443, 345, 83, 342, 80, 62, 90, 385, 457, 448, 483, 49, 485, 50, 35, 152, 315, 386, 161, 251, 325, 227, 205, 45, 313, 185, 114, 446, 371, 330, 437, 37, 452, 327, 186, 76, 158, 368, 129, 118, 292, 232, 218, 398, 396, 423, 170, 314, 421, 6, 488, 235, 127, 286, 480, 433, 47, 28, 250, 322, 276, 489, 394, 349, 436, 33, 217, 463, 306, 471, 450, 404, 278, 225, 339, 399, 355, 351, 213, 9, 285, 98, 458, 146, 113, 103, 328, 221, 78, 91, 499, 220, 4, 498, 390, 388, 40, 412, 348, 378, 41, 288, 86, 7, 283, 192, 395, 105] arr = a ans = lis(arr) print("Length of lis is", ans[0]) print("The longest sequence is", ", ".join(str(x) for x in ans[1])) def lengthOfLIS(nums): if not nums: return 0 dp = [1] * len(nums) for i in range(1, len(nums)): for j in range(i): if nums[i] > nums[j]: dp[i] = max(dp[i], dp[j] + 1) return dp #max(dp) #a = [8,4,12,2,10,6,14,9,5,13,11,7,15] a = [175, 38, 151, 167, 17, 168, 26, 244, 229, 364, 137, 54, 55, 245, 403, 319, 402, 67, 51, 479, 362, 224, 482, 63, 237, 277, 268, 201, 335, 490, 239, 307, 324, 387, 34, 274, 280, 365, 162, 372, 214, 261, 27, 376, 144, 337, 248, 363, 117, 70, 193, 200, 84, 284, 108, 410, 438, 81, 177, 104, 236, 174, 264, 145, 316, 430, 71, 487, 243, 425, 19, 183, 417, 57, 406, 282, 169, 392, 266, 20, 455, 356, 291, 209, 462, 48, 346, 257, 309, 64, 59, 32, 2, 153, 303, 115, 445, 269, 196, 77, 199, 208, 333, 166, 259, 413, 344, 468, 176, 95, 400, 8, 206, 469, 381, 310, 304, 139, 379, 132, 1, 428, 110, 179, 453, 123, 442, 188, 72, 101, 475, 477, 99, 427, 447, 382, 270, 96, 407, 255, 191, 408, 133, 273, 370, 343, 112, 74, 467, 56, 149, 249, 73, 189, 138, 234, 367, 198, 212, 279, 136, 124, 491, 478, 82, 496, 287, 262, 331, 434, 79, 24, 494, 374, 106, 61, 13, 401, 290, 474, 247, 242, 298, 296, 473, 352, 397, 223, 65, 360, 495, 230, 369, 465, 97, 69, 422, 492, 122, 454, 155, 321, 353, 121, 211, 357, 21, 165, 30, 336, 318, 171, 271, 254, 451, 143, 295, 131, 500, 233, 358, 16, 449, 231, 156, 240, 493, 466, 172, 164, 405, 263, 160, 253, 202, 476, 415, 302, 210, 464, 359, 23, 481, 332, 11, 347, 39, 418, 361, 260, 246, 42, 119, 440, 10, 44, 341, 293, 94, 312, 14, 85, 299, 68, 238, 256, 301, 354, 472, 107, 486, 157, 148, 125, 329, 424, 89, 311, 380, 36, 431, 154, 215, 393, 265, 109, 334, 102, 228, 159, 22, 52, 126, 459, 75, 272, 275, 432, 116, 141, 373, 135, 60, 294, 326, 426, 53, 5, 134, 297, 29, 267, 289, 389, 18, 391, 460, 484, 340, 180, 111, 195, 173, 470, 128, 92, 207, 366, 204, 350, 441, 58, 182, 120, 25, 377, 130, 147, 411, 163, 384, 305, 281, 190, 43, 222, 375, 203, 142, 300, 93, 320, 497, 439, 194, 338, 184, 140, 308, 12, 241, 323, 216, 150, 197, 456, 258, 88, 31, 409, 420, 435, 15, 219, 100, 317, 461, 429, 419, 3, 187, 46, 414, 66, 178, 444, 226, 416, 252, 87, 383, 181, 443, 345, 83, 342, 80, 62, 90, 385, 457, 448, 483, 49, 485, 50, 35, 152, 315, 386, 161, 251, 325, 227, 205, 45, 313, 185, 114, 446, 371, 330, 437, 37, 452, 327, 186, 76, 158, 368, 129, 118, 292, 232, 218, 398, 396, 423, 170, 314, 421, 6, 488, 235, 127, 286, 480, 433, 47, 28, 250, 322, 276, 489, 394, 349, 436, 33, 217, 463, 306, 471, 450, 404, 278, 225, 339, 399, 355, 351, 213, 9, 285, 98, 458, 146, 113, 103, 328, 221, 78, 91, 499, 220, 4, 498, 390, 388, 40, 412, 348, 378, 41, 288, 86, 7, 283, 192, 395, 105] dp = lengthOfLIS(a) print(dp) print(f"La subsecuencia creciente más larga tiene {max(dp)} elementos.") def function(arr): n = len(arr) print("n:", n) # Declare the list (array) for LIS and initialize LIS # values for all indexes lis = [1]*n prev = list(range(n)) # una lista con los index # Compute optimized LIS values in bottom up manner for i in range (1, n): for j in range(i): if arr[i] > arr[j] and lis[i] < lis[j] + 1: lis[i] = lis[j]+1 prev[i] = j # Initialize maximum to 0 to get the maximum of all # LIS maximum = 0 idx = 0 # Pick maximum of all LIS values for i in range(n): if maximum < lis[i]: maximum = lis[i] idx = i seq = [arr[idx]] while idx != prev[idx]: idx = prev[idx] seq.append(arr[idx]) return (maximum, reversed(seq)) a = [175, 38, 151, 167, 17, 168, 26, 244, 229, 364, 137, 54, 55, 245, 403, 319, 402, 67, 51, 479, 362, 224, 482, 63, 237, 277, 268, 201, 335, 490, 239, 307, 324, 387, 34, 274, 280, 365, 162, 372, 214, 261, 27, 376, 144, 337, 248, 363, 117, 70, 193, 200, 84, 284, 108, 410, 438, 81, 177, 104, 236, 174, 264, 145, 316, 430, 71, 487, 243, 425, 19, 183, 417, 57, 406, 282, 169, 392, 266, 20, 455, 356, 291, 209, 462, 48, 346, 257, 309, 64, 59, 32, 2, 153, 303, 115, 445, 269, 196, 77, 199, 208, 333, 166, 259, 413, 344, 468, 176, 95, 400, 8, 206, 469, 381, 310, 304, 139, 379, 132, 1, 428, 110, 179, 453, 123, 442, 188, 72, 101, 475, 477, 99, 427, 447, 382, 270, 96, 407, 255, 191, 408, 133, 273, 370, 343, 112, 74, 467, 56, 149, 249, 73, 189, 138, 234, 367, 198, 212, 279, 136, 124, 491, 478, 82, 496, 287, 262, 331, 434, 79, 24, 494, 374, 106, 61, 13, 401, 290, 474, 247, 242, 298, 296, 473, 352, 397, 223, 65, 360, 495, 230, 369, 465, 97, 69, 422, 492, 122, 454, 155, 321, 353, 121, 211, 357, 21, 165, 30, 336, 318, 171, 271, 254, 451, 143, 295, 131, 500, 233, 358, 16, 449, 231, 156, 240, 493, 466, 172, 164, 405, 263, 160, 253, 202, 476, 415, 302, 210, 464, 359, 23, 481, 332, 11, 347, 39, 418, 361, 260, 246, 42, 119, 440, 10, 44, 341, 293, 94, 312, 14, 85, 299, 68, 238, 256, 301, 354, 472, 107, 486, 157, 148, 125, 329, 424, 89, 311, 380, 36, 431, 154, 215, 393, 265, 109, 334, 102, 228, 159, 22, 52, 126, 459, 75, 272, 275, 432, 116, 141, 373, 135, 60, 294, 326, 426, 53, 5, 134, 297, 29, 267, 289, 389, 18, 391, 460, 484, 340, 180, 111, 195, 173, 470, 128, 92, 207, 366, 204, 350, 441, 58, 182, 120, 25, 377, 130, 147, 411, 163, 384, 305, 281, 190, 43, 222, 375, 203, 142, 300, 93, 320, 497, 439, 194, 338, 184, 140, 308, 12, 241, 323, 216, 150, 197, 456, 258, 88, 31, 409, 420, 435, 15, 219, 100, 317, 461, 429, 419, 3, 187, 46, 414, 66, 178, 444, 226, 416, 252, 87, 383, 181, 443, 345, 83, 342, 80, 62, 90, 385, 457, 448, 483, 49, 485, 50, 35, 152, 315, 386, 161, 251, 325, 227, 205, 45, 313, 185, 114, 446, 371, 330, 437, 37, 452, 327, 186, 76, 158, 368, 129, 118, 292, 232, 218, 398, 396, 423, 170, 314, 421, 6, 488, 235, 127, 286, 480, 433, 47, 28, 250, 322, 276, 489, 394, 349, 436, 33, 217, 463, 306, 471, 450, 404, 278, 225, 339, 399, 355, 351, 213, 9, 285, 98, 458, 146, 113, 103, 328, 221, 78, 91, 499, 220, 4, 498, 390, 388, 40, 412, 348, 378, 41, 288, 86, 7, 283, 192, 395, 105] arr = a ans = function(arr) print("Length of lis is", ans[0]) print("The longest sequence is", ", ".join(str(x) for x in ans[1]))