1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 #include <math.h> 5 6 using namespace std; 7 typedef long long LL; 8 LL a[2000+5]; 9 int n; 10 11 int Binary_search(int x) 12 { 13 int l = 0,r = n-1; 14 while(l<=r) 15 { 16 int mid = (l+r)/2; 17 if(a[mid] == x) 18 return mid; 19 else if(a[mid] > x) 20 r= mid-1; 21 else 22 l = mid +1; 23 } 24 return l; 25 26 } 27 28 int main() 29 { 30 int T; 31 cin>>T; 32 int flag = 0; 33 while(T--) 34 { 35 36 cin>>n; 37 for(int i = 0; i < n; i++) 38 scanf("%lld",&a[i]); 39 sort(a,a+n); 40 int ans = 0; 41 for(int i = 0; i < n-1; i++) 42 for(int j = i+1; j < n; j++) 43 { 44 LL sum = a[i] + a[j]; 45 LL cha = abs(a[j] - a[i]); 46 47 int up = Binary_search(sum)-1; 48 if(up <= j) 49 continue; 50 int low = Binary_search(cha); 51 if(low < j) 52 low = j; 53 ans += (up-low); 54 55 56 } 57 58 cout<<"Case "<<++flag<<": "<<ans<<endl; 59 60 } 61 return 0; 62 }
1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 #include <math.h> 5 6 using namespace std; 7 typedef long long LL; 8 LL a[2000+5]; 9 int n; 10 11 int Binary_search(int x) 12 { 13 int l = 1,r = n; 14 while(l<r) 15 { 16 int mid = (l+r)/2; 17 if(a[mid] == x) 18 return mid; 19 else if(a[mid] > x) 20 r= mid; 21 else 22 l = mid +1; 23 } 24 return l; 25 26 } 27 28 int main() 29 { 30 int T; 31 cin>>T; 32 int flag = 0; 33 while(T--) 34 { 35 36 cin>>n; 37 for(int i = 0; i < n; i++) 38 scanf("%lld",&a[i]); 39 sort(a,a+n); 40 int ans = 0; 41 for(int i = 0; i < n-1; i++) 42 for(int j = i+1; j < n; j++) 43 { 44 LL sum = a[i] + a[j]; 45 LL cha = abs(a[j] - a[i]); 46 47 int up = Binary_search(sum)-1; 48 if(up <= j) 49 continue; 50 int low = Binary_search(cha); 51 if(low < j) 52 low = j; 53 ans += (up-low); 54 55 56 } 57 58 cout<<"Case "<<++flag<<": "<<ans<<endl; 59 60 } 61 return 0; 62 }
1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 #include <math.h> 5 6 using namespace std; 7 typedef long long LL; 8 LL a[2000+5]; 9 int n; 10 11 int main() 12 { 13 int T; 14 cin>>T; 15 int flag = 0; 16 while(T--) 17 { 18 19 cin>>n; 20 for(int i = 0; i < n; i++) 21 scanf("%lld",&a[i]); 22 sort(a,a+n); 23 int ans = 0; 24 for(int i = 0; i < n-1; i++) 25 for(int j = i+1; j < n; j++) 26 { 27 LL sum = a[i] + a[j]; 28 LL cha = abs(a[j] - a[i]); 29 30 int up = upper_bound(a+j+1,a+n,sum)-a; 31 32 33 int low = lower_bound(a+j+1,a+n,cha)-a; 34 35 ans += (up-low); 36 37 38 } 39 40 cout<<"Case "<<++flag<<": "<<ans<<endl; 41 42 } 43 return 0; 44 }