#FT0000200. Tổng của các giá trị xuất hiện nhiều nhất trên mỗi cửa sổ (Find X-Sum of All K-Long Subarrays II)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tổng của các giá trị xuất hiện nhiều nhất trên mỗi cửa sổ (Find X-Sum of All K-Long Subarrays II)
Find X-Sum of All K-Long Subarrays II
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an array of positive integers and two integers .
Consider a contiguous subarray containing exactly elements. For each distinct value occurring in the subarray, let denote its frequency.
Rank distinct values in descending order of , breaking ties by favoring the larger numerical value.
Keep all occurrences of the first distinct values in this ranking and sum the retained elements. If the subarray contains fewer than distinct values, retain every element. The resulting sum is called its -sum.
For every starting position , calculate the -sum of and print these answers in increasing order of .
Input
- The first line contains three integers : the array length, window length, and number of top distinct values.
- The second line contains exactly integers , in their original order.
Output
Print exactly integers on one line, separated by spaces. Integer is the -sum of the window beginning at position .
Subtasks
- Subtask 1 (20 points): ; ; for every .
- Subtask 2 (30 points): ; ; for every .
- Subtask 3 (50 points): ; ; for every .
Examples
Input
8 6 2
1 1 2 2 3 4 2 3
Output
6 10 12
Explanation
There are windows.
- For , values and each occur twice, while and occur once. Keeping both copies of and both copies of gives .
- For , value occurs three times. Values each occur once, so the larger value wins the tie. The retained elements sum to .
- For , value occurs three times, value twice, and value once. Keeping all s and s gives .
Hence the answers are 6 10 12.