{"id":18741,"date":"2022-11-01T20:19:55","date_gmt":"2022-11-01T14:49:55","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/"},"modified":"2022-11-01T20:19:55","modified_gmt":"2022-11-01T14:49:55","slug":"solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/","title":{"rendered":"[Solved] Longest monotonic subsequence algorithm NOT longest increasing algorithm"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-20002948\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"20002948\" data-parentid=\"19975276\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>try this one:<\/p>\n<pre><code>import java.io.BufferedReader;\nimport java.io.InputStreamReader;\n\npublic class Rozwiazanie {\n    public static void main(String[] args) {\n        try {\n            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\n            String[] splittedLinia = br.readLine().split((char) 32 + \"\");\/\/moglaby byc \" \" ale tak na wszelki wypadek nie ma chuja zeby sie popierdolilo teraz nawet na linuxie\n            int aktualnyWyraz = Integer.parseInt(splittedLinia[0]);\/\/uwaga jakby cos nie gralo w sprawdzarce zmien typ na long\n            int poprzedniWyraz = 0; \n\n            long podsumaRosnaca = aktualnyWyraz;\n            long podsumaSpadajaca = aktualnyWyraz;\n\n            int dlugoscRosnaca = 1;\n            int dlugoscSpadajaca = 1;\n\n            int maxDlugosc = 1;\n            long maxPodsuma = aktualnyWyraz;\n\n            int czyRosnacy = 0; \/\/ 0 -- nie znane (jezeli w poczatku wszystkie liczby sa rowne), 1 -- rosnacy, -1 -- spadajacy\n            boolean rowny = false;\n            System.out.println(\"akt: \" + aktualnyWyraz + \" dlR: \" + dlugoscRosnaca + \" podsumaR: \" + podsumaRosnaca + \" dlP: \" + dlugoscSpadajaca + \" podsumaP: \" + podsumaSpadajaca);\n\n            for (int i = 1; i &lt; splittedLinia.length; i++) {\n                poprzedniWyraz = aktualnyWyraz;\n                aktualnyWyraz = Integer.parseInt(splittedLinia[i]);\n                if (aktualnyWyraz == poprzedniWyraz) {\n                    podsumaRosnaca += aktualnyWyraz;\n                    podsumaSpadajaca += aktualnyWyraz;\n                    dlugoscRosnaca++;\n                    dlugoscSpadajaca++;\n                    rowny = true;\n                } else { \/\/ rozne liczby\n                    if (aktualnyWyraz &gt; poprzedniWyraz) { \/\/ rosnie\n                        podsumaRosnaca += aktualnyWyraz;\n                        dlugoscRosnaca++;      \n\n                        if (rowny) {\n                            dlugoscSpadajaca = 1;\n                            podsumaSpadajaca = 0;\n                            rowny = false;\n                        }       \n                        if (czyRosnacy &lt; 0) {\n                            if (dlugoscSpadajaca &gt; maxDlugosc) {\n                                maxDlugosc = dlugoscSpadajaca;\n                                maxPodsuma = podsumaSpadajaca;\n                            }\n                            podsumaSpadajaca = 0;\n                            dlugoscSpadajaca = 1;\n                        }\n                        czyRosnacy = 1;   \n                    } else { \/\/ spada\n                        podsumaSpadajaca += aktualnyWyraz;\n                        dlugoscSpadajaca++; \n\n                        if (rowny) {\n                            dlugoscRosnaca = 1;\n                            podsumaRosnaca = 0;\n                            rowny = false;\n                        }\n                        if (czyRosnacy == 1) {\n                            if (dlugoscRosnaca &gt; maxDlugosc) {\n                                maxDlugosc = dlugoscRosnaca;\n                                maxPodsuma = podsumaRosnaca;\n                            }\n                            podsumaRosnaca = 0;\n                            dlugoscRosnaca = 1;\n                        }  \n                        czyRosnacy = -1;\n                    }\n\n                }\n                System.out.println(\"akt: \" + aktualnyWyraz + \" dlR: \" + dlugoscRosnaca + \" podsumaR: \" + podsumaRosnaca + \" dlP: \" + dlugoscSpadajaca + \" podsumaP: \" + podsumaSpadajaca);\n            }\n\n\n            System.out.println(\"maxDlugosc \" + maxDlugosc + \" maxPodsuma \" + maxPodsuma);\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/code><\/pre>\n<p>what I had to change:<\/p>\n<ul>\n<li>you need a counter [dlugosc] (+ sum [podsuma]) for ascending<br \/>\n[rosnacy] and descending [spadajacy] values, as you need to count<br \/>\nboth when the values are the same [rowny]. <\/li>\n<li>I changed boolean &#8220;rowny&#8221;<br \/>\n[equal] to int, as I thought that there are 3 values possible:<br \/>\nascending, descending or unknown. If there are several equal values<br \/>\nat the beginning, there&#8217;s all the time &#8220;unknown&#8221;. <\/li>\n<li>this program needs at least one number, but therefore you don&#8217;t need to check in every<br \/>\niteration of the loop whether i == 0 or not. <\/li>\n<li>Going through the<br \/>\nnumbers, I have to check several things, most important is, whether<br \/>\nthe actual value [aktualnyWyraz] and the last used value<br \/>\n[poprzedniWyraz] are the same (then all counters and sums have to be<br \/>\nchanged) or different. Different can mean ascending or descending, so<br \/>\nwe have to increment the related counters and sum up the related sum.<br \/>\nWhat happens the &#8220;opposite&#8221; variables? Well, we need to check whether<br \/>\nthe counter is bigger then the maximum one (so maybe these values are<br \/>\nuseful for the result) and then set them back to their start.<\/li>\n<\/ul>\n<\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Longest monotonic subsequence algorithm NOT longest increasing algorithm <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] try this one: import java.io.BufferedReader; import java.io.InputStreamReader; public class Rozwiazanie { public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] splittedLinia = br.readLine().split((char) 32 + &#8220;&#8221;);\/\/moglaby byc &#8221; &#8221; ale tak na wszelki wypadek nie ma chuja zeby sie popierdolilo teraz nawet na linuxie int aktualnyWyraz = Integer.parseInt(splittedLinia[0]);\/\/uwaga &#8230; <a title=\"[Solved] Longest monotonic subsequence algorithm NOT longest increasing algorithm\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/\" aria-label=\"More on [Solved] Longest monotonic subsequence algorithm NOT longest increasing algorithm\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[323,1060,2058],"class_list":["post-18741","post","type-post","status-publish","format-standard","hentry","category-solved","tag-java","tag-sequence","tag-subset"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Longest monotonic subsequence algorithm NOT longest increasing algorithm - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Longest monotonic subsequence algorithm NOT longest increasing algorithm - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] try this one: import java.io.BufferedReader; import java.io.InputStreamReader; public class Rozwiazanie { public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] splittedLinia = br.readLine().split((char) 32 + &quot;&quot;);\/\/moglaby byc &quot; &quot; ale tak na wszelki wypadek nie ma chuja zeby sie popierdolilo teraz nawet na linuxie int aktualnyWyraz = Integer.parseInt(splittedLinia[0]);\/\/uwaga ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-01T14:49:55+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Longest monotonic subsequence algorithm NOT longest increasing algorithm\",\"datePublished\":\"2022-11-01T14:49:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/\"},\"wordCount\":208,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"java\",\"sequence\",\"subset\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/\",\"name\":\"[Solved] Longest monotonic subsequence algorithm NOT longest increasing algorithm - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-01T14:49:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Longest monotonic subsequence algorithm NOT longest increasing algorithm\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Longest monotonic subsequence algorithm NOT longest increasing algorithm - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Longest monotonic subsequence algorithm NOT longest increasing algorithm - JassWeb","og_description":"[ad_1] try this one: import java.io.BufferedReader; import java.io.InputStreamReader; public class Rozwiazanie { public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] splittedLinia = br.readLine().split((char) 32 + \"\");\/\/moglaby byc \" \" ale tak na wszelki wypadek nie ma chuja zeby sie popierdolilo teraz nawet na linuxie int aktualnyWyraz = Integer.parseInt(splittedLinia[0]);\/\/uwaga ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/","og_site_name":"JassWeb","article_published_time":"2022-11-01T14:49:55+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Longest monotonic subsequence algorithm NOT longest increasing algorithm","datePublished":"2022-11-01T14:49:55+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/"},"wordCount":208,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["java","sequence","subset"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/","url":"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/","name":"[Solved] Longest monotonic subsequence algorithm NOT longest increasing algorithm - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-01T14:49:55+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-longest-monotonic-subsequence-algorithm-not-longest-increasing-algorithm\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Longest monotonic subsequence algorithm NOT longest increasing algorithm"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/18741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=18741"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/18741\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=18741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=18741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=18741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}