{"id":968,"date":"2014-09-14T02:39:47","date_gmt":"2014-09-13T18:39:47","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=968"},"modified":"2014-10-05T16:19:20","modified_gmt":"2014-10-05T08:19:20","slug":"spoj-3557-ioi04-hermes-2","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/spoj-3557-ioi04-hermes-2\/","title":{"rendered":"SPOJ 3557. IOI04 Hermes"},"content":{"rendered":"<p><!--more--><br \/>\n<a href=\"http:\/\/www.spoj.com\/OI\/problems\/HERMES\/\">http:\/\/www.spoj.com\/OI\/problems\/HERMES\/<\/a><\/p>\n<h3>Brief description: <\/h3>\n<p>\u3002\u3002\u7ed9\u5b9a\u4e00\u4e2a [-m,m]\u00d7[-m,m] \u7684 Grid\uff0c\u8981\u6c42\u987a\u6b21\u8bbf\u95ee\u5176\u4e2d n \u4e2a\u70b9\u3002\uff08\u53ea\u8981\u548c\u8fd9\u4e2a\u70b9\u5904\u5728\u540c\u4e00\u884c\u6216\u540c\u4e00\u5217\u5c31\u5224\u5b9a\u4e3a\u8bbf\u95ee\u5230\uff09<br \/>\n\u3002\u3002\u4f60\u4ece (0, 0) \u51fa\u53d1\uff0c\u53ea\u80fd\u7ad6\u76f4\u6216\u6c34\u5e73\u79fb\u52a8\u3002\u3002\u6700\u5c0f\u5316\u603b\u8def\u7a0b\u3002<\/p>\n<h3>Analysis: <\/h3>\n<p>\u6734\u7d20 DP\uff1a 90 \u5206<br \/>\n\/\/ \u8bbe\uff1a<br \/>\n\/\/ fx[i][yy] \u8def\u8fc7\u4e86\u524d i \u4e2a\uff0c\u5f53\u524d\u5750\u6807\u5728 (x[i], yy) \u7684 xxx\u3002\u3002\u3002<br \/>\n\/\/ fy[i][xx] \u8def\u8fc7\u4e86\u524d i \u4e2a\uff0c\u5f53\u524d\u5750\u6807\u5728 (xx, y[i]) \u7684 xxx\u3002\u3002\u3002<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n\/\/}\/* .................................................................................................................................. *\/\r\n\r\nconst int N = int(2e4) + 9, C = 1009;\r\n\r\nint fx&#x5B;2]&#x5B;C*2], fy&#x5B;2]&#x5B;C*2], x&#x5B;N], y&#x5B;N];\r\nint n;\r\n\r\nint main(){\r\n\r\n#ifndef ONLINE_JUDGE\r\n    freopen(&amp;quot;in.txt&amp;quot;, &amp;quot;r&amp;quot;, stdin);\r\n    \/\/freopen(&amp;quot;out2.txt&amp;quot;, &amp;quot;w&amp;quot;, stdout);\r\n#endif\r\n\r\n    REP_1_C(i, RD(n)){\r\n        RDD(x&#x5B;i], y&#x5B;i]);\r\n    }\r\n\r\n#define fxq fx&#x5B;q]\r\n#define fxp fx&#x5B;p]\r\n#define fyq fy&#x5B;q]\r\n#define fyp fy&#x5B;p]\r\n\r\n    int p = 0, q = 1; REP(i, 2*C) fxp&#x5B;i] = fyp&#x5B;i] = abs(i - C);\r\n\r\n    REP_1(i, n){\r\n        swap(p, q); RST(fxp, fyp);\r\n        REP(yy, 2*C) fxp&#x5B;yy] = min(fxq&#x5B;yy]+abs(x&#x5B;i]-x&#x5B;i-1]), fyq&#x5B;x&#x5B;i]+C] + abs(yy-C-y&#x5B;i-1]));\r\n        REP(xx, 2*C) fyp&#x5B;xx] = min(fyq&#x5B;xx]+abs(y&#x5B;i]-y&#x5B;i-1]), fxq&#x5B;y&#x5B;i]+C] + abs(xx-C-x&#x5B;i-1]));\r\n    }\r\n\r\n    int res = INF; REP(i, 2*C){\r\n        checkMin(res, min(fxp&#x5B;i], fyp&#x5B;i]));\r\n    }\r\n    OT(res);\r\n}\r\n<\/pre>\n<p>\u590d\u6742\u5ea6 O(nc)\u3002\u3002\u3002\u8fd9\u4e2a\u72b6\u6001\u548c\u8f6c\u79fb\u76f8\u5bf9\u76f4\u767d\u3002\u3002\u5bb9\u6613\u60f3\u5230\u3002\u3002\u3002<br \/>\n\u3002\u3002\u3002\u8003\u8651\u8fdb\u4e00\u6b65\u4f18\u5316\u3002\u3002\u3002<\/p>\n<p>\u6211\u4eec\u8981 keep in mind \u7684\u53ea\u6709\u4e00\u4ef6\u4e8b\u3002\u3002\u3002<\/p>\n<p>\u201c \u5c31\u662f dp \u662f\u53ef\u4ee5\u62ff\u6570\u636e\u7ed3\u6784\u7d27\u51d1\u5730\u8868\u793a\u7684\u3002 \u3002\u3002\u3002\u201d<\/p>\n<p>\u7c7b\u4f3c\u7684\u4f8b\u5b50\u6709\uff1a<br \/>\n<a href=\"http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=4703\">HDU Problem &#8211; 4703<\/a><br \/>\n<a href=\"http:\/\/codeforces.com\/blog\/entry\/6939\">Codeforces Round #172 Editorial<\/a><\/p>\n<hr \/>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">fxp&#x5B;yy] = min(fxq&#x5B;yy]+abs(x&#x5B;i]-x&#x5B;i-1]), fyq&#x5B;x&#x5B;i]+C] + abs(yy-C-y&#x5B;i-1]));<\/pre>\n<p>\u4f46\u662f\u6211\u4eec\u4ecd\u7136\u53d1\u73b0\u53f3\u8fb9\u7684\u90e8\u5206\u8fd8\u662f\u5341\u5206\u68d8\u624b\u3002\u3002\u3002<br \/>\n\u9700\u8981\u5bf9\u4e00\u4e2a\u7ebf\u6027\u51fd\u6570\u51fd\u6570\u53d6 min \u3002\u3002\u3002\u3002<\/p>\n<p>\u4ee5\u9000\u4e3a\u8fdb\uff01<\/p>\n<p>\u6211\u4eec\u5bf9\u72b6\u6001\u8fdb\u884c redesign \u3002\u3002\uff01\uff01\u3002\u3002<\/p>\n<p>    \/\/ fx[i][yy] \u8def\u8fc7\u4e86\u524d i \u4e2a\uff0c\u5f53\u524d\u5750\u6807\u5728 (x[i], yy) \u7684 xxx\u3002\u3002\u3002<br \/>\n\u6539\u6210\u3002\u3002\u3002<br \/>\n    \/\/ fx[i][yy] \u8def\u8fc7\u4e86\u524d i \u4e2a\uff0c\u5f53\u524d\u5750\u6807\u5728 (x[i], yy) \u7684 xxx\u3002\u3002\u3002\u4f46\u4e0d\u5141\u8bb8\u5230 x[i] \u4e4b\u540e\u4ece yy \u4e0a\u6ed1\u52a8\u3002\u3002\u3002\u3002\u3002<\/p>\n<p>\u7ec6\u5fc3\u7684\u670b\u53cb\u53ef\u80fd\u5c31\u8981\u95ee\u4e86\uff1f\u8fd9\u6709\u4ec0\u4e48\u7528\u5462\uff1f\u3002\u3002\u3002\u3002\u3002\u3002<\/p>\n<p>\u8003\u5bdf\u8f6c\u79fb\uff1a<br \/>\nfx&#8217;  ->  fx \u3002\u3002\u3002\u3002<br \/>\n\u3002\u3002\u6ce8\u610f\u5230\u6211\u4eec\u4e0d\u4f1a\u5728 x[i-1] \u8fd9\u6761\u76f4\u7ebf\u4e0a\u8d70\u3002\u3002\uff08\u56e0\u4e3a\u7b49\u4ef7\u4e8e\u5728 x[i] \u4e0a\u8d70\u3002\u3002\u3002\uff09<\/p>\n<p>\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u4ece fxq[yy] \u8f6c\u79fb\u3002\u3002\u770b\u8d77\u6765\u8fd8\u662f\u3002\u3002\u3002<br \/>\nfxp[yy] = fxq[yy]+abs(x[i]-x[i-1]);<\/p>\n<p>fy&#8217; -> fx<br \/>\n\u3002\u3002\u6ce8\u610f\u3002\u3002\u3002\u3002\u6211\u4eec\u7684\u843d\u70b9\u4e00\u5b9a\u662f\u5728 (x[i], y[i-1])\u3002\u3002\u3002<br \/>\n\uff08\u5426\u5219\u3002\u3002\u5047\u8bbe\u6211\u4eec\u7684\u843d\u70b9\u662f (x[i], y)\u3002\u3002\u90a3\u4e48 y[i-1] -> y \u7684\u8fd9\u6bb5\u4f4d\u79fb\u3002\u3002\u540c\u6837\u53ef\u4ee5\u653e\u5728 x[i] \u4e0a\u8d70\u3002\u3002\u3002\uff09<\/p>\n<p>\u6240\u4ee5\u8fd9\u91cc\u5c31\u53d8\u6210\u4e86 <\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">fx&#x5B;y&#x5B;i-1]] = min(fy'&#x5B;xx] + |xx-x&#x5B;i]| );<\/pre>\n<p>\u548c\u4e4b\u524d\u7684\u72b6\u6001\u8bbe\u8ba1\u4e2d\u3002\u3002\u5bf9\u5e94\u7684\u8fd9\u5219\u8f6c\u79fb\u6bd4\u8f83\u3002\u3002\u3002<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\nREP(yy, 2*C) fxp&#x5B;yy] = min(fxq&#x5B;yy]+abs(x&#x5B;i]-x&#x5B;i-1]), fyq&#x5B;x&#x5B;i]+C] + abs(yy-C-y&#x5B;i-1]));\r\n<\/pre>\n<p>\u4ece\u5f71\u54cd\u7684\u72b6\u6001\u662f O(n) \u8f6c\u79fb\u662f O(1)\u3002\u3002<br \/>\n\u53d8\u6210\u4e86\u5f71\u54cd\u7684\u72b6\u6001\u662f O(1)\u3002\u3002\u8f6c\u79fb\u662f O(n)\u3002\u3002\u3002<\/p>\n<p>\u800c\u65b0\u7684\u72b6\u6001\u5df2\u7ecf\u5341\u5206\u5bb9\u6613\u7ef4\u62a4\u4e86\u3002\u3002\u3002\u3001\u3001\u3001<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\nfx&#x5B;y&#x5B;i-1]] = min(fy'&#x5B;xx] + |xx-x&#x5B;i]| );\r\n<\/pre>\n<p>\u5c06\u7edd\u5bf9\u503c\u62c6\u5f00\u3002\u3002<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\nfx&#x5B;y&#x5B;i-1]] =  min(\r\nx&#x5B;i] + min(fy'&#x5B;xx]-xx) | xx &lt;= x&#x5B;i], \r\nmin(fy'&#x5B;xx]+xx) - x&#x5B;i] | xx &gt;= x&#x5B;i]\r\n);\r\n<\/pre>\n<p>\u7ebf\u6bb5\u6811\u7ef4\u62a4\u5373\u53ef\u3002\u3002 = =<\/p>\n<p>Beautiful !!!<\/p>\n<h3>External Link: <\/h3>\n<p><a href=\"https:\/\/gist.github.com\/lychees\/9faa6e55fa996e7d2cb4\">Github Gist &#8211; http:\/\/www.spoj.com\/OI\/problems\/HERMES\/ <\/a><\/p>\n<hr \/>\n<p>\u540e\u9762\u4e00\u9898\uff1a<\/p>\n<p>\u8d2a\u5fc3\u663e\u7136\u662f\u4e0d\u5bf9\u7684\u3002\u3002\u8003\u8651\u4e71\u641e\uff1a<br \/>\n\u5b98\u65b9\u9898\u89e3\uff1a<br \/>\n\u201c<br \/>\n\u9996\u5148\u5bf9\u6a2a\u7ebf\u548c\u7ad6\u7ebf\u7684\u5750\u6807\u5206\u522b\u505a\u955c\u50cf\u7ffb\u8f6c\uff0c\u4f7f\u5b83\u4eec\u7684\u5750\u6807\u5206\u522b\u90fd\u662f\u5355\u8c03\u9012\u589e\u7684\uff0c<br \/>\n\u5e76\u4e14\u76f8\u90bb\u7684\u6a2a\u7ebf\u548c\u76f8\u90bb\u7684\u7ad6\u7ebf\u7684\u8ddd\u79bb\u6839\u539f\u59cb\u5750\u6807\u7684\u8ddd\u79bb\u4fdd\u6301\u4e00\u81f4\u3002\u8fd9\u53ef\u4ee5\u5728\u7ebf\u6027\u65f6\u95f4\u5185\u5b8c\u6210\uff0c<br \/>\n\u5e76\u4e14\u4e0d\u5f71\u54cd\u7b54\u6848\u3002\u7136\u540e\u6309\u7167\u987a\u5e8f\u626b\u63cf\u6240\u6709\u76f4\u7ebf\uff0c\u7ef4\u62a4\u4ece\u539f\u70b9\u51fa\u53d1\u7684\u4e24\u6761\u79fb\u52a8\u8def\u5f84\u3002<br \/>\n\u4e00\u6761\u79fb\u52a8\u8def\u5f84\u5c3d\u53ef\u80fd\u5f80\u5de6\u8d70\uff0c\u4e00\u6761\u79fb\u52a8\u8def\u5f84\u5c3d\u53ef\u80fd\u5f80\u53f3\u8d70\uff0c<br \/>\n\u4f46\u90fd\u4fdd\u6301\u6700\u77ed\u8def\u5e76\u4e14\u6ee1\u8db3\u89c4\u5b9a\u7684\u76f4\u7ebf\u5230\u8fbe\u987a\u5e8f\u3002\u8fd9\u4e24\u6761\u79fb\u52a8\u8def\u5f84\u5206\u522b\u662f\u4e0a\u51f8\u66f2\u7ebf\u548c\u4e0b\u51f8\u66f2\u7ebf\uff0c<br \/>\n\u6bcf\u626b\u63cf\u5230\u53cd\u5411\u4e0d\u540c\u7684\u4e24\u6761\u76f4\u7ebf\u65f6\u5c31\u6839\u636e\u4ea4\u53c9\u70b9\u66f4\u65b0\u5176\u4e2d\u4e00\u6761\u79fb\u52a8\u8def\u5f84\u3002<br \/>\n\u626b\u63cf\u5230\u6700\u540e\u4e00\u6761\u76f4\u7ebf\u65f6\u5c31\u53ef\u4ee5\u7b97\u51fa\u6700\u77ed\u7684\u79fb\u52a8\u8def\u5f84\u957f\u5ea6\u4e86\u3002<br \/>\n\u201d<\/p>\n<p>\u5c3c\u739b\u7b2c\u4e00\u53e5\u8bdd\u5c31\u770b\u4e0d\u61c2\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[1],"tags":[],"class_list":["post-968","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-fC","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/comments?post=968"}],"version-history":[{"count":1,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/968\/revisions"}],"predecessor-version":[{"id":969,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/968\/revisions\/969"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}