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 - Rock-paper-scissors
Three people play rock-paper-scissors to a draw. Two of them chose $x$ and $y$ respectively. Determine the third person’s choice.
$0\le x,y\le 2$ (0=Rock, 1=Scissors, 2=Paper)
Input Format
$x,y$
Output Format
Output the integer representing the third person’s choice.
Samples
$x$ | $y$ | Output |
---|---|---|
$0$ | $1$ | $2$ |
$0$ | $0$ | $0$ |
Analysis
A three-way draw occurs in two cases (let $z$ be the third person’s choice):
- $x=y=z$
- All three choices are distinct
The formula can be derived as:
$z=\begin{cases}x & (x=y)\\3-x-y & (x\ne y)\end{cases}$
Code
|
|
B - Nuts
There are $N$ trees. The $i$-th tree has $A_i$ nuts. A person collects $\max(0,A_i-10)$ nuts from each tree. Calculate the total collected nuts.
$1\le N,A_i\le 1000$
Input Format
$N$
$A_1~\dots~A_N$
Output Format
Output the total.
Samples
Sample Input 1
|
|
Sample Output 1
|
|
Collected nuts: $0+7+18=25$
Sample Input 2
|
|
Sample Output 2
|
|
Only the last tree contributes 1 nut.
Analysis
Simulate according to the problem statement.
Code
|
|
C - Tour
A country has $N$ cities and $M$ one-way roads. Count the number of valid ordered pairs $(X,Y)$ where one can travel from city $X$ to city $Y$ through any number of roads (including staying at the same city).
$2\le N\le 2000$
$0\le M\le \min(2000,N(N-1))$
All roads are distinct and not self-looping.
Input Format
$N~M$
$A_1~B_1$
$\vdots$
$A_M~B_M$
Output Format
Output the count.
Samples
Sample Input 1
|
|
Sample Output 1
|
|
Valid pairs: (1,1), (1,2), (1,3), (2,2), (2,3), (3,2), (3,3)
Sample Input 2
|
|
Sample Output 2
|
|
Only self-pairs are valid.
Analysis
Model the country as a directed unweighted graph. Perform DFS from each node to count reachable nodes. Time complexity: $\mathcal O(n^2)$.
Code
Note: Reset the vis
array before each DFS!
|
|
D - Cooking
Two people wash $N$ dishes. Dish $i$ takes $T_i$ minutes. Calculate the minimum time to wash all dishes when working in parallel.
$1\le N\le 100$
$1\le T_i\le 10^3$
Input Format
$N$
$T_1~T_2~\dots~T_N$
Output Format
Output the minimal total time.
Samples
Sample Input 1
|
|
Sample Output 1
|
|
Optimal split: 13 (8+5) vs 10 (3+7+2)
Sample Input 2
|
|
Sample Output 2
|
|
Sample Input 3
|
|
Sample Output 3
|
|
Analysis
Classic 0-1 knapsack problem. Find the maximum subset sum not exceeding half of total time. Answer is total minus this subset sum.
Code
|
|
E - Rush Hour 2
A country has $N$ cities and $M$ bidirectional roads. The travel time through road $i$ at time $t$ is $C_i+\lfloor\frac{D_i}{t+1}\rfloor$. Find the earliest time to reach city $N$ from city $1$, or output -1
.
$2\le N\le 10^5$
$2\le M\le 10^5$
$0\le C_i,D_i\le 10^9$
Input Format
$N~M$
$A_1~B_1~C_1~D_1$
$\vdots$
$A_M~B_M~C_M~D_M$
Output Format
Output the minimal time or -1
.
Samples
Sample Input 1
|
|
Sample Output 1
|
|
Depart at t=1: 1+2+β3/2β=4
Sample Input 2
|
|
Sample Output 2
|
|
Multiple roads and self-loops allowed.
Sample Input 3
|
|
Sample Output 3
|
|
No valid path exists.
Analysis
Model as graph and use Dijkstra’s algorithm. For each edge, optimal departure time is around $\lfloor\sqrt{D}\rfloor-1$. Time complexity: $\mathcal O(M\log N)$.
Code
|
|