Alice and Bob are playing a game using an integer ...
创建于:2025年6月23日
使用 O4 Mini High 回答 Chat01
创建于:2025年6月23日
使用 O4 Mini High 回答 Chat01
Alice and Bob are playing a game using an integer array a
of size n
.
Initially, all elements of the array are colorless. First, Alice chooses 3
elements and colors them red. Then Bob chooses any element and colors it blue (if it was red — recolor it). Alice wins if the sum of the red elements is strictly greater than the value of the blue element.
Your task is to calculate the number of ways that Alice can choose 3
elements in order to win regardless of Bob's actions.
Input
The first line contains a single integer t
(1≤t≤1000
) — the number of test cases.
The first line of each test case contains a single integer n
(3≤n≤5000
).
The second line contains n
integers a1,a2,…,an
(1≤a1≤a2≤⋯≤an≤105
).
Additional constraint on the input: the sum of n
over all test cases doesn't exceed 5000
.
Output
For each test case, print a single integer — the number of ways that Alice can choose 3
elements in order to win regardless of Bob's actions.
Example
InputCopy
6
3
1 2 3
4
1 1 2 4
5
7 7 7 7 7
5
1 1 2 2 4
6
2 3 3 4 5 5
5
1 1 1 1 3
OutputCopy
0
0
10
2
16
0
Note
In the first two test cases, no matter which three elements Alice chooses, Bob will be able to paint one element blue so that Alice does not win.
In the third test case, Alice can choose any three elements. If Bob colors one of the red elements, the sum of red elements will be 14
, and the sum of blue elements will be 7
. If Bob chooses an uncolored element, the sum of red elements will be 21
, and the sum of blue elements will be 7
.
In the fourth test case, Alice can choose either the 1
-st, 3
-rd and 4
-th element, or the 2
-nd, 3
-rd and 4
-th element.
code in java
javaimport java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; int t = Integer.parseInt(br.readLine()); StringBuilder sb = new StringBuilder(); while (t-- > 0) { int n = Integer.parseInt(br.readLine()); int[] a = new int[n]; st = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(st.nextToken()); } // Input is guaranteed sorted non-decreasing; no need to sort. long ans = 0; int max1 = a[n - 1]; int max2 = (n >= 2 ? a[n - 2] : 0); // Iterate over k, the index of the largest red element for (int k = 2; k < n; k++) { int ak = a[k]; // Determine Bob's best outside choice int mout = (k == n - 1 ? max2 : max1); int threshold = Math.max(mout, 2 * ak); // We need a[i] + a[j] + a[k] > threshold // Equivalently, a[i] + a[j] > threshold - a[k] int target = threshold - ak; // Two-pointer count of pairs (i < j < k) with a[i] + a[j] > target int left = 0, right = k - 1; while (left < right) { if (a[left] + a[right] > target) { // All pairs (left, left+1 ... right-1) with this right are valid ans += (right - left); right--; } else { left++; } } } sb.append(ans).append('\n'); } System.out.print(sb.toString()); } }