{"id":310,"date":"2013-06-08T13:30:09","date_gmt":"2013-06-08T05:30:09","guid":{"rendered":"http:\/\/blog.dayandcarrot.net\/?p=310"},"modified":"2013-06-08T13:30:09","modified_gmt":"2013-06-08T05:30:09","slug":"1007-maximum-subsequence-sum-25","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=310","title":{"rendered":"1007. Maximum Subsequence Sum (25)"},"content":{"rendered":"<h1><span style=\"font-size: 13px;\">\u65f6\u95f4\u9650\u5236<\/span><\/h1>\n<div id=\"problemInfo\">\n<div>\n<div>400 ms<\/div>\n<\/div>\n<div>\n<div>\u5185\u5b58\u9650\u5236<\/div>\n<div>32000 kB<\/div>\n<\/div>\n<div>\n<div>\u4ee3\u7801\u957f\u5ea6\u9650\u5236<\/div>\n<div>16000 B<\/div>\n<\/div>\n<div>\n<div>\u5224\u9898\u7a0b\u5e8f<\/div>\n<div>Standard<\/div>\n<\/div>\n<div>\u4f5c\u8005<\/div>\n<div>CHEN, Yue<\/div>\n<\/div>\n<div id=\"problemContent\">\nGiven a sequence of K integers { N<sub>1<\/sub>, N<sub>2<\/sub>, &#8230;, N<sub>K<\/sub>\u00a0}. A continuous subsequence is defined to be { N<sub>i<\/sub>, N<sub>i+1<\/sub>, &#8230;, N<sub>j<\/sub>\u00a0} where 1 &lt;= i &lt;= j &lt;= K. The\u00a0<i>Maximum Subsequence<\/i>\u00a0is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.<br \/>\nNow you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.<br \/>\n<b>Input Specification:<\/b><br \/>\nEach input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (&lt;= 10000). The second line contains K numbers, separated by a space.<br \/>\n<b>Output Specification:<\/b><br \/>\nFor each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.<br \/>\n<b>Sample Input:<\/b><\/p>\n<pre>10\n-10 1 2 3 4 -5 -23 3 7 -21<\/pre>\n<p><b>Sample Output:<\/b><\/p>\n<pre>10 1 4<\/pre>\n<\/div>\n<p>==============================================<br \/>\n\u8fd9\u662f\u4e00\u4e2a\u7ecf\u5178\u95ee\u9898\uff0c\u53ef\u4ee5\u5229\u7528\u300a\u7f16\u7a0b\u73e0\u7391\u300b\u7b2c8\u7ae0<\u7b97\u6cd5\u8bbe\u8ba1\u6280\u672f>\u91cc\u9762\u7684\u626b\u63cf\u7b97\u6cd5<br \/>\n\u7b97\u6cd5\u4e0d\u5199\u4e86\uff0c\u4e66\u4e0a\u4e00\u6a21\u4e00\u6837\u7684\u539f\u9898\uff0c\u4f2a\u4ee3\u7801\u5982\u4e0b:<\/p>\n<pre>\nmaxsofar = 0\nmaxendinghere = 0\nfor i = [0,n)\n    \/* invariant: maxendinghere and maxsofar\n       are accurate for x[0..i-1] *\/\n    maxendinghere = max(maxendinghere + x[i], 0)\n    maxsofar = max(maxendinghere, maxsofar)\n<\/pre>\n<p>\u53ea\u4e0d\u8fc7\u8fd9\u91cc\u8f93\u51fa\u7684\u65f6\u5019\u8981\u8f93\u51fa\u9996\u672b\u4f4d\u7f6e\u7684\u6570\u5b57\uff0c\u6240\u4ee5\u52a0\u4e86\u4e00\u4e9b\u53d8\u91cf\u3002<br \/>\n\u4ec0\u4e48\u65f6\u5019\u4f1a\u6539\u53d8\u9996\u672b\u4f4d\u7f6e\u5462\uff1a<br \/>\n1. maxEndingHere == 0\u7684\u65f6\u5019\uff0c\u8868\u793a\u5982\u679c\u540e\u9762\u8fd8\u4f1a\u6709\u66f4\u597d\u7684\u7ed3\u679c\u7684\u8bdd\uff0c\u80af\u5b9a\u5f00\u59cb\u7684\u4f4d\u7f6e\u662f\u4ece\u8fd9\u4e2a\u70b9\u4e4b\u540e\u7684\u4e00\u4e2a\u6570\u5b57\u5f00\u59cb\u7684\u3002\u6b64\u65f6\u8981\u8bb0\u5f55\u53ef\u80fd\u4f1a\u4ea7\u751fgap\uff0c\u7559\u7ed9\u540e\u7eed\u5904\u7406<br \/>\n2.\u5f53\u5230\u8fbevStartIndex\uff0c\u522b\u5fd8\u4e86\u8bb0\u4e0b<\u53ef\u80fd\u7684>\u65b0\u7684\u8d77\u70b9<br \/>\n3.\u5f53\u540e\u9762\u90a3\u4e00\u5768\u771f\u7684\u6bd4\u539f\u6765\u7684\u503c\u8fd8\u5927\u65f6\uff0c\u5982\u679c\u5b58\u5728gap\uff0c\u90a3\u8981\u7528\u65b0\u7684\u8d77\u70b9\u66ff\u6362\u6389\u65e7\u7684\uff0c\u7136\u540e\u5427gap\u5f00\u5173\u5173\u4e0a\uff0c\u4e00\u4e2a\u65b0\u7684\u5f00\u59cb<br \/>\n4.\u5f53\u7b97\u51fa\u6765\u603b\u6570maxEndingHere\u6bd4maxSofar\u5927\u65f6\uff0c\u522b\u5fd8\u4e86\u8bb0\u4e0b\u5f53\u524d\u4f4d\u7f6e\u7684\u6570\u503c\u4f5c\u4e3a\u672b\u5c3e\u6570\u503c<br \/>\n\u53e6\u5916\u8981\u6ce8\u610f\uff0c\u5982\u679c\u5168\u662f\u8d1f\u503c\u600e\u4e48\u5904\u7406\uff0c\u7279\u522b\u6ce8\u610f\u8fd9\u6837\u7684\u8f93\u5165\uff0c\u4e0d\u80fd\u4ee5maxSofar\u662f\u5426>0\u5224\u65ad<\/p>\n<pre>\n3\n-1 0 -1\n<\/pre>\n<p>\u4ee3\u7801\u4e2dstartIndex\u4e0eendIndex\u662f\u4e0d\u5fc5\u8981\u5b58\u50a8\u7684\u3002<br \/>\n=============================================<\/p>\n<pre>\n#include <stdio.h>\n#include <algorithm> \/\/max\nusing namespace std;\nint main()\n{\n\tint N;\n\tscanf(\"%d\", &N);\n\tlong maxSofar = 0;\n\tlong maxEndingHere = 0;\n\tint startVal = 0;\n\tint startIndex = -1;  \/\/\n\tint vStartIndex = 0;\/\/\n\tint vStartVal = 0;\n\tint endIndex = -1;\/\/\n\tint endVal = 0;\n\tbool hasGap = true;\n\tbool allNegative = true;\n\t\/\/int *savedVals = new int[N];\n\tint totalStartVal;\n\tint totalEndVal;\n\tfor(int i=0; i<n; i++)\n\t{\n\t\tint inputVal;\n\t\tscanf(\"%d\", &#038;inputVal);\n\t\tif(inputVal >= 0 && allNegative)\n\t\t\tallNegative = false;\n\t\tif(i==0)\n\t\t\ttotalStartVal = inputVal;\n\t\telse if(i == N-1)\n\t\t\ttotalEndVal = inputVal;\n\t\t\/\/savedVals[i] = inputVal;\n\t\tmaxEndingHere = max( maxEndingHere + inputVal, (long)0);\n\t\tif(maxEndingHere == 0)\n\t\t{\n\t\t\thasGap = true;\n\t\t\tvStartIndex = i+1;\n\t\t}\n\t\tif(i == vStartIndex)\n\t\t\tvStartVal = inputVal;\n\t\tif( maxEndingHere > maxSofar)\n\t\t{\n\t\t\tmaxSofar = maxEndingHere;\n\t\t\tendIndex = i;\n\t\t\tendVal = inputVal;\n\t\t\tif(hasGap)\n\t\t\t{\n\t\t\t\tstartIndex = vStartIndex;\n\t\t\t\tstartVal = vStartVal;\n\t\t\t\thasGap = false;\n\t\t\t}\n\t\t}\n\t}\n\tif(!allNegative)\n\t\tprintf(\"%ld %d %d\", maxSofar, startVal, endVal);\n\telse\n\t\tprintf(\"%ld %d %d\", maxSofar, totalStartVal, totalEndVal);\n\treturn 0;\n}\n<\/pre>\n<p>\u6d4b\u8bd5\u70b9\t\u7ed3\u679c\t\u7528\u65f6(ms)\t\u5185\u5b58(kB)\t\u5f97\u5206\/\u6ee1\u5206<br \/>\n0\t\u7b54\u6848\u6b63\u786e\t0\t750\t2\/2<br \/>\n1\t\u7b54\u6848\u6b63\u786e\t0\t790\t1\/1<br \/>\n2\t\u7b54\u6848\u6b63\u786e\t0\t790\t3\/3<br \/>\n3\t\u7b54\u6848\u6b63\u786e\t0\t790\t4\/4<br \/>\n4\t\u7b54\u6848\u6b63\u786e\t0\t790\t4\/4<br \/>\n5\t\u7b54\u6848\u6b63\u786e\t0\t790\t3\/3<br \/>\n6\t\u7b54\u6848\u6b63\u786e\t0\t790\t3\/3<br \/>\n7\t\u7b54\u6848\u6b63\u786e\t0\t750\t5\/5<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u65f6\u95f4\u9650\u5236 400 ms \u5185\u5b58\u9650\u5236 32000 kB \u4ee3\u7801\u957f\u5ea6\u9650\u5236 16000 B \u5224\u9898\u7a0b\u5e8f Standard \u4f5c\u8005 CHEN, Yue Given a sequence of K integers { N1, N2, &#8230;, NK\u00a0}. A continuous subsequence is defined to be { Ni, Ni+1, &#8230;, Nj\u00a0} where 1 &lt;= i &lt;= j &lt;= K. The\u00a0Maximum Subsequence\u00a0is the continuous subsequence which has the largest sum [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[84],"class_list":["post-310","post","type-post","status-publish","format-standard","hentry","category-study","tag-pat"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=310"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/310\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}