Punish the Students

PROBLEM :

A Professor conducts a Computer Science paper for all students. He had strictly ordered all students to sit according to their roll numbers. However when he started checking the papers, he found out that all the papers were randomly ordered. The students had sat randomly during the exam instead of sitting according to their roll numbers. Professor was very angry and he wanted to teach the students a lesson. He decides to sort the papers by Bubble Sort, count the number of swaps required for each and every student and deduct as many marks of a student as were the number of swaps required for that student. However he also has to maintain a class average of atleast M else he may lose his job. The Professor wants to know whether he should punish the students or save his job.

Input:

The first line of input takes an integer T denoting the total number of test cases. Then T test cases follow. Each test case has 3 input lines.

The first line of each test case takes the number of students, N and the minimum average to be maintained,M.

The second line of each test case takes N space separated integers, the ith integer denoting the roll number of the student sitting in i th seat during exam.

The third line of each test case takes N space separated integer, the i th integer denoting the marks obtained by the student having roll number i.

Output:

Print 1 if he can teach the students a lesson without putting his job at risk, else print 0.

Constraints:

1<=T<=100
1<=N<=1000
1<=M<=100
1<=Marks<=100

Example:

Input:
2
5 68
3 2 4 1 5
50 67 89 79 58
6 67.5
5 4 1 3 2 6
78 98 86 67 78 79

Output:
0
1

Explanation:

Test Case#1

Iteration

     1                         2 3 1 4 5                  -                       Array

                                1 1 1 1 0                  -                      Swaps

     2                         2 1 3 4 5                   -                       Array

                                1 2 2 1 0                   -                      Swaps

     3                         1 2 3 4 5                    -                       Array

                                3 2 2 1 0                   -                      Swaps


Marks of 1st student = 50 - 3 = 47

Marks of 2nd student = 67 - 2 = 65

Marks of 3rd student = 89 - 2 = 87

Marks of 4th student = 79 - 1 = 78

Marks of 5th student = 58 - 0 = 58

Class Average = 67<68

Hence, Professor decides to save his job and O/P is 0.

The remaining test cases follow from the same logic.

**For More Examples Use Expected Output**

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<stdio.h>

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int no,marks[100],roll[100],arr[100]={0},i,j,count =0;
        float sum=0,av ;
        scanf("%d%f",&no,&av);
        for(i=0;i<no;i++)
        {
            scanf("%d",&roll[i]);
        }
        for(i=0;i<no;i++)
        {
            scanf("%d",&marks[i]);
        }
        for(i=0;i<no-1;i++)
        {
            for(j=0;j<no-i-1;j++)
            {
                if(roll[j] > roll[j+1])
                {
                    arr[roll[j]]++ ;
                    arr[roll[j+1]]++ ;
                    int temp = roll[j];
                    roll[j] = roll[j+1];
                    roll[j+1] = temp;
                }
            }
        }
       
       for(i=0;i<no;i++)
       {
           marks[i] = marks[i] - arr[i];
           sum +=marks[i];
       }
   
       sum = sum/no;
       if(sum < av)
       {
           printf("0\n");
       }else
       printf("1\n");
    }
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )