Advertisement
Hasan1026

Counting Triangles

Feb 18th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. /*
  2. * @Author: Kabid
  3. * @Date:   {{create_time}}
  4. * @Last Modified by:   Kabid
  5. * @Last Modified time: 2021-02-18 18:02:31
  6. */
  7. #include "bits/stdc++.h"
  8. using namespace std;
  9. #define ll long long
  10. #define dbug cerr<<"HERE"<<endl;
  11. //===============Declaration=======================================
  12. int t;
  13. ll i, j;     //iterators
  14. ll n, k , p, m, c, x; //variables;
  15. ll mx = LLONG_MIN, mn = LLONG_MAX;      // max min
  16. int a [20001];
  17.  
  18. int bs(int l, int r, int xx) {
  19.     //cerr << "HERE" << endl;
  20.     int mid = (l + r) / 2;
  21.     while(l<=r){
  22.         //cerr<<mid<<endl;
  23.         if(a[mid]<xx)
  24.             return bs (l+1,r,xx);
  25.         else if(a[mid]>xx)
  26.             return bs (l, mid-1, xx);
  27.         else return mid;
  28.     }
  29.     //cerr<<"--------+"<<r-1<<endl;
  30.     return l;
  31. }
  32. void solve();
  33. int main() {
  34.  
  35.  
  36.     cin >> t; x = t;
  37.     while (t-- ) {
  38.         solve();
  39.     }
  40.     return 0;
  41.  
  42. }
  43.  
  44.  
  45. void solve() {
  46.     cin >> n; int ct = 0;
  47.     for (i = 0; i < n; i++) {
  48.         cin >> a[i];
  49.     }
  50.     sort(a, a + n);
  51.     for (i = 0; i < n - 2; i++) {
  52.         for (j = i + 1; j < n - 1; j++) {
  53.  
  54.             ct += bs(j + 1, n - 1, a[i] + a[j]) - j -1;         //cout<<a[i]<<' '<<a[j]<<' '<<k<<' '<<j<<endl;
  55.  
  56.         }
  57.     }
  58.     cout << "Case " << x - t << ": " << ct << endl;
  59. }
  60.  
  61. /**
  62. 3
  63. 5
  64. 3 12 5 4 9
  65. 6
  66. 1 2 3 4 5 6
  67. 4
  68. 100 211 212 121
  69.  
  70. **/
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement