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

×

ENTEXAM - Editorial

PROBLEM LINK:

Contest
Practice

Author: Sergey Kulik
Testers: Vasya Antoniuk and Kevin Atienza
Translators: Vasya Antoniuk (Russian), Team VNOI (Vietnamese) and Hu Zecong (Mandarin)
Editorialist: Kevin Atienza

DIFFICULTY:

Simple

PREREQUISITES:

Loops

PROBLEM:

$N$ students will take $E$ exams. All the students with score strictly greater than at least $(N-K)$ students' total score pass. Each exam's score is an integer between $0$ and $M$, inclusive.

They have already taken $E-1$ exams. Sergey is the $N$th person. Given that he knows the results of the other $N-1$ students in the remaining exam, what is the minimum score he must have to pass? (or determine if it is impossible to pass.)

QUICK EXPLANATION:

  • Find all total scores of the first $N-1$ students, and sort them in increasing order. Let the $(N-K)$'th smallest total be $x$.
  • Let $y$ be the total score Sergey in the first $E-1$ exams.
  • Then Sergey needs at least $\max(0, x - y + 1)$ points. If this exceeds $M$, then the answer is Impossible.

EXPLANATION:

Sergey passes if and only if his total score is strictly greater than the $(N-K)$'th smallest total score. So the answer consists of two parts:

  • Computing the $(N-K)$'th smallest total score, and
  • Finding the smallest number of points for Sergey to exceed this total score.

The first part can easily be done by first computing the $N-1$ total scores of the other students using a nested loop, and then sorting. The $(N-K)$'th element of the sorted array is what we want! This runs in $O(N \log N + NE)$ time, but we also note that this can be reduced to just $O(NE)$ by using a linear-time selection algorithm.

For the second part, suppose the $(N-K)$'th smallest total is $x$, and Sergey's total score in the first $E-1$ exams is $y$. ($y$ can be computed with a simple loop). Then we must find the minimum score which, when added to $y$, becomes strictly greater than $x$. If $y > x$ already, then this is $0$. Otherwise (i.e., if $y\le x$), then we need to find an integer $T$ such that $y + T > x$. Clearly this is minimized if the sum is as small as possible, but the smallest integer $> x$ is $x + 1$, so $y + T$ must be equal to $x + 1$. Thus, $T = x - y + 1$. Finally, note that an exam score is at most $M$, so we must check if $T \le M$, because if $T > M$, then we conclude that Sergey will not pass.

Here's an implementation of this algorithm in C++:

#include <iostream>
#include <algorithm>
using namespace std;

long long totals[10011];

// select the I'th smallest element from totals[0]...totals[N-1]
long long select(int N, int I) {
    sort(totals, totals+N);
    return totals[I-1];
}

int main() {
    int T;
    cin >> T;
    for (int cas = 1; cas <= T; cas++) {
        int N, K, E, M;
        cin >> N >> K >> E >> M;
        for (int i = 0; i < N-1; i++) {
            long long total = 0;
            for (int j = 0; j < E; j++) {
                long long score;
                cin >> score;
                total += score;
            }
            totals[i] = total;
        }
        long long x = select(N-1, N-K);
        long long y = 0;
        for (int j = 0; j < E-1; j++) {
            long long score;
            cin >> score;
            y += score;
        }
        long long answer = max(0LL, x - y + 1);
        if (answer > M) {
            cout << "Impossible" << endl;
        } else {
            cout << answer << endl;
        }
    }
}

Note that we don't store the individual exam scores. Rather we compute the total of each student on the fly, and just store the totals.

Time Complexity:

$O(N \log N + NE)$

AUTHOR'S AND TESTER'S SOLUTIONS:

setter
tester
editorialist

This question is marked "community wiki".

asked 12 Jun '16, 08:06

kevinsogo's gravatar image

7★kevinsogo
1.7k483141
accept rate: 11%

edited 15 Jun '16, 14:19

admin's gravatar image

0★admin ♦♦
19.0k348495533


12345next »

//Can someone please tell me what's wrong with this code

include <iostream>

using namespace std;

int main() { int T; scanf("%d",&T);

while(T--){
    int N, K, E, surgeysMarks =0;
    long int M;
    scanf("%d%d%d%ld",&N,&K,&E,&M);

    int totalMarksArray[N-1]={0};//Total marks array of N-1 students

    for(int i = 0; i<N-1; i++){//Storing marks of each of N-1 students in the array
        for(int j=0; j<E; j++){
            int marks;//Temporary variable marks of each of E subjects
            scanf("%d",&marks);
            totalMarksArray[i]+=marks;//Calculating total marks obtained by the ith student
        }
    }

    //Sorting the totalMarksArray

    while(1){
        int swapped = 0; //Variable used as a reference to check if the array is sorted
        for(int i=0; i<(N-1)-1; i++){
            if(totalMarksArray[i]>totalMarksArray[i+1]){
                int temp = 0;//Temporary variable for swapping
                temp = totalMarksArray[i];
                totalMarksArray[i]=totalMarksArray[i+1];
                totalMarksArray[i+1]=temp;

                swapped = 1;
            }
        }

            if(swapped==0)
                break;
    }

    //Inputting Surgey's Marks in E-1 subjects

    for(int i=0; i<E-1; i++){
        int surgeysTempMarks;//Temporary variable
        scanf("%d",&surgeysTempMarks);

        surgeysMarks+=surgeysTempMarks;
    }

    //Checking if Surgey can pass

    if((totalMarksArray[K-1]-surgeysMarks)>M)
        printf("Impossible\n");
    else if(((totalMarksArray[K-1])-surgeysMarks)<0)//Case when surgey need not score anything to pass
        printf("%d",0);
    else if((totalMarksArray[K-1]-surgeysMarks)<M)
        printf("%d",totalMarksArray[K-1]-surgeysMarks+1);

}

return 0;

}

link

answered 23 Jun '16, 23:00

greeshmanreddy's gravatar image

0★greeshmanreddy
11
accept rate: 0%

All those who are getting wrong answers, its highly suggested to do a dry run of some test case which is giving a wrong answer, checking the corner cases. Because this is a simple ad-hoc problem and most of your simple logic implemented is correct. Though, you can also check someone else's code if you are not able to figure out the mistake in your code.

link

answered 18 Aug '16, 04:34

shubhiks's gravatar image

5★shubhiks
732311
accept rate: 0%

Can someone please help what is wrong in this code?

link

answered 03 Apr '17, 01:30

monsij's gravatar image

4★monsij
535
accept rate: 0%

edited 03 Apr '17, 01:33

vijju123's gravatar image

5★vijju123 ♦
13.9k11240

Fixed the link. :)

