{"id":31884,"date":"2023-01-25T08:43:10","date_gmt":"2023-01-25T03:13:10","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/"},"modified":"2023-01-25T08:43:10","modified_gmt":"2023-01-25T03:13:10","slug":"solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/","title":{"rendered":"[Solved] What is wrong with this solution while solving JUICE on SPOJ?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-29964207\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"29964207\" data-parentid=\"29901665\" 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>It&#8217;s probably wrong when there is some segments <strong>having same start time<\/strong>.<\/p>\n<p>I run this test case with your code:<\/p>\n<blockquote>\n<p>10<\/p>\n<p>1 1<\/p>\n<p>1 2<\/p>\n<p>3 3<\/p>\n<p>1 4<\/p>\n<p>3 5<\/p>\n<p>3 6<\/p>\n<p>3 7<\/p>\n<p>8 8<\/p>\n<p>3 9<\/p>\n<p>3 10<\/p>\n<\/blockquote>\n<p>Your code output <strong>10<\/strong> but the answer is actually <strong>9<\/strong><\/p>\n<p>I have several WA because of this test case too,<br \/>\nand got AC after I handle this case&#8230;I will share my experience and please see if it is the same bug for you.<\/p>\n<p>Using this test case, my code will output <strong>10, I will explain quickly as follows:<\/strong> <\/p>\n<p><strong>Let dp(i) be the state that you have your last cut at i-th segment&#8217;s start time<\/strong><\/p>\n<p>Clearly, dp(i) = maximum (dp(j) + # of segments(after j)intersect with i-th segment) for all j &lt; i<\/p>\n<p>Physically , it means <strong>you have the second last cut at j-th segment&#8217;s start time<\/strong> which is logical <strong>except<\/strong> if <strong>j-th and i-th segment&#8217;s start time is the same! Then it is not 2 different cut but the same cut!<\/strong><\/p>\n<p>To handle this, <strong>the correct way is choose the last segment of consecutive segments having same start time ONLY!<\/strong><\/p>\n<p>(<strong>Assuming the segment is sorted by start time already, the order below is the sorted order<\/strong>)<\/p>\n<p>Here is the bug, my program (maybe yours as well) gives the output 10 because (using 0-based)<\/p>\n<p>DP(9) = DP(6) + # overlap segments in [7,9]<\/p>\n<p>= 7 + 3 = 10!<\/p>\n<p>If you look deeper, DP(6) = 7 is actually right, as it does not consider the segments afterwards. But <strong>you should not use DP(6) to update any DP(X) for X&gt;6!<\/strong> as <strong>the last segment having same start time (with 6-th segment) is the 8-th segment<\/strong> <\/p>\n<p>In short, if we use some DP state which is NOT the last one having the same start time with itself to update other DP states, something may go wrong.<\/p>\n<p>My solution is that when I doing DP on the i-th segment, whenever I find some segments j having same start time with segment i (j &lt; i), <strong>I set DP(j) to negative infinity<\/strong> so that it must not be optimal enough to update other states.<\/p>\n<p><strong>EDITED: Accepted Code Added<\/strong><br \/>\nAs required by OP, below is my code which is accpeted<\/p>\n<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"snippet-code-js lang-js prettyprint-override\"><code>#include&lt;bits\/stdc++.h&gt;\r\n#define pii pair&lt;int,int&gt;\r\n#define x first\r\n#define y second\r\n#define INF 1LL&lt;&lt;28\r\nusing namespace std;\r\n\r\nint T,n;\r\nint dp[1005];\r\npii a[1005];\r\nint main(){\r\n\tscanf(\"%d\", &amp;T);\r\n\tfor(int qwe=1; qwe &lt;= T; qwe++){\r\n\t\tscanf(\"%d\", &amp;n);\r\n\t\tfor(int i=0; i&lt;n;i++) scanf(\"%d%d\", &amp;a[i].x, &amp;a[i].y);\r\n\t\tsort(a, a+n);\r\n\t\tmemset(dp,0,sizeof(dp));\r\n\t\t\r\n\t\tfor(int i=0; i&lt;n;i++){\r\n\t\t\tint cnt = 0;\r\n\t\t\tfor(int j=i-1; j&gt;=0; j--){\r\n\t\t\t\tif(a[j].x == a[i].x){dp[j] = -INF; cnt++; continue;}\r\n\t\t\t\tcnt += (a[i].x &gt;= a[j+1].x &amp;&amp; a[i].x &lt;= a[j+1].y);\r\n\t\t\t\tdp[i] = max(dp[i], dp[j] + (cnt&gt;2? cnt:0));\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tcnt += (a[i].x &gt;= a[0].x &amp;&amp; a[i].x &lt;= a[0].y);\r\n\t\t\tdp[i] = max(dp[i], (cnt&gt;2? cnt:0));\r\n\t\t}\r\n\t\tprintf(\"Case #%d: %d\\n\", qwe, dp[n-1]);\r\n\t}\r\n\treturn 0;\t\r\n}<\/code><\/pre>\n<\/div>\n<\/div><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved What is wrong with this solution while solving JUICE on SPOJ? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] It&#8217;s probably wrong when there is some segments having same start time. I run this test case with your code: 10 1 1 1 2 3 3 1 4 3 5 3 6 3 7 8 8 3 9 3 10 Your code output 10 but the answer is actually 9 I have several &#8230; <a title=\"[Solved] What is wrong with this solution while solving JUICE on SPOJ?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/\" aria-label=\"More on [Solved] What is wrong with this solution while solving JUICE on SPOJ?\">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":[457,324,1372],"class_list":["post-31884","post","type-post","status-publish","format-standard","hentry","category-solved","tag-algorithm","tag-c","tag-dynamic-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] What is wrong with this solution while solving JUICE on SPOJ? - 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-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] What is wrong with this solution while solving JUICE on SPOJ? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] It&#8217;s probably wrong when there is some segments having same start time. I run this test case with your code: 10 1 1 1 2 3 3 1 4 3 5 3 6 3 7 8 8 3 9 3 10 Your code output 10 but the answer is actually 9 I have several ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-25T03:13:10+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=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] What is wrong with this solution while solving JUICE on SPOJ?\",\"datePublished\":\"2023-01-25T03:13:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/\"},\"wordCount\":373,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"algorithm\",\"c++\",\"dynamic-programming\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/\",\"name\":\"[Solved] What is wrong with this solution while solving JUICE on SPOJ? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-01-25T03:13:10+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] What is wrong with this solution while solving JUICE on SPOJ?\"}]},{\"@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=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] What is wrong with this solution while solving JUICE on SPOJ? - 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-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] What is wrong with this solution while solving JUICE on SPOJ? - JassWeb","og_description":"[ad_1] It&#8217;s probably wrong when there is some segments having same start time. I run this test case with your code: 10 1 1 1 2 3 3 1 4 3 5 3 6 3 7 8 8 3 9 3 10 Your code output 10 but the answer is actually 9 I have several ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/","og_site_name":"JassWeb","article_published_time":"2023-01-25T03:13:10+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] What is wrong with this solution while solving JUICE on SPOJ?","datePublished":"2023-01-25T03:13:10+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/"},"wordCount":373,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["algorithm","c++","dynamic-programming"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/","url":"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/","name":"[Solved] What is wrong with this solution while solving JUICE on SPOJ? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-01-25T03:13:10+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-what-is-wrong-with-this-solution-while-solving-juice-on-spoj\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] What is wrong with this solution while solving JUICE on SPOJ?"}]},{"@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=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","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\/31884","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=31884"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/31884\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=31884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=31884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=31884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}