Educational Codeforces Round 161 (Rated for Div. 2) 1922B. Forming Triangles Solution
Problem Link : https://codeforces.com/contest/1922/problem/B
Solution in C++:
- /// Author : AH_Tonmoy
- #include <bits/stdc++.h>
- using namespace std;
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t;
- cin >> t;
- while (t--) {
- int n ; cin >> n ;
- vector<int>a(n) ;
- map<int,int>mp ;
- for ( int i = 0 ; i < n ; i++){
- cin >> a[i] ;
- mp[a[i]]++ ;
- }
- sort(a.begin(),a.end()) ;
- long long ans = 0 ;
- for (auto [x,y] : mp ){
- ans += ( 1LL * y * ( y - 1) * ( y - 2) ) / 6 ;
- int index = lower_bound(a.begin(),a.end(),x) - a.begin() ;
- ans += ( 1ll * ( y * ( y - 1 )) / 2) * index ;
- }
- cout << ans << '\n' ;
- }
- return 0 ;
- }
No comments