C. Delete Two Elements
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Monocarp has got an array a consisting of n integers. Let's denote k as the mathematic mean of these elements (note that it's possible that k is not an integer).
The mathematic mean of an array of n elements is the sum of elements divided by the number of these elements (i. e. sum divided by n).
Monocarp wants to delete exactly two elements from a so that the mathematic mean of the remaining (n−2) elements is still equal to k.
Your task is to calculate the number of pairs of positions [i,j] (i<j) such that if the elements on these positions are deleted, the mathematic mean of (n−2) remaining elements is equal to k (that is, it is equal to the mathematic mean of n elements of the original array a).
Input
The first line contains a single integer t (1≤t≤1e4) — the number of testcases.
The first line of each testcase contains one integer n (3≤n≤2⋅1e5) — the number of elements in the array.
The second line contains a sequence of integers a1,a2,…,an (0≤ai≤1e9), where ai is the i-th element of the array.
The sum of nn over all testcases doesn't exceed 2⋅1052⋅105.
Output
Print one integer — the number of pairs of positions [i,j] (i<j) such that if the elements on these positions are deleted, the mathematic mean of (n−2) remaining elements is equal to k (that is, it is equal to the mathematic mean of n elements of the original array a).
Example
input
4 4 8 8 8 8 3 50 20 10 5 1 4 7 3 5 7 1 2 3 4 5 6 7output
6 0 2 3Note
In the first example, any pair of elements can be removed since all of them are equal.
In the second example, there is no way to delete two elements so the mathematic mean doesn't change.
In the third example, it is possible to delete the elements on positions 1 and 3, or the elements on positions 4 and 5.
#include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int t; cin >> t; while(t--){ int n; cin >> n; vector<int> a(n); long long sum = 0; for(int i = 0;i < n;i++){ cin >> a[i]; sum += a[i]; } long long ans = 0; map<long long ,long long> mp; for(int i = 0;i < n;i++){ ans += mp[1ll * n * a[i]]; mp[2ll * sum - 1ll * n * a[i]]++; //例三:1 4 7 3 5 sum=20 ->5 20 35 15 25 平均数20 //ans+=mp[5],mp[35]=1 //ans+=mp[20],mp[20]=1 //ans+=mp[35],mp[5]=1 //ans+=mp[15],mp[25]=1 //ans+=mp[25],mp[15]=1 //原平均数sum/n,要求删除的两个数需要是2sum/n,避免小数,所有数*n //平均数变成sum,那么删除的两个数的和是2sum //用map记录 } cout << ans << endl; } return 0; }
Educational Codeforces Round 115 (Rated for Div. 2) C. Delete Two Elements
于 2021-11-21 19:21:31 首次发布