#CT0000076. Cặp số gần K nhất (Pair Sum Closest to K)
Bài tập này chưa được xếp vào lộ trình.
Mục lục đề ⌄
Cặp số gần K nhất (Pair Sum Closest to K)
Pair Sum Closest to K
Version: Phuoc Hung OJ Extended
Problem Statement
You are given integers and an integer .
Choose two distinct positions (). The absolute difference between their sum and is
Find the minimum absolute difference over all valid pairs. Equal values may be selected if they occur at distinct positions. Print the minimum difference, not the indices.
Input
Line 1 contains integers , .
Line 2 contains integers .
Output
Output one integer: the minimum absolute difference.
Subtasks
- Subtask 1 (30 points): ; ; for every .
- Subtask 2 (30 points): ; ; for every .
- Subtask 3 (40 points): ; ; for every .
Examples
Example 1
Input
5 10
1 4 7 13 20
Output
1
Explanation: Values 4 and 7 occur at different positions, giving sum 11 and difference |11 − 10| = 1; no pair sums to 10.
Example 2
Input
4 -2
-7 -1 3 10
Output
2
Explanation: The pair −7 and 3 sums to −4, so the difference is |−4 − (−2)| = 2; no other pair is closer.
Example 3
Input
2 4
2 2
Output
0
Explanation: The two values of 2 occur at distinct indices. Their sum is exactly K = 4, so the difference is 0.
Liên quan
Trong các cuộc thi sau: