You are not logged in. Please login at www.codechef.com to post your questions!

×

QM6P5B editorial

PROBLEM LINK:

Practice
Contest

Author: Chandan Boruah
Tester: Chandan Boruah
Editorialist: Chandan Boruah

DIFFICULTY:

EASY

PREREQUISITES:

Sorting (bubble).

PROBLEM:

Given few teams with scores and groups. Find what happens to the group winners when two teams swap groups.

QUICK EXPLANATION:

Swap the two teams in the groups with the scores and then sort the teams in the groups.

EXPLANATION:

Swap the two teams in the groups along with their scores. Do a bubble sort on the groups, swap positions if scores are in ascending and only if the groups of the teams are same. Since same name groups appear contigously the groups won't mix and everything would be sorted. Then print the winners of each group.

AUTHOR'S SOLUTION IN C#:

using System;
class some
{
    public static void Main()
    {
        int n=int.Parse(Console.ReadLine());
        for(int t=0;t<n;t++)
        {
            string[]ss=Console.ReadLine().Split();
            int a=int.Parse(ss[0]);
            int b=int.Parse(ss[1]);
            string[]names=new string[a*b];
            string[]teams=new string[a*b];
            int[]scores=new int[a*b];
            for(int i=0;i<names.Length;i++)
            {
                ss=Console.ReadLine().Split();
                names[i]=ss[0];
                teams[i]=ss[1];
                scores[i]=int.Parse(ss[2]);
            }
            ss=Console.ReadLine().Split();
            string aa=ss[0];
            string bb=ss[1];
            int indA=Array.IndexOf(names,aa);
            int indB=Array.IndexOf(names,bb);
            names[indA]=bb;
            names[indB]=aa;
            int temp=scores[indA];
            scores[indA]=scores[indB];
            scores[indB]=temp;
            for(int i=0;i<scores.Length;i++)
            {
                for(int j=0;j<scores.Length-1;j++)
                {
                    if(scores[j]<scores[j+1] && teams[j]==teams[j+1])
                    {
                        int tt=scores[j];
                        scores[j]=scores[j+1];
                        scores[j+1]=tt;
                        string k1=names[j];
                        names[j]=names[j+1];
                        names[j+1]=k1;
                    }
                }
            }
            for(int i=0;i<teams.Length;i++)
            {
                if(i%b==0)
                Console.WriteLine(names[i]);
            }
        }
    }
}

asked 12 Feb, 11:09

chandubaba's gravatar image

3★chandubaba ♢
634
accept rate: 0%

edited 13 Feb, 15:59

admin's gravatar image

0★admin ♦♦
18.2k347492525

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×13,349
×628
×115
×13

question asked: 12 Feb, 11:09

question was seen: 31 times

last updated: 13 Feb, 15:59