C - お土産選び / Choosing Souvenirs 解説 by admin
GPT 5.2 HighOverview
This is a problem where, among products satisfying conditions on price and tastiness, you need to find the optimal product number with the priority order: (minimum price → maximum tastiness → minimum product number).
Approach
There are two main things to do in this problem:
Checking whether conditions are satisfied
For each product, check whether both of the following are satisfied:- \(L \le P_i \le R\)
- \(S_i \ge T\)
Selecting the “best” among products that satisfy the conditions
The priority order for selection is as follows:- Cheapest price (\(P\) minimum)
- If prices are the same, highest tastiness (\(S\) maximum)
- If still tied, smallest product number (\(i\) minimum)
The key insight here is that it is sufficient to scan through all products once and update the best candidate along the way.
Since \(N \le 2\times 10^5\), a full scan (\(O(N)\)) is more than fast enough. Alternatively, collecting all qualifying products and sorting them is also possible, but it is unnecessarily heavy at \(O(N\log N)\) (and the implementation becomes slightly more complex).
As a concrete example, suppose the following 3 products satisfy the conditions:
| Number | Price \(P\) | Tastiness \(S\) |
|---|---|---|
| 2 | 100 | 50 |
| 5 | 100 | 70 |
| 3 | 120 | 80 |
The cheapest are the two with price 100 (numbers 2 and 5). Among them, the one with the highest tastiness is number 5 (\(S=70\)), so the answer is 5.
Algorithm
- Initialize
bestas “not yet found.” - Read each product \((P_i, S_i)\) in order from \(i=1\) to \(N\).
- Only consider it as a candidate if it satisfies the conditions \(L \le P_i \le R\) and \(S_i \ge T\).
- Use
(P, -S, i)as the comparison key for candidates.- Smaller
Pis better (cheapest) - Smaller
-Sis better ⇔ largerSis better (highest tastiness) - Smaller
iis better (smallest number)
- Smaller
- Update
bestif it is unset or if the candidate’s key is smaller than that ofbest. - At the end, output the product number of
bestif it exists, otherwise output-1.
Since Python tuples are compared in lexicographic order, simply comparing (P, -S, i) directly achieves the required priority order.
Complexity
- Time complexity: \(O(N)\) (just a single pass through all products)
- Space complexity: \(O(1)\) (only storing the best candidate)
Implementation Notes
To make “maximum tastiness” easy to handle with tuple comparison, we use
-Sto convert it into a minimization problem.Using something like
best = Noneand setting it when the first qualifying product is found is a clean approach.Since the input can have up to \(2\times 10^5\) lines, using
sys.stdin.readlineensures stable and fast reading.Source Code
import sys
def main():
input = sys.stdin.readline
N, L, R, T = map(int, input().split())
best = None # (P, -S, idx)
for i in range(1, N + 1):
P, S = map(int, input().split())
if L <= P <= R and S >= T:
cand = (P, -S, i)
if best is None or cand < best:
best = cand
print(best[2] if best is not None else -1)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
投稿日時:
最終更新: