You are given n points with integer coordinates x1…xn which lie on a number lineFor some integer s we construct segments sx1 sx2 … sxn Note that if xis then the segment will look like xis The segment
To solve this problem, we can use the concept of prefix sums. First, we initialize an array "prefix" of size 109 with all elements set to 0.
For each point xi, we increment prefix[xi] by 1. This step counts the number of times each point appears in the given coordinates.
Next, we iterate over each point xi again and calculate the sum of fp for s = xi. We can do this by initializing a variable "sum" to 0 and iterating from 1 to 109. For each integer p, we add prefix[p] to sum if p is less than or equal to xi, otherwise we subtract prefix[p] from sum. This step calculates the sum of fp for each point.
Finally, we output the sum for each test case.
Here is the implementation in Python:
Function to calculate the sum of fp for each test case
def calculate_sum_of_fp(t, test_cases): for _ in range(t): n = test_cases[][0] coordinates = test_cases[][1]
# Initialize prefix array
prefix = [0] * 110
# Count the number of times each point appears
for point in coordinates:
prefix[point] += 1
# Calculate the sum of fp for each point
result = []
for xi in coordinates:
s = xi
sum = 0
for p in range(1, 110):
if p <= s:
sum += prefix[p]
else:
sum -= prefix[p]
result.append(sum)
# Output the sum for each test case
print(*result)
Read input
t = int(input()) test_cases = [] for _ in range(t): n = int(input()) coordinates = list(map(int, input().split())) test_cases.append((n, coordinates))
Calculate and output the sum of fp for each test case
calculate_sum_of_fp(t, test_cases
原文地址: https://www.cveoy.top/t/topic/iwS8 著作权归作者所有。请勿转载和采集!