give solution in cpp C. Coloring Game time limit ...
Tạo vào: 23 tháng 6, 2025
Trả lời bằng O4 Mini High bởi Chat01
Tạo vào: 23 tháng 6, 2025
Trả lời bằng O4 Mini High bởi Chat01
give solution in cpp
C. Coloring Game
time limit per test2.5 seconds
memory limit per test256 megabytes
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 \le t \le 1000) — the number of test cases.
The first line of each test case contains a single integer n (3 \le n \le 5000).
The second line contains n integers a_1, a_2, \dots, a_n (1 \le a_1 \le a_2 \le \cdots \le a_n \le 10^5).
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.
cpp#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } // Ensure the array is sorted (though input guarantees non-decreasing order) sort(a.begin(), a.end()); ll ans = 0; // For each k as the index of the third red element (0-based) for (int k = 2; k < n; k++) { // T = max(a[k], a[n-1] - a[k]) int T = max(a[k], a[n-1] - a[k]); // Count pairs (i, j) with 0 <= i < j < k and a[i] + a[j] > T int l = 0, r = k - 1; while (l < r) { if (a[l] + a[r] > T) { // All pairs (l, l+1), (l, l+2), ..., (l, r-1) with r fixed ans += (r - l); r--; } else { l++; } } } cout << ans << "\n"; } return 0; }
Explanation of the approach:
Reformulating the win conditions
Alice picks three indices . Let .
Combining both, the necessary and sufficient condition for the triple to be safe is
One can show this reduces to
Two-pointer counting
Complexity
This yields a total runtime of roughly operations, which is efficient enough under the given constraints.