{"id":629,"date":"2013-09-15T10:17:50","date_gmt":"2013-09-15T02:17:50","guid":{"rendered":"http:\/\/blog.dayandcarrot.net\/?p=629"},"modified":"2013-09-15T10:17:50","modified_gmt":"2013-09-15T02:17:50","slug":"%e4%b9%9d%e5%ba%a6oj-%e9%a2%98%e7%9b%ae1172%ef%bc%9a%e5%93%88%e5%a4%ab%e6%9b%bc%e6%a0%91","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=629","title":{"rendered":"\u4e5d\u5ea6OJ \u9898\u76ee1172\uff1a\u54c8\u592b\u66fc\u6811"},"content":{"rendered":"<dl>\n<dt><b>\u9898\u76ee\u63cf\u8ff0\uff1a<\/b><\/dt>\n<dd>\u54c8\u592b\u66fc\u6811\uff0c\u7b2c\u4e00\u884c\u8f93\u5165\u4e00\u4e2a\u6570n\uff0c\u8868\u793a\u53f6\u7ed3\u70b9\u7684\u4e2a\u6570\u3002\u9700\u8981\u7528\u8fd9\u4e9b\u53f6\u7ed3\u70b9\u751f\u6210\u54c8\u592b\u66fc\u6811\uff0c\u6839\u636e\u54c8\u592b\u66fc\u6811\u7684\u6982\u5ff5\uff0c\u8fd9\u4e9b\u7ed3\u70b9\u6709\u6743\u503c\uff0c\u5373weight\uff0c\u9898\u76ee\u9700\u8981\u8f93\u51fa\u6240\u6709\u7ed3\u70b9\u7684\u503c\u4e0e\u6743\u503c\u7684\u4e58\u79ef\u4e4b\u548c\u3002\n<\/dd>\n<\/dl>\n<dl>\n<dt><b>\u8f93\u5165\uff1a<\/b><\/dt>\n<dd>\u8f93\u5165\u6709\u591a\u7ec4\u6570\u636e\u3002<br \/>\n\u6bcf\u7ec4\u7b2c\u4e00\u884c\u8f93\u5165\u4e00\u4e2a\u6570n\uff0c\u63a5\u7740\u8f93\u5165n\u4e2a\u53f6\u8282\u70b9\uff08\u53f6\u8282\u70b9\u6743\u503c\u4e0d\u8d85\u8fc7100\uff0c2&lt;=n&lt;=1000\uff09\u3002\n<\/dd>\n<\/dl>\n<dl>\n<dt><b>\u8f93\u51fa\uff1a<\/b><\/dt>\n<dd>\u8f93\u51fa\u6743\u503c\u3002\n<\/dd>\n<\/dl>\n<dl>\n<dt><b>\u6837\u4f8b\u8f93\u5165\uff1a<\/b><\/dt>\n<dd>\n<pre>5\n1 2 2 5 9<\/pre>\n<\/dd>\n<\/dl>\n<dl>\n<dt><b>\u6837\u4f8b\u8f93\u51fa\uff1a<\/b><\/dt>\n<dd>\n<pre>37<\/pre>\n<\/dd>\n<\/dl>\n<p>=================================<br \/>\n\u5410\u69fd:VC6\u91cc\u9762\u667a\u80fd\u63d0\u793a\u597d\u50cf\u5230while\u8bed\u53e5\u4e4b\u7c7b\u7684\u5faa\u73af\u91cc\u9762\u5c31\u4f1a\u5931\u6548\uff0c\u7136\u540e\u5c31\u53ea\u80fd\u76f2\u6253..Orz&#8230;<br \/>\n=================================<br \/>\n<code lang=\"c++\"><br \/>\n#include <vector><br \/>\n#include <iostream><br \/>\n#include <algorithm><br \/>\nusing namespace std;<br \/>\nstruct Node<br \/>\n{<br \/>\n    int val;<br \/>\n    Node *parent;<br \/>\n    Node()<br \/>\n    {<br \/>\n        parent = NULL;<br \/>\n    }<br \/>\n    int getHeight()<br \/>\n    {<br \/>\n        int h = 0;<br \/>\n        Node *p = parent;<br \/>\n        while(p != NULL)<br \/>\n        {<br \/>\n            h++;<br \/>\n            p = p->parent;<br \/>\n        }<br \/>\n        return h;<br \/>\n    }<br \/>\n};<br \/>\nbool compare(Node *x, Node *y)<br \/>\n{<br \/>\n    return x->val > y->val;<br \/>\n}<br \/>\nint main()<br \/>\n{<br \/>\n    int N;<br \/>\n    while(cin >> N)<br \/>\n    {<br \/>\n        vector<node*> workingSet;<br \/>\n        Node *nodes = new Node[N]; \/\/Primitive Nodes<br \/>\n        int i;<br \/>\n        for(i=0; i<n; i++)\n        {\n            cin >> nodes[i].val;<br \/>\n            workingSet.push_back(&nodes[i]);<br \/>\n        }<br \/>\n        \/\/\/<br \/>\n        while(workingSet.size() > 1)<br \/>\n        {<br \/>\n            sort(workingSet.begin(), workingSet.end(), compare);<br \/>\n            Node *small1 = workingSet.back();<br \/>\n            workingSet.pop_back();<br \/>\n            Node *small2 = workingSet.back();<br \/>\n            workingSet.pop_back();<br \/>\n            \/\/Combine<br \/>\n            Node *combined = new Node;<br \/>\n            combined->val = small1->val + small2->val;<br \/>\n            small1->parent = combined;<br \/>\n            small2->parent = combined;<br \/>\n            workingSet.push_back(combined);<br \/>\n        }<br \/>\n        \/\/output<br \/>\n        int sum = 0;<br \/>\n        for(i=0; i<n; i++)\n        {\n            int val = nodes[i].val * nodes[i].getHeight();\n            sum += val;\n        }\n        cout << sum << endl;\n        delete []nodes;\n    }\n    return 0;\n}\n\/**************************************************************\n    Problem: 1172\n    User: iorange\n    Language: C++\n    Result: Accepted\n    Time:70 ms\n    Memory:1520 kb\n****************************************************************\/\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0\uff1a \u54c8\u592b\u66fc\u6811\uff0c\u7b2c\u4e00\u884c\u8f93\u5165\u4e00\u4e2a\u6570n\uff0c\u8868\u793a\u53f6\u7ed3\u70b9\u7684\u4e2a\u6570\u3002\u9700\u8981\u7528\u8fd9\u4e9b\u53f6\u7ed3\u70b9\u751f\u6210\u54c8\u592b\u66fc\u6811\uff0c\u6839\u636e\u54c8\u592b\u66fc\u6811\u7684\u6982\u5ff5\uff0c\u8fd9\u4e9b\u7ed3\u70b9\u6709\u6743\u503c\uff0c\u5373weight\uff0c\u9898\u76ee\u9700\u8981\u8f93\u51fa\u6240\u6709\u7ed3\u70b9\u7684\u503c\u4e0e\u6743\u503c\u7684\u4e58\u79ef\u4e4b\u548c\u3002 \u8f93\u5165\uff1a \u8f93\u5165\u6709\u591a\u7ec4\u6570\u636e\u3002 \u6bcf\u7ec4\u7b2c\u4e00\u884c\u8f93\u5165\u4e00\u4e2a\u6570n\uff0c\u63a5\u7740\u8f93\u5165n\u4e2a\u53f6\u8282\u70b9\uff08\u53f6\u8282\u70b9\u6743\u503c\u4e0d\u8d85\u8fc7100\uff0c2&lt;=n&lt;=1000\uff09\u3002 \u8f93\u51fa\uff1a \u8f93\u51fa\u6743\u503c\u3002 \u6837\u4f8b\u8f93\u5165\uff1a 5 1 2 2 5 9 \u6837\u4f8b\u8f93\u51fa\uff1a 37 ================================= \u5410\u69fd:VC6\u91cc\u9762\u667a\u80fd\u63d0\u793a\u597d\u50cf\u5230while\u8bed\u53e5\u4e4b\u7c7b\u7684\u5faa\u73af\u91cc\u9762\u5c31\u4f1a\u5931\u6548\uff0c\u7136\u540e\u5c31\u53ea\u80fd\u76f2\u6253..Orz&#8230; ================================= #include #include #include using namespace std; struct Node { int val; Node *parent; Node() { parent = NULL; } int getHeight() { int h = 0; Node *p = parent; while(p != NULL) { h++; p [&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":[132],"class_list":["post-629","post","type-post","status-publish","format-standard","hentry","category-study","tag-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/629","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=629"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/629\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}