Purchasing Power (APCS 2021-01 Beginner)
Points 100 1.0s 256MThere are \(n\) products in the market, and you know the prices of these \(n\) products over the last \(3\) days.
You want to buy every product whose recent price fluctuates greatly, that is, every product for which the difference between the highest and lowest of its last three days' prices is at least \(d\). The cost of buying a product is the average of its last \(3\) days' prices (which is guaranteed to be an integer).
Given the last \(3\) days' prices of the \(n\) products and the value \(d\), output the total number of products bought and the total cost.
Input
The first line contains two integers \(n\) and \(d\), the number of products and the fluctuation threshold.
The next \(n\) lines each contain three integers, the last \(3\) days' prices of the \(i\)-th product.
Constraints
- \(1 \le n \le 100\)
- \(0 \le d \le 100\)
- Every price is an integer in \([0, 100]\)
- For every bought product (price range \(\ge d\)), the average of its \(3\) prices is guaranteed to be an integer
Output
On one line, output two numbers separated by a space: the number of products bought and the total cost.
Scoring
The time limit for each test case is \(1\) second; your score is the sum over the test cases you pass. The subtasks are:
- Subtask 1 (\(50\) points): \(n = 1\).
- Subtask 2 (\(50\) points): \(1 \le n \le 100\).
Sample Input 1
1 3
24 27 21
Sample Output 1
1 24
Sample Explanation 1
The range is \(27 - 21 = 6 \ge 3\), so it is bought; the average is \((24+27+21)/3 = 24\).
Sample Input 2
3 4
24 33 42
51 48 60
77 77 77
Sample Output 2
2 86
Sample Explanation 2
Product \(1\) has range \(18 \ge 4\), average \(33\); product \(2\) has range \(12 \ge 4\), average \(53\); product \(3\) has range \(0 < 4\) and is not bought. Two products are bought, for a total of \(33 + 53 = 86\).
Source
APCS programming exam, January 2021, Problem 1.
Log in to write and submit code.
Log in