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 - Large Digits
Problem Statement
Given two three-digit integers $A$ and $B$, find the maximum of their digit sums.
Digit Sum: For example, the digit sum of $123$ is $1+2+3=6$.
Constraints:
$100\le A,B\le 999$
Input Format
$A~~B$
Output Format
Output the maximum digit sum of $A$ and $B$.
Sample
Input | Output |
---|---|
123 234 | 9 |
593 953 | 17 |
100 999 | 27 |
Analysis
Directly implement as per the problem description.
Code
|
|
B - Gentle Pairs
Problem Statement
There are $N$ points with coordinates $(x_i,y_i)$, where all $x$-coordinates are distinct.
How many pairs of points satisfy $-1 \le \text{slope} \le 1$?
Constraints:
$1\le N\le 10^3$
$|x_i|,|y_i|\le 10^3$
$x_i \ne x_j$ ($i < j$)
Input Format
$N$
$x_1~y_1$
$\vdots$
$x_n~y_n$
Output Format
Output the answer.
Sample
Sample Input1
|
|
Sample Output1
|
|
There are three points: $(0,0)$, $(1,2)$, and $(2,1)$.
- From $(0,0)$ to $(1,2)$, slope is $2$;
- From $(0,0)$ to $(2,1)$, slope is $\frac{1}{2}$;
- From $(1,2)$ to $(2,1)$, slope is $-1$.
There are 2 valid pairs.
Sample Input2
|
|
Sample Output2
|
|
Only one point exists. Output $0$.
Sample Input3
|
|
Sample Output3
|
|
Analysis
$$-1 \le \frac{y_1-y_2}{x_1-x_2} \le 1$$$$|\frac{y_1-y_2}{x_1-x_2}| \le 1$$$$|y_1-y_2|\le|x_1-x_2|$$
We use integer operations to avoid floating-point errors.
Code
Enumerate all pairs.
|
|
C - 1-SAT
Problem Statement
Given $N$ strings $S_1,S_2,...,S_N$. Each string consists of lowercase letters and may have at most one !
at the beginning.
Find any string $S$ such that both $S$ and !
+$S$ exist in the list. If none exist, output satisfiable
.
Constraints:
$1\le N\le 10^5$
$1\le |S_i|\le 10$
Input Format
$N$
$S_1$
$\vdots$
$S_N$
Output Format
If a valid string exists, output any such string.
Otherwise, output satisfiable
.
Sample
Sample Input1
|
|
Sample Output1
|
|
$S_1$ is a
and $S_2$ is !a
, so $S_1$ is valid.
$S_5$ is d
and $S_6$ is !d
, so d
is also a valid output.
Sample Input2
|
|
Sample Output2
|
|
No valid string exists.
Analysis
A brute-force $\mathcal O(N^2)$ approach is infeasible.
Optimization:
- Store strings starting with
!
(after removing!
) in aset
. - Check other strings against this
set
in $\mathcal O(\log N)$ per query.
Overall complexity: $\mathcal O(N\log N)$.
Code
|
|
D - Choose Me
Problem Statement
Refer to the original problem page for details.
Constraints:
$1\le N\le 10^5$
$1\le A_i,B_i\le 10^9$
Input Format
$N$
$A_1~B_1$
$\vdots$
$A_N~B_N$
Output Format
Output the minimum number of cities to visit.
Sample
Sample Input1
|
|
Sample Output1
|
|
After visiting the third city, Aoki gets 5 votes and Takahashi gets 6.
Sample Input2
|
|
Sample Output2
|
|
Visiting any three cities gives Aoki 4 votes and Takahashi 9.
Sample Input3
|
|
Sample Output3
|
|
Analysis
Our goal is to minimize the vote difference. Initially, the difference is the sum of all $A_i$.
Each city $i$ reduces the difference by $2A_i + B_i$. We greedily select cities with the largest reduction first using a priority queue.
Code
Note: Must use long long to avoid overflow.
|
|