ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1100. Final Standings

What is the wrong in this code??
Posted by SHANTO ISLAM 20 Apr 2023 09:32
//URAL-1100-FINAL STANDINGS

//---------------------------------
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'


bool comp(pair<int,int>a,pair<int,int>b)
{
    return a.second>b.second;
}
void sortt(map<int,int>mp)
{
    vector<pair<int,int>>v;
    for(auto it:mp)
    {
        v.push_back(it);
    }

    stable_sort(v.begin(),v.end(),comp);
    for(auto it:v)
    {
        cout<<it.first<<" "<<it.second<<endl;

    }
}
int main()
{
    map<int,int>mp;
    int n;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        int x,y;
        cin>>x>>y;
        mp[x]=y;
    }
    sortt(mp);


    return 0;
}
Re: What is the wrong in this code??
Posted by Hristo Nikolaev (B&W) 20 Apr 2023 16:07
You are expecting the map to preserve the order. It does not - it sorts by key.
Try using unordered_map<int, int> mp