A - 図鑑コンプリート / Encyclopedia Completion Editorial by admin
Qwen3-Coder-480BOverview
Given a list of flower variety numbers, select only the varieties that appear for the first time and find their count and sum.
Analysis
In this problem, we need to efficiently manage whether each variety number has “already been seen.”
For example, if the flower variety numbers are given as [1, 3, 1, 2, 3], the varieties to be included in the encyclopedia are those that “appear for the first time”: [1, 3, 2].
In this case, the count is \(3\) and the sum is \(1 + 3 + 2 = 6\).
A naive approach would be to check, for each flower we are currently looking at, whether its variety has appeared before. However, in the worst case, this requires checking all previous elements for each element, resulting in a time complexity of \(O(N^2)\). Since the constraint allows \(N\) up to \(2 \times 10^5\), this would not be fast enough (TLE).
Therefore, we need a way to quickly determine whether each variety has “already been seen.” A set is useful for this purpose. A set can check for the existence of an element and add an element in \(O(1)\) on average, making it very efficient.
Algorithm
- Receive the list of flower variety numbers as input.
- Prepare a set
seento record “already seen varieties.” - Initialize the count of flowers in the encyclopedia
countand the sum of variety numberstotal. - For each flower variety number, do the following:
- If the variety is not in
seen:- Add it to
seen - Increment
countby 1 - Add the variety number to
total
- Add it to
- If the variety is not in
- Finally, output
countandtotal.
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(1)\) (Since the number of distinct variety numbers is at most 100, the size of the set can be treated as a constant)
Implementation Notes
- By using Python’s
set, we can efficiently check for the existence of an element and add elements. - Since the range of variety numbers is \(1\) to \(100\), which is small, the space complexity is effectively constant.
## Source Code
```python
N = int(input())
S = list(map(int, input().split()))
seen = set()
count = 0
total = 0
for s in S:
if s not in seen:
seen.add(s)
count += 1
total += s
print(count, total)
This editorial was generated by qwen3-coder-480b.