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

Why my code doesn't work?Sequence is wrong,but only'16 3'and'20 3'. Even the '22 4' and '26 4' are right.
Posted by jingyi Ma 13 Jan 2020 13:03
#include <iostream>
using namespace std;
int main()
{
    int n,a[100],b[100],c,d,i,j;
    cin>>n;
    for(i=0;i<n;i++)
        cin>>a[i]>>b[i];
    for(i=0;i<n;i++)
        for(j=i+1;j<n;j++)
            if(b[i]<b[j])
            {
                c=b[i];
                b[i]=b[j];
                b[j]=c;
                d=a[i];
                a[i]=a[j];
                a[j]=d;
            }
    for(i=0;i<n;i++)
        cout<<a[i]<<' '<<b[i]<<'\n';
    return 0;
}


Help me! I'm die.
Sorrry,my English is poor.
I will try my best to understand you.
Re: Why my code doesn't work?Sequence is wrong,but only'16 3'and'20 3'. Even the '22 4' and '26 4' are right.
Posted by ToadMonster 21 Jan 2020 17:21
Sort declared in the task is stable. Your sort isn't stable
Let we have scores:
("team1", 10),
("team2", 10),
("team3", 15).
Your code when i=0, j=2 swaps team1, team3:
("team3", 15).
("team2", 10),
("team1", 10).

Note: when you fix your sort you'll face the fact real bubble sort is too slow.