Translation Notice
This article was machine-translated using DeepSeek-R1.
- Original Version: Authored in Chinese by myself
- Accuracy Advisory: Potential discrepancies may exist between translations
- Precedence: The Chinese text shall prevail in case of ambiguity
- Feedback: Technical suggestions regarding translation quality are welcomed
A - Median?
Problem Statement
Given three positive integers $a$, $b$, and $c$, determine whether $b$ is the median of the three numbers (i.e., the second value when sorted in non-decreasing order, not the average).
Constraints:
$1 \le a, b, c \le 100$
Input Format
$a~b~c$
Output Format
Print Yes
if $b$ is the median; otherwise, print No
.
Sample Cases
$a$ | $b$ | $c$ | Output |
---|---|---|---|
5 | 3 | 2 | Yes |
2 | 5 | 3 | No |
100 | 100 | 100 | No |
Analysis
Since this is an A-level problem, the solution is straightforward. I misread it as the average during the contest and got WA.. (The description should be clear enough.)
We can directly sort the three numbers or check if either $a \le b \le c$ or $c \le b \le a$ holds.
Code
|
|
B - Distance Between Tokens
Problem Statement
On an $H \times W$ grid, exactly two cells contain tokens, and the rest are empty. Starting from either token, find the minimum number of moves required to reach the other token by moving up, down, left, or right.
Input Format
The first line contains $H$ and $W$ separated by a space. The next $H$ lines each contain a string of length $W, where
-denotes an empty cell and
o` denotes a token.
Output Format
Print the minimum number of moves required.
Sample Cases
Sample Input 1
|
|
Sample Output 1
|
|
Sample Input 2
|
|
Sample Output 2
|
|
Analysis
No BFS is needed. Since there are no obstacles, directly compute the Manhattan distance between the two tokens: $|x_1 - x_2| + |y_1 - y_2|$.
Code
|
|
C - Max - Min Query
Problem Statement
Given an initially empty sequence $S$, process $Q$ operations:
1 x
: Append $x$ to $S$.2 x c
: Remove $\min(c, \text{count of } x)$ occurrences of $x$ from $S$.3
: Print the difference between the maximum and minimum values in $S`.
Constraints:
$1 \le Q \le 2 \times 10^5$
$0 \le x \le 10^9$
$1 \le c \le Q$
Input Format
$Q$
$\text{query}_1$
$\text{query}_2$
$\vdots$
$\text{query}_Q$
Output Format
For each 3
operation, print the difference.
Analysis
A classic STL exercise.
Use a multiset
or map
(C++ specific). Here’s a map
approach:
mp[x]
returns the count of $x$ (auto-initialized to 0).mp.begin()->first
gives the minimum element.mp.rbegin()->first
gives the maximum element.mp.erase(x)
removes all occurrences of $x$.
Each query can be handled efficiently with these operations.
Code
|
|
D - FizzBuzz Sum Hard
Problem Statement
Compute the sum of integers from $1$ to $N$ that are not multiples of $A$ or $B$.
Constraints:
$1 \le N, A, B \le 10^9$
Input Format
$N~A~B$
Output Format
Print the answer.
Analysis
By inclusion-exclusion principle, the sum of multiples of $A$ or $B$ is:
$\text{sum}(A) + \text{sum}(B) - \text{sum}(\text{lcm}(A,B))$.
Define:
- $f(n) = 1 + 2 + \dots + n = \frac{n(n+1)}{2}$
- $\text{sum}(x) = x \cdot f(\lfloor N/x \rfloor)$ (sum of multiples of $x$ up to $N$)
The final answer is:
$f(N) - \text{sum}(A) - \text{sum}(B) + \text{sum}(\text{lcm}(A,B))$
Time complexity is dominated by computing $\text{lcm}(A,B)$ using GCD.
Code
|
|
E - Distance Sequence
Problem Statement
Count the number of sequences $A = (A_1, \dots, A_N)$ modulo $998244353$ that satisfy:
- $1 \le A_i \le M$ for all $i$
- $|A_i - A_{i+1}| \ge K$ for all $1 \le i < N$
Constraints:
$2 \le N \le 1000$
$1 \le M \le 5000$
$0 \le K < M$
Input Format
$N~M~K$
Output Format
Print the count modulo $998244353$.
Analysis
Dynamic Programming (DP) approach:
- Define $\text{dp}(i, j)$ as the number of valid sequences ending with $j$ at position $i$.
- Transition: $\text{dp}(i, j) = \sum \text{dp}(i-1, p)$ where $|j - p| \ge K$.
Optimize using prefix/suffix sums to reduce time complexity from $\mathcal{O}(NM^2)$ to $\mathcal{O}(NM)$. Special case $K=0$ has answer $M^N \bmod 998244353$.
Code
|
|