{"id":656,"date":"2023-10-03T22:18:05","date_gmt":"2023-10-03T14:18:05","guid":{"rendered":"http:\/\/ggapa.net:81\/?p=656"},"modified":"2023-10-15T00:13:58","modified_gmt":"2023-10-14T16:13:58","slug":"%e7%ae%97%e6%b3%95%e7%ab%9e%e8%b5%9b%e4%b8%ad-sort-%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"http:\/\/ggapa.net:81\/2023\/10\/03\/%e7%ae%97%e6%b3%95%e7%ab%9e%e8%b5%9b%e4%b8%ad-sort-%e7%9a%84%e4%bd%bf%e7%94%a8\/","title":{"rendered":"\u7b97\u6cd5\u7ade\u8d5b\u4e2d sort \u7684\u4f7f\u7528"},"content":{"rendered":"

sort<\/h1>\n

\u7b97\u6cd5\u7ade\u8d5b\u4e2d\u7684\u6bcf\u4e00\u6beb\u79d2\u90fd\u662f\u73cd\u8d35\u7684
\n\u800c sort<\/code> \u7684\u4e0d\u6070\u5f53\u4f7f\u7528\u4f1a\u6d6a\u8d39\u5f88\u591a\u989d\u5916\u7684\u65f6\u95f4\uff0c\u5c24\u5176\u662f\u5728\u6570\u636e\u91cf\u8f83\u5927\u7684\u65f6\u5019\u3002
\n\u5e73\u65f6\u9605\u8bfb\u9898\u89e3\u7684\u65f6\u5019\u53d1\u73b0\u5f88\u591a\u5927\u4f6c\u90fd\u4f1a\u8fd9\u6837\u5b50\u5199 sort<\/code> :<\/p>\n

sort(a + 1, a + 1 + n, [](const int& x, const int& y) { return x > y; });<\/code><\/pre>\n

\u6216\u8005\u662f\u8fd9\u6837<\/p>\n

sort(a + 1, a + 1 + n, []greater());<\/code><\/pre>\n

\u7684\u786e\u8fd9\u6837\u5b50\u5199\u90fd\u80fd\u52a0\u5feb\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u95f4\uff0c\u76f8\u6bd4\u4e8e\u901a\u8fc7\u4f20\u9012\u7684 sort<\/code> \u5199\u6cd5\uff1a<\/p>\n

...\nbool cmp(int x, int y) {\n    return x > y;\n}\n...\nsort(a + 1, a + 1 + n, cmp);\n...<\/code><\/pre>\n

\u8fd9\u6837\u5b50\u5199\u6bcf\u4e00\u6b21\u5728\u8c03\u7528\u7684\u65f6\u5019\u90fd\u4f1a\u5b9a\u4e49\u4e24\u4e2a\u53d8\u91cf x\u3001 y \u9020\u6210\u7a0b\u5e8f\u7684\u6548\u7387\u964d\u4f4e\uff0c\u5728\u4f7f\u7528\u7ed3\u6784\u4f53\u7684\u65f6\u5019\u5c24\u5176\u660e\u663e\u3002<\/p>\n

\u4f8b\u5b50<\/h1>\n

UVA11729 Commando War<\/a><\/p>\n

\u5206\u522b\u4f7f\u7528\u4ee5\u4e0b\u4e24\u4efd C++ \u4ee3\u7801<\/p>\n

\/*\nCommando War\nhttps:\/\/vjudge.csgrandeur.cn\/contest\/584210#problem\/A\n*\/\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nconst int maxn = 1005;\n\nstruct Work {\n    int pre, spd;\n}work[maxn];\n\nbool cmp(Work x, Work y) {\n    return x.spd > y.spd;\n}\n\nint n;\n\nint main() {\n    ios::sync_with_stdio(0);\n    cin.tie(0);\n    cout.tie(0);\n    int it = 0;\n    while(true) {\n        cin >> n;\n        it++;\n        if (n == 0) break;\n        for(int i = 1; i <= n; i++)\n            cin >> work[i].pre >> work[i].spd;\n        sort(work + 1, work + 1 + n, cmp);\n        int ans1 = 0, ans2 = 0;\n        for(int i = 1; i <= n; i++) {\n            ans1 += work[i].pre;\n            ans2 = max(ans2, ans1 + work[i].spd);\n        }\n        cout << "Case " << it << ": " << ans2 << endl;\n    }\n    return 0;\n}\n\n\/*\n\u7f16\u7a0b\u8bed\u8a00\nC++14 (GCC 9)\n\u4ee3\u7801\u957f\u5ea6\n843B\n\u7528\u65f6\n10ms\n\u5185\u5b58\n0B\n*\/<\/code><\/pre>\n
\/*\nCommando War\n*\/\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nconst int maxn = 1005;\n\nstruct Work {\n    int pre, spd;\n}work[maxn];\n\n\/*\nbool cmp(Work x, Work y) {\n    return x.spd > y.spd;\n}\n*\/\n\nint n;\n\nint main() {\n    ios::sync_with_stdio(0);\n    cin.tie(0);\n    cout.tie(0);\n    int it = 0;\n    while(true) {\n        cin >> n;\n        it++;\n        if (n == 0) break;\n        for(int i = 1; i <= n; i++)\n            cin >> work[i].pre >> work[i].spd;\n        sort(work + 1, work + 1 + n,\n         [](const Work& x, const Work& y) { return x.spd > y.spd; });\n        int ans1 = 0, ans2 = 0;\n        for(int i = 1; i <= n; i++) {\n            ans1 += work[i].pre;\n            ans2 = max(ans2, ans1 + work[i].spd);\n        }\n        cout << "Case " << it << ": " << ans2 << "\\n";\n    }\n    return 0;\n}\n\/*\n\u7f16\u7a0b\u8bed\u8a00\nC++14 (GCC 9)\n\u4ee3\u7801\u957f\u5ea6\n916B\n\u7528\u65f6\n0ms\n\u5185\u5b58\n0B\n*\/<\/code><\/pre>\n

*UVA \u7684\u6d4b\u8bc4\u673a\u5341\u5206\u611f\u4eba\uff0c\u53ef\u80fd\u4e0e\u5b9e\u9645\u6570\u636e\u76f8\u6bd4\u53ef\u80fd\u6709\u504f\u5dee<\/em><\/p>\n

\u5728 $n \\le 1000$ \u7684\u60c5\u51b5\u4e0b\uff0c\u8db3\u8db3\u6162\u4e86 10ms \uff08\u53ef\u80fd\u8fd8\u4e0d\u6b62\uff0c\uff09\uff0c\u7531\u6b64\u53ef\u89c1\u4e8c\u8005\u7684\u6548\u7387\u5dee\u8ddd<\/p>\n","protected":false},"excerpt":{"rendered":"

sort \u7b97\u6cd5\u7ade\u8d5b\u4e2d\u7684\u6bcf\u4e00\u6beb\u79d2\u90fd\u662f\u73cd\u8d35\u7684 \u800c sort \u7684\u4e0d\u6070\u5f53\u4f7f\u7528\u4f1a\u6d6a\u8d39\u5f88\u591a\u989d\u5916\u7684\u65f6\u95f4\uff0c\u5c24\u5176\u662f\u5728\u6570\u636e\u91cf\u8f83\u5927\u7684 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[22,23],"_links":{"self":[{"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/posts\/656"}],"collection":[{"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/comments?post=656"}],"version-history":[{"count":2,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/posts\/656\/revisions"}],"predecessor-version":[{"id":658,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/posts\/656\/revisions\/658"}],"wp:attachment":[{"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/media?parent=656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/categories?post=656"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/tags?post=656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}