Problem Description
Jumong’s command is one of the algorithm problems based on the situations he encounters during military operations.
Jumong wants to give specific orders to the soldiers in his unit. However, the soldiers may not fully understand Jumong’s commands.
Considering this situation, you need to solve the problem as described below.
Problem Definition
Jumong is trying to issue commands to N soldiers. Each soldier can receive a command exactly once, and this command is in the following format. Each soldier is numbered from 1 to N. Command: `i j k` - Meaning: Soldiers from soldier number i to soldier number j must follow the command, - Soldier number k can ignore it. If the number of soldiers following the command is greater than that of soldier number k, print "YES", otherwise print "NO". N, M i, j, k
Solution Process
To solve this problem, you can approach it in the following steps.
Step 1: Understanding and Analyzing the Problem
The numbers of the soldiers and the range for executing the command are given to solve the problem.
The given i and j represent the range of soldiers who must follow the command, while k is a specific soldier within this range who will not follow the command.
Therefore, by checking the number of soldiers from i to j and whether soldier k is included, you can print “YES” or “NO”.
Step 2: Designing the Algorithm
You can use the following algorithm to solve the problem.
- Input N and M.
- Input the command M times.
- For each command, perform the following tasks:
- Calculate the number of soldiers in the range [i, j].
- Check if soldier k is included in the range.
- If the number of soldiers following the command is greater than that of soldier k, print “YES”, otherwise print “NO”.
Step 3: Implementing C++ Code
Now let’s implement the algorithm designed above into C++ code.
#includeusing namespace std; int main() { int N, M; cin >> N >> M; // Input number of soldiers and commands for (int q = 0; q < M; q++) { // Repeat for each command int i, j, k; cin >> i >> j >> k; // Input command int command_count = j - i + 1; // Calculate number of soldiers from i to j if (command_count > 1) { // More than one soldier must be present. if (k >= i && k <= j) { cout << "NO" << endl; // Soldier k does not follow the command } else { cout << "YES" << endl; // Other soldiers excluding soldier k follow the command. } } else { cout << "NO" << endl; // If there is 1 or fewer soldiers following the command } } return 0; }
Step 4: Explaining the Code
I will provide a brief explanation of each part of the code.
#include <iostream>
: The input-output stream library of C++.using namespace std;
: Uses the std namespace to enhance code readability.int N, M;
: Integer variables that store the number of soldiers and commands, respectively.cin >> N >> M;
: Input the number of soldiers and commands.-
for (int q = 0; q < M; q++)
: Repeats M times to process each command.cin >> i >> j >> k;
: Inputs i, j, k values for each command.int command_count = j - i + 1;
: Calculates the number of soldiers following the command.-
if (command_count > 1)
: The count of following soldiers must be greater than 1 for the command to be valid.-
if (k >= i && k <= j)
: Print “NO” if soldier k is included in the range. -
cout << "YES" << endl;
: Print “YES” if soldier k is outside the range.
-
- Each command output moves to the next line using endl.
Step 5: Test Cases
To validate the accuracy of the program, several test cases must be considered. Below is an example of input and output.
Input: 5 3 1 3 2 1 5 5 2 4 3 Output: NO YES YES
Conclusion
In this C++ coding test course, we learned the algorithmic problem-solving process and C++ programming techniques through Jumong’s command problem.
The key to the problem was to accurately understand the command given and efficiently implement an algorithm to process it.
I would like to emphasize again the importance of considering various cases when solving a given problem.
The process of solving such problems can often help us significantly in solving real-world issues.
In the next course, we will deal with more complex algorithm problems. Thank you!