{"id":441,"date":"2018-09-08T14:49:55","date_gmt":"2018-09-08T14:49:55","guid":{"rendered":"https:\/\/www.haoyuan.info\/?p=441"},"modified":"2018-09-08T14:49:55","modified_gmt":"2018-09-08T14:49:55","slug":"%e8%a1%a8%e8%be%be%e5%bc%8f%e8%a7%a3%e6%9e%90%e6%a8%a1%e6%9d%bf%e5%a4%87%e4%bb%bd","status":"publish","type":"post","link":"https:\/\/haoyuan.info\/?p=441","title":{"rendered":"\u8868\u8fbe\u5f0f\u89e3\u6790\u6a21\u677f\u5907\u4efd"},"content":{"rendered":"<pre><code class=\"language-cpp line-numbers\">#include &lt;bits\/stdc++.h&gt;\n\nusing namespace std;\nusing ll = long long;\nmap&lt;char, ll&gt; L;\nmap&lt;char, ll&gt; R;\n\nstring rePolish(const string &amp;str) {\n    string ret;\n    stack&lt;char&gt; s;\n    s.push('#');\n    for (ll i = 0; i &lt; str.length(); i++) {\n        char t = str[i];\n        if (t == '(')\n            s.push(t);\n        else if (t == ')') {\n            while (s.top() != '(') {\n                ret += s.top();\n                s.pop();\n                ret += ' ';\n            }\n            s.pop();\n        } else if (t == '+' || t == '-' || t == '*' || t == '\/' || t == 'd') {\n            while (L[s.top()] &gt;= R[t]) {\n                ret += s.top();\n                ret += ' ';\n                s.pop();\n            }\n            s.push(t);\n        } else {\n            while (isdigit(str[i]))\n                ret += str[i++];\n            i--, ret += ' ';\n        }\n    }\n    while (s.top() != '#') {\n        ret += s.top();\n        ret += ' ';\n        s.pop();\n    }\n    return ret;\n}\n\npair&lt;ll, ll&gt; operator+(pair&lt;ll, ll&gt; a, pair&lt;ll, ll&gt; b) {\n    long long vec[] = {a.first + b.first, a.first + b.second, a.second + b.first, a.second + b.second};\n    sort(vec, vec + 4);\n    return {vec[0], vec[3]};\n}\n\npair&lt;ll, ll&gt; operator-(pair&lt;ll, ll&gt; a, pair&lt;ll, ll&gt; b) {\n    long long vec[] = {a.first - b.first, a.first - b.second, a.second - b.first, a.second - b.second};\n    sort(vec, vec + 4);\n    return {vec[0], vec[3]};\n}\n\npair&lt;ll, ll&gt; operator*(pair&lt;ll, ll&gt; a, pair&lt;ll, ll&gt; b) {\n    long long vec[] = {a.first * b.first, a.first * b.second, a.second * b.first, a.second * b.second};\n    sort(vec, vec + 4);\n    return {vec[0], vec[3]};\n}\n\npair&lt;ll, ll&gt; operator\/(pair&lt;ll, ll&gt; a, pair&lt;ll, ll&gt; b) {\n    long long vec[] = {a.first \/ b.first, a.first \/ b.second, a.second \/ b.first, a.second \/ b.second};\n    sort(vec, vec + 4);\n    return {vec[0], vec[3]};\n}\n\npair&lt;ll, ll&gt; operator^(pair&lt;ll, ll&gt; a, pair&lt;ll, ll&gt; b) {\n    if (a.first &lt; 0) a.first = 0;\n    if (b.first &lt; 1) b.first = 1;\n    long long vec[] = {a.first, a.second, a.first * b.first, a.first * b.second, a.second * b.first,\n                       a.second * b.second};\n    sort(vec, vec + 6);\n    return {vec[0], vec[5]};\n}\n\npair&lt;ll, ll&gt; calc(const string &amp;str) {\n    stack&lt;pair&lt;ll, ll&gt;&gt; s;\n    ll i = 0;\n\n    pair&lt;ll, ll&gt; tmp;\n    while (i &lt; str.length()) {\n        if (str[i] == ' ') {\n            i++;\n            continue;\n        }\n        if (str[i] == '+')\n            tmp = s.top(), s.pop(), tmp = tmp + s.top(), s.pop(), i++;\n        else if (str[i] == '-')\n            tmp = s.top(), s.pop(), tmp = s.top() - tmp, s.pop(), i++;\n        else if (str[i] == '*')\n            tmp = s.top(), s.pop(), tmp = tmp * s.top(), s.pop(), i++;\n        else if (str[i] == '\/')\n            tmp = s.top(), s.pop(), tmp = s.top() \/ tmp, s.pop(), i++;\n        else if (str[i] == 'd')\n            tmp = s.top(), s.pop(), tmp = s.top() ^ tmp, s.pop(), i++;\n        else {\n            ll x = 0;\n            while (isdigit(str[i]))\n                x = x * 10 + str[i++] - '0';\n            tmp = {x, x};\n        }\n        s.push(tmp);\n    }\n    return s.top();\n}\n\nint main() {\n    ios::sync_with_stdio(false);\n    cin.tie(0);\n    cout.tie(0);\n    L['+'] = 1, L['-'] = 1, L['*'] = 2, L['\/'] = 2, L['d'] = 3;\n    R['+'] = 1, R['-'] = 1, R['*'] = 2, R['\/'] = 2, R['d'] = 4;\n    string s;\n    while (cin &gt;&gt; s) {\n        string tmp = rePolish(s);\n        auto res = calc(tmp);\n        cout &lt;&lt; res.first &lt;&lt; \" \" &lt;&lt; res.second &lt;&lt; '\\n';\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#include &lt;bits\/stdc++.h&gt; using namespace std; usi [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[39,40],"tags":[],"class_list":["post-441","post","type-post","status-publish","format-standard","hentry","category-c","category-40"],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8UC2c-77","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/posts\/441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=441"}],"version-history":[{"count":0,"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/posts\/441\/revisions"}],"wp:attachment":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}