Repository navigation
Expand file tree
/
Copy pathaverage_pairs.cpp
More file actions
75 lines (75 loc) · 1.93 KB
/
Copy pathaverage_pairs.cpp
File metadata and controls
75 lines (75 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define Fl(i,a,b) for(ll i=a;i<b;i++)
#define Fll(i,a,b) for(ll i=a;i<=b;i++)
#define Fr(i,a,b) for(ll i=a;i>=b;i--)
//-------------------------------------------------------
#define ipair pair<ll,ll>
#define vll vector<ll>
#define vstr vector<string>
#define vchar vector<char>
#define sll set<ll>
#define mll map<ll,ll>
//---------------------------------------------------------
#define MIN -1
#define MAX 1e10
#define VER 101
#define fastread ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
vll adj[VER];
//----------------------------------------------------------------------------
void make_edge(ll a,ll b)
{
adj[a].pb(b);
adj[b].pb(a);
}
ll sum_of(ll n)
{
if(n==0)
return 0;
return (n*(n+1))/2;
}
//-----------------------------------------------------------------------------
int main()
{
ll t,n,val,pos,ans;
cin>>t;
while(t--){
ans=0;
cin>>n;
sll s;
vll v(n);
vll check(1e5+1,0);
Fl(i,0,n){
cin>>v[i];
s.insert(2*v[i]);
check[v[i]]++;
}
sort(all(v));
for(sll :: iterator it=s.begin();it!=s.end();it++){
val=(*it);
pos=lower_bound(all(v),val)-v.begin();
if(pos==n)
pos=lower_bound(all(v),v[pos-1])-v.begin();
//cout<<val<<" "<<pos<<"\n";
while(pos>=0){
if(v[pos]<(val/2))
break;
if(v[pos]==(val/2))
ans+=(sum_of(check[v[pos]]-1));
else{
if(check[v[pos]] && check[val-v[pos]])
ans+=(check[v[pos]]*check[val-v[pos]]);
}
pos--;
}
//cout<<ans<<" ";
}
cout<<ans<<"\n";
}
return 0;
}