Doubt in logic of this code (codeforces round 246)

Revision en5, by Akash_Roy, 2020-09-23 06:54:51

This is the problem link

An O(n^2) solution gave me TLE on test 10. I came across one of the solutions below.

main() {

int n,i,f,s;
 cin>>n;
 map<int,int> mp;
 pair<int , int> a[n];
 for(i=0;i<n;i++)
 {
    cin>>f>>s;
    mp[f]++;
    a[i]={f,s};
 }

int m=n-1;
for(i=0;i<n;i++)
{
    a[i].first=m+mp[a[i].second];
    a[i].second=m-mp[a[i].second];

    cout<<a[i].first<<" "<<a[i].second<<endl;
}

}

I don't understand how the following two lines work.

a[i].first=m+mp[a[i].second];

a[i].second=m-mp[a[i].second];

Each team plays n-1 home and n-1 away games . But what is purpose of mp[a[i].second]? Please help.

Tags #std::map, #brute force, #greedy, #implementation

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en5 English Akash_Roy 2020-09-23 06:54:51 8
en4 English Akash_Roy 2020-09-23 06:53:48 22 Reverted to en2
en3 English Akash_Roy 2020-09-23 06:53:33 22 Reverted to en1
en2 English Akash_Roy 2020-09-23 06:52:59 22
en1 English Akash_Roy 2020-09-23 06:51:02 827 Initial revision (published)