{"id":132,"date":"2023-04-16T21:21:00","date_gmt":"2023-04-16T13:21:00","guid":{"rendered":"https:\/\/solost.cn\/?p=132"},"modified":"2023-04-16T21:24:57","modified_gmt":"2023-04-16T13:24:57","slug":"239","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=132","title":{"rendered":"Educational Codeforces Round 99 (Rated for Div. 2) &#8211; D. Sequence and Swaps"},"content":{"rendered":"<h2>\u9898\u76ee<\/h2>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/11\/image1-9.png\" alt=\"image(1).png\" title=\"image(1).png\"><\/p>\n<h2>\u9898\u76ee\u94fe\u63a5<\/h2>\n<p><a href=\"https:\/\/codeforces.com\/problemset\/problem\/1455\/D\">\u94fe\u63a5<\/a><\/p>\n<h2>\u9898\u76ee\u5927\u610f<\/h2>\n<p>\u8fd9\u9053\u9898\u7ed9\u4f60\u4e00\u4e2a\u5e8f\u5217\uff0c\u7136\u540e\u7136\u4f60\u628a\u4ed6\u53d8\u6210\u4e00\u4e2a\u975e\u4e25\u683c\u4e0a\u5347\u5e8f\u5217\uff0c\u7ed9\u4f60\u4e00\u4e2a\u6570x\uff0c\u6ee1\u8db3<code>x &gt; a[i]<\/code>\u624d\u80fd\u6362\u3002\u95ee\u4f60\u6700\u5c11\u591a\u5c11\u6b21\u64cd\u4f5c\u80fd\u628a\u5e8f\u5217\u53d8\u6210\u4e0a\u5347\u7684\uff0c\u5982\u679c\u4e0d\u80fd\u8f93\u51fa-1<\/p>\n<h2>\u9898\u89e3<\/h2>\n<p>\u770b\u4e86\u9898\u89e3\uff0c\u6211\u7684\u60f3\u6cd5\u662f\u6392\u5e8f\uff0c\u7136\u540e\u5224\u65ad<\/p>\n<pre><code class=\"lang-cpp\">for(int i = 1; i &lt;= n; i++){\n        cin &gt;&gt; a[i];\n        b[i] = a[i];\n        if(i &gt; 1 &amp;&amp; a[i] &lt; a[i - 1]) f = 0;\n    }\n    if(f){\n        cout &lt;&lt; &quot;0\\n&quot;;\n        return;\n    }\n    b[n + 1] = x;\n    sort(b + 1, b + n + 2);\n    int ans = 0;\n    for(int i = 1; i &lt;= n; i++){\n        if(a[i] &lt;= x){\n            if(a[i] != b[i]){\n                cout &lt;&lt; &quot;-1\\n&quot;;\n                return;\n            }\n        }\n        else{\n            if(a[i] != b[i]){\n                ans++;\n            }\n        }\n    }<\/code><\/pre>\n<p>\u7136\u540e\u53d1\u73b0\u5728\u80e1\u626f\uff0c\u6709\u7684\u65f6\u5019\u591a\u6362\u4e86\u3002<\/p>\n<p>\u770b\u4e86\u9898\u89e3\u7684\u601d\u8def\uff0c\u771f\u725b\u903c\uff0c\u4f46\u662f\u5192\u72af\u5230\u6211\u4e86\uff01\uff01\uff01<\/p>\n<blockquote><p>\u672c\u9898\u5bf9\u4e8e\u4ee3\u7801\u80fd\u529b\u8981\u6c42\u5e76\u4e0d\u9ad8\uff0c\u5c31\u662f\u770b\u4f60 ~\u8111\u5b50\u597d\u4e0d\u597d\u4f7f~ \u80fd\u4e0d\u80fd\u60f3\u5230\u3002<\/p><\/blockquote>\n<p>\u9996\u5148\uff0c\u5982\u679c\u6709\u4e00\u4e2a<code>a[i] &gt; x<\/code>\u4f46\u662f\u6ca1\u6709\u4ea4\u6362\uff0c\u90a3\u4e48\u5230\u4e86\u540e\u9762\u60f3\u6362\u7684\u65f6\u5019<code>a[j] &gt; x<\/code>\u4f46\u662f\u524d\u9762\u6709\u4e00\u4e2a<code>a[i] &gt; x<\/code>,\u4e5f\u5c31\u662f\u524d\u9762\u6709\u4e00\u4e2a\u6570\u6bd4\u5f53\u524d\u4f4d\u7f6e\u8fd8\u5927\u3002\u6240\u4ee5\uff0c\u8981\u6362\u5c31\u5fc5\u987b\u5f97\u6362\uff0c\u5982\u679c\u4e00\u4e2a\u5e8f\u5217\u672c\u6765\u5c31\u6709\u5e8f\uff0c\u90a3\u4e48\u6362\u4e86\u53cd\u800c\u589e\u52a0\u4e86\u6b21\u6570\u3002\u6240\u4ee5\u8981\u6362\u5c31\u5fc5\u987b\u4ece<code>a[i] &gt; x<\/code>\u7684\u65f6\u5019\u5c31\u5f00\u59cb\u6362\u3002\u5982\u679c\u540e\u9762\u5df2\u7ecf\u6709\u5e8f\uff0c\u90a3\u4e48\u6362\u4e86\u4f1a\u589e\u52a0\u6b21\u6570\u3002\u5982\u679c\u6362\u5b8c\u6700\u540e\u4e00\u4e2a\u65e0\u5e8f\u70b9a[j]\uff0c\u90a3\u4e48<code>a[j]<\/code>\u5fc5\u7136\u5927\u4e8e\u540e\u9762\u67d0\u4e2a\u6570\uff0c\u4f46\u662f\u6362\u8fc7\u53bb\u7684x\u5fc5\u7136\u6bd4\u90a3\u4e2a\u6570\u8fd8\u5927\uff0c\u6240\u4ee5\u5c31\u7b97\u5f80\u540e\u904d\u5386\u4e5f\u6ca1\u7528\u3002\u6240\u4ee5\u6211\u4eec\u53ea\u7528\u904d\u5386\u5230\u6700\u540e\u4e00\u4e2a\u65e0\u5e8f\u70b9\u5373\u53ef<\/p>\n<pre><code class=\"lang-cpp\">#include &lt;map&gt;\n#include &lt;set&gt;\n#include &lt;cmath&gt;\n#include &lt;queue&gt;\n#include &lt;stack&gt;\n#include &lt;vector&gt;\n#include &lt;bitset&gt;\n#include &lt;cstdio&gt;\n#include &lt;cstdlib&gt;\n#include &lt;cstring&gt;\n#include &lt;fstream&gt;\n#include &lt;iostream&gt;\n#include &lt;algorithm&gt;\n#include &lt;unordered_map&gt;\n#include &lt;unordered_set&gt;\n\n#define x first\n#define y second\n#define endl &#039;\\n&#039;\n#define IOS                       \\\n    ios_base::sync_with_stdio(0); \\\n    cin.tie(0);                   \\\n    cout.tie(0);\n\nusing namespace std;\n\ntypedef long long ll;\ntypedef pair&lt;ll, ll&gt; pll;\ntypedef pair&lt;int, int&gt; pii;\ntypedef unsigned long long ull;\n\nconst int INF = 0x3f3f3f3f, mod = 1000000007;\nconst int N = 510;\n\nint n, x;\nint a[N];\n\nvoid solve() {\n    cin &gt;&gt; n &gt;&gt; x;\n    int f = 0;\n    for(int i = 1; i &lt;= n; i++){\n        cin &gt;&gt; a[i];\n        if(i &gt; 1 &amp;&amp; a[i] &lt; a[i - 1]) f = i;\n    }\n    if(!f){\n        cout &lt;&lt; &quot;0\\n&quot;;\n        return;\n    }\n    int ans = 0;\n    for(int i = 1; i &lt;= f; i++){\n        if(a[i] &gt; x) swap(a[i], x), ans++;\n    }\n    for(int i = 1; i &lt; n; i++){\n        if(a[i] &gt; a[i + 1]){\n            cout &lt;&lt; &quot;-1\\n&quot;;\n            return;\n        }\n    }\n    cout &lt;&lt; ans &lt;&lt; endl;\n}\nint main()\n{\n\/\/    IOS;\n    int t = 1;\n    cin &gt;&gt; t;\n    while (t--) {\n        solve();\n    }\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee \u9898\u76ee\u94fe\u63a5 \u94fe\u63a5 \u9898\u76ee\u5927\u610f \u8fd9\u9053\u9898\u7ed9\u4f60\u4e00\u4e2a\u5e8f\u5217\uff0c\u7136\u540e\u7136\u4f60\u628a\u4ed6\u53d8\u6210\u4e00\u4e2a\u975e\u4e25\u683c\u4e0a\u5347\u5e8f\u5217\uff0c\u7ed9\u4f60\u4e00\u4e2a\u6570x\uff0c\u6ee1\u8db3x &gt; a[i]\u624d\u80fd\u6362\u3002\u95ee\u4f60\u6700\u5c11\u591a\u5c11\u6b21\u64cd\u4f5c\u80fd\u628a\u5e8f\u5217\u53d8\u6210\u4e0a\u5347\u7684\uff0c\u5982\u679c\u4e0d\u80fd\u8f93\u51fa-1 \u9898\u89e3 \u770b\u4e86\u9898\u89e3\uff0c\u6211\u7684\u60f3\u6cd5\u662f\u6392\u5e8f\uff0c\u7136\u540e\u5224\u65ad for(int i = 1; i &lt;= n; i++){ cin &gt;&gt; a[i]; b[i] = a[i]; if(i &gt; 1 &amp;&amp; a[i] &lt; a[i - 1]) f = 0; } if(f){ cout &lt;&lt; &quot;0\\n&quot;; return; } b[n + 1] = x; sort(b + 1, b + n + 2); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[5,7],"class_list":["post-132","post","type-post","status-publish","format-standard","hentry","category-solution","tag-5","tag-7"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=132"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/132\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}