(03 Apr '17, 01:33) vijju123 ♦5★

Got it finally

(31 Dec '17, 15:46) monsij4★

whats wrong in my code it works all fine here is my code

#include <iostream>
using namespace std;

int maxminmarks(int a[],bool b,int siz){
    int returning=a[0];
    if(b==true){
        for(int z=0;z<siz;z++){
            if(returning<a[z]){
                returning=a[z];
            }
        }
    }
    if(b==false){
        for(int z=0;z<siz;z++){
            if(returning>a[z]){
                returning=a[z];
            }
        }
    }
    return returning;
}

int minmarksindex(int a[],int siz){
    int y=a[0],x=0;
    for(int z=0;z<siz;z++){
            if(y>a[z]){
                y=a[z];
                x=z;
            }
        }
        return x;
}

int main(){
    int T;
    cin >>T;
    if(T>=1 && T<=5){
    for(int a=0;a<T;a++){
       int N=0,K=0,E=0,M=0;
       cin >> N;
       cin >> K;
       cin >> E;
       cin >> M;
       int marks[N-1];
        if(N>=1 && N<=10000 && K>=1 && K<=10000 && M>=1 && M <=1000000000 && E>=1 && E <=4){
       for(int b=0;b<(N-1);b++){
            int totalmark=0;
            for(int c=0;c<E;c++){
                int y=0;
                cin >>y;
                totalmark+=y;
            }
            marks[b]=totalmark;
       }

       int topper=0;
       topper=maxminmarks(marks,true,(N-1));

       int failures=N-K;
       for(int z=1;z<failures;z++){
            int small=minmarksindex(marks,(N-1));
            marks[small]=topper;
       }
       int firstfailure=maxminmarks(marks,false,(N-1));
        int sergeymark=0;
        for(int c=0;c<(E-1);c++){
                int y;
                cin >>y;
                sergeymark+=y;
        }

        int requiredmarks=1+firstfailure-sergeymark;
        if(requiredmarks>M){
            cout << "Impossible" << endl;
        }
        else{
            if(requiredmarks >=0){
            cout << requiredmarks<< endl;
        }
        else {
            cout << 0 << endl;
        }
        }

            }
        }
    }
}

Do tell me examples where it doesn't work, But codechef ain't accepting this one

link

answered 17 Jun '16, 23:22

chinnanah's gravatar image

1★chinnanah
1
accept rate: 0%

edited 18 Jun '16, 15:58

if have now corrected that mistake, still it shows not working

(18 Jun '16, 15:57) chinnanah1★

Hey chinnanah ! try these test case 1 4 2 3 10 2 7 7 4 1 10 0 10 1 10 10 and your output will be negative and it is clearly stated in the question 0<=score<=M.

link

answered 18 Jun '16, 07:41

demo01's gravatar image

2★demo01
702
accept rate: 12%

Please anyone find bug in my program

define mstud 10000

main( ) { long long test,students,select,exams,marks,x[mstud],i,j,temp; scanf("%lld",&test); while (test--) { scanf("%lld %lld %lld %lld",&students,&select,&exams,&marks); students--; for (i=0;i<students;i++) {="" x[i]="0;" for="" (j="0;j&lt;exams;j++)" {="" scanf("%lld",&temp);="" x[i]+="temp;" }="" }="" printf("fine");="" sorting------heapheap="" {="" long="" long="" elt,ivalue;="" long="" long="" s,f;="" for="" (i="1;i&lt;students;i++)" {="" elt="x[i],s=i,f=(s-1)/2;" while="" (s="">0 && x[f]<elt) x[s]="x[f],s=f,f=(s-1)/2;" x[s]="elt;" }="" for="" (i="students-1;i">0;i--){ ivalue=x[i],x[i]=x[0],f=0; if (i==1) s=-1; else s=1; if (i>2 && x[2]>x[1]) s=2; while (s>=0 && ivalue<x[s]) {="" x[f]="x[s],f=s,s=2*f+1;" if="" (s+1<="i-1" &&="" x[s]<x[s+1])="" s++;="" if="" (s="">i-1) s=-1; } x[f]=ivalue; } } x[students]=0; for (j=1;j<exams;j++) { scanf("%lld",&temp); x[students]+=temp; } // printf("x[%llu]=%llu\nx[%llu]=%llu\n",students-select,x[students-select],students,x[students]); if (x[students-select]-x[students]<marks) printf("%lld\n",-x[students]+x[students-select]+1); else printf("Impossible\n"); } }

link

answered 18 Jun '16, 10:33

anushi's gravatar image

5★anushi
24617
accept rate: 15%

hi! please help me with my code, it works fine on my computer but gives wrong answer when submitted...

       #include<iostream>
       #include<algorithm>
       using namespace std;
       int main()
       {
        int t;
        cin>>t;
        while(t--)
        {
         long int n,k,e,m,i,j;
         cin>>n>>k>>e>>m;
         long int a[n-1][e],diff=0;
         long int sum[1001]={0};
         long int serscr[4]={0},sersum=0;
         for(i=0;i<n-1;i++)
         {
            for(j=0;j<e;j++)
            {
               cin>>a[i][j];
               sum[i]+=a[i][j];

            }   
         }

        sort(sum,sum+(n-1));
         for(i=0;i<e-1;i++)
         {
            cin>>serscr[i];
            sersum+=serscr[i];

         }

         diff=sum[n-k-1]-sersum;

         if(sersum<0)
             {
            cout<<0<<endl;
             }
         else
           {
           if(diff+1<=m)
           {
             cout<<diff+1<<endl;
            }
            else
            {


             cout<<"Impossible"<<endl;

            }


         }


     }


return 0;

}

link

answered 18 Jun '16, 16:36

kashif93's gravatar image

1★kashif93
1
accept rate: 0%

enter code here
#include <iostream>

include<algorithm>

using namespace std;

int main() { int t,n,e,k,q;long long int m,mymarks=0;long long int arr[100]={0}; cin>>t; if(t>=1 && t<=5) { for(int i=0;i<t;i++) {="" cin="">>n>>k>>e>>m; if(k>=1 && k<=10000 && n>=1 && n<=10000 && m>=1 && m<=1000000000 && e<=4 && e>=1) { for(int j=0;j<n-1;j++) {="" for(int="" x="0;x&lt;e;x++)" {="" cin="">>q; arr[j]=arr[j]+q; } } sort(arr,arr+n-1); for(int x=0;x<e-1;x++) {="" cin="">>q; mymarks=mymarks+q; } int a=arr[n-k]; int req=arr[n-k-1]-mymarks+1; if(req<0)req=0; if(req>m)cout<<"Impossible"; else cout<<req;

    }
    else if(n==k)cout<<"0";
}
}
return 0;

}

link

answered 19 Jun '16, 16:37

r_rohit23's gravatar image

0★r_rohit23
1
accept rate: 0%

Range of M is 1 to 10^9 .. how can the score be max(0,x−y+1) // sergey can score a minimum of 1 in the last exam right? .. he cant score 0

link

answered 20 Jun '16, 13:49

tarun_174's gravatar image

2★tarun_174
1
accept rate: 0%

@tarun_174 He can. The max marks, M is 1 to 10^9. But he can get 0.

Please upvote,I need karma to upvote or ask questions

(28 Jun '16, 19:11) cyberneo3★

hey @tarun_174 ! your assumption is totally unpragmatic.Actually ,he can definitely score 0 marks in the last examination as it is already mentioned in the problem statement Score [0,M].

link

answered 20 Jun '16, 16:50

demo01's gravatar image

2★demo01
702
accept rate: 12%

edited 21 Jun '16, 21:58

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:

×14,480
×1,005
×70
×45
×9

question asked: 12 Jun '16, 08:06

question was seen: 8,509 times

last updated: 10 Jul, 23:57