Official

B - 12435 Editorial by en_translator


If you are new to learning programming and do not know where to start, please try Problem A “Welcome to AtCoder” from practice contest. There you can find a sample code for each language.
Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in “AtCoder Beginners Selection” (https://atcoder.jp/contests/abs).


First of all, the operations can be limited to the following four cases:

  • Swap \(A_1\) and \(A_2\).
  • Swap \(A_2\) and \(A_3\).
  • Swap \(A_3\) and \(A_4\).
  • Swap \(A_4\) and \(A_5\).

Thus, we can try all of these four operations. If any of them makes \(A\) ascending, then the answer is Yes; otherwise, the answer is No.

Sample code (Python3)

a = list(map(int, input().split()))

b = a.copy()
b[0], b[1] = b[1], b[0]
if b == [1, 2, 3, 4, 5]:
    print("Yes")
    exit()

b = a.copy()
b[1], b[2] = b[2], b[1]
if b == [1, 2, 3, 4, 5]:
    print("Yes")
    exit()

b = a.copy()
b[2], b[3] = b[3], b[2]
if b == [1, 2, 3, 4, 5]:
    print("Yes")
    exit()

b = a.copy()
b[3], b[4] = b[4], b[3]
if b == [1, 2, 3, 4, 5]:
    print("Yes")
    exit()

print("No")

This program can be written even simpler using a for statement.

Sample code (Python3)

a = list(map(int, input().split()))
for i in range(4):
    b = a.copy()
    b[i], b[i + 1] = b[i + 1], b[i]
    if b == [1, 2, 3, 4, 5]:
        print("Yes")
        exit()
print("No")

Sample code (C++)

#include <bits/stdc++.h>
using namespace std;
int main()
{
    vector<int> a(5);
    for (int &i : a)
        cin >> i;
    vector<int> ans = {1, 2, 3, 4, 5};
    for (int i = 0; i < 4; i++)
    {
        vector<int> b = a;
        swap(b[i], b[i + 1]);
        if (b == ans)
        {
            cout << "Yes" << endl;
            return 0;
        }
    }
    cout << "No" << endl;
    return 0;
}

posted:
last update: