PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#PS0000284. Tổng khoảng cách theo giá trị (Sum of Distances)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Tổng khoảng cách theo giá trị (Sum of Distances)
Sum of Distances
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given an integer sequence , calculate for every index . If no other index carries the same value, . Output the results in original index order.
Input
First line: integer . Second line: exactly integers .
Output
Print integers on one line, separated by spaces.
Subtasks
- Subtask 1 (20 points): ; .
- Subtask 2 (30 points): ; ; all elements are pairwise distinct.
- Subtask 3 (50 points): ; .
Example 1
Input
5
1 3 1 1 2
Output
5 0 3 4 0
Explanation
Value 1 occurs at positions 1, 3, 4, giving 5, 3, 4. The other values occur once.
Example 2
Input
3
0 5 3
Output
0 0 0
Explanation
All values are distinct, so every answer is zero.