Official

A - 321-like Checker Editorial by en_translator


For beginners

Approach 1: decompose \(N\) into digits

One can decompose \(N\) into digits as follows.

  • First, let \(D=[]\).
  • While \(N>0\), repeat the following.
    • Push to \(D\) the remainder when \(N\) is divided by \(10\).
    • Then, replace \(N\) with the quotient when is \(N\) divided by \(10\).
  • Finally, reverse \(D\).

For example, when \(N=321\) the procedure goes as follows.

  • Start with \(D = []\).
  • Since \(321>0\), push \(1\) to the tail of \(D\), and replace \(N\) with \(32\). Now, \(D=[1]\).
  • Since \(32>0\), push \(2\) to the tail of \(D\), and replace \(N\) with \(3\). Now, \(D=[1,2]\).
  • Since \(3>0\), push \(3\) to the tail of \(D\), and replace \(N\) with \(0\). Now, \(D=[1,2,3]\).
  • Now that \(N=0\), terminate the loop.
  • Finally, reverse \(D\) to obtain \(D=[3,2,1]\).

With this \(D\), one can check the condition in the problem statement using a loop to solve the problem.
To check the condition, it is good idea to deform the condition

  • Yes if (\(i\)-th digit) \(>\) (\((i+1)\)-th digit) for all \(i\)

into

  • No if (\(i\)-th digit) \(\le\) (\((i+1)\)-th digit) for all \(i\).

Sample code (Python):

N = int(input())
D = []
while N>0:
    D.append(N%10)
    N //= 10
D.reverse()

for i in range(1,len(D)):
    if D[i-1]<=D[i]:
        print("No")
        exit()

print("Yes")


Approach 2: receive \(N\) as a string

If one can somehow receive \(N\) as a string, then its \(i\)-th character is the \(i\)-th significant digit of \(N\).
Then one can use that string to check the condition, where \(N\) does no longer need to be decomposed into digits.

Sample code (C++):

#include<bits/stdc++.h>

using namespace std;

int main(){
  string s;
  cin >> s;
  for(int i=1;i<s.size();i++){
    if(s[i-1]<=s[i]){
      cout << "No\n";
      return 0;
    }
  }
  cout << "Yes\n";
  return 0;
}

posted:
last update: