SHARE
TWEET

codeforce - candy box (easy version)

jakaria_hossain Jun 27th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define fast()(ios_base::sync_with_stdio(0),cin.tie(NULL));
  4. #define pb push_back
  5. int main()
  6. {
  7.     fast();
  8.     int q;
  9.     cin>>q;
  10.     while(q--)
  11.     {
  12.         map<int,int>mp,taken;
  13.         vector<int>val,freq;
  14.         int n,x,sum=0;
  15.         cin>>n;
  16.         for(int i=0;i<n;i++)
  17.         {
  18.             cin>>x;
  19.             if(mp[x]==0)val.pb(x);
  20.             mp[x]++;
  21.         }
  22.         for(int i=0;i<val.size();i++)
  23.         {
  24.             freq.pb(mp[val[i]]);
  25.  
  26.         }
  27.         sort(freq.begin(),freq.end());
  28.         x=freq[freq.size()-1];
  29.         for(int i=freq.size()-1;i>=0;i--)
  30.         {
  31.             if(x==0)break;
  32.             else if(taken[freq[i]]==0)
  33.             {
  34.                 sum+=freq[i];
  35.                 taken[freq[i]]++;
  36.                 x=freq[i];
  37.             }
  38.             else
  39.             {
  40.                 x--;
  41.                 taken[x]++;
  42.                 sum+=x;
  43.             }
  44.         }
  45.         cout<<sum<<endl;
  46.     }
  47. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top