Advertisement
shamiul93

prioroty queue

Jun 14th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. // LightOJ always needs this format for sure..so I made a copy of it...
  2. #include <bits/stdc++.h>
  3. #include<vector>
  4. #define ll                                      long long int
  5. #define fi                                      freopen("in.txt", "r", stdin)
  6. #define fo                                      freopen("out.txt", "w", stdout)
  7. #define m0(a) memset(a , 0 , sizeof(a))
  8. #define m1(a) memset(a , -1 , sizeof(a))
  9. #define inf LLONG_MAX
  10. #define min3(a,b,c) min(a,min(b,c))
  11. #define max3(a,b,c) max(a,max(b,c))
  12. #define ones(mask)  __builtin_popcount(mask) /// __builtin_popcount : it's a built in function of GCC. Finds out the numbers of 1 in binary representation.
  13. #define mx 150000
  14.  
  15. using namespace std;
  16.  
  17.  
  18. struct compare
  19. {
  20.     bool operator () (const ll &a , const ll &b)
  21.     {
  22.         return a > b ;
  23.     }
  24. };
  25.  
  26. priority_queue<ll,vector<ll> ,compare> pq ;
  27.  
  28. int main()
  29. {
  30.     ll T ;
  31.     cin >> T ;
  32.     while(T--)
  33.     {
  34.         ll n ;
  35.         cin >> n ;
  36.         ll ans = 0 ;
  37.  
  38.         for(ll i = 1 ; i <= n ; i++)
  39.         {
  40.             pq.push(i) ;
  41.         }
  42.  
  43.         while(!pq.empty() && pq.size()>1)
  44.         {
  45.             ll sum = 0 ;
  46.             sum += pq.top() ;
  47.             ans += pq.top() ;
  48.             pq.pop() ;
  49.             sum += pq.top() ;
  50.             ans += pq.top() ;
  51.             pq.pop() ;
  52.  
  53.             pq.push(sum) ;
  54.         }
  55.  
  56.         cout << ans << endl ;
  57.         while(!pq.empty())
  58.             pq.pop() ;
  59.     }
  60.    return 0 ;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement