×

# ENTEXAM - Editorial

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

Simple

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);
}

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);
cout << "Impossible" << endl;
} else {
}
}
}


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:

This question is marked "community wiki".

1.7k483141
accept rate: 11%

19.0k348495533

//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;


}

11
accept rate: 0%

 1 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. answered 18 Aug '16, 04:34 5★shubhiks 732●3●11 accept rate: 0%
 1 Can someone please help what is wrong in this code? answered 03 Apr '17, 01:30 4★monsij 53●5 accept rate: 0% 13.9k●1●12●40 Fixed the link. :) (03 Apr '17, 01:33) Got it finally (31 Dec '17, 15:46) monsij4★
 0 whats wrong in my code it works all fine here is my code #include using namespace std; int maxminmarks(int a[],bool b,int siz){ int returning=a[0]; if(b==true){ for(int z=0;za[z]){ returning=a[z]; } } } return returning; } int minmarksindex(int a[],int siz){ int y=a[0],x=0; for(int z=0;za[z]){ y=a[z]; x=z; } } return x; } int main(){ int T; cin >>T; if(T>=1 && T<=5){ for(int a=0;a> 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>y; totalmark+=y; } marks[b]=totalmark; } int topper=0; topper=maxminmarks(marks,true,(N-1)); int failures=N-K; for(int z=1;z>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 answered 17 Jun '16, 23:22 1 accept rate: 0% if have now corrected that mistake, still it shows not working (18 Jun '16, 15:57)
 0 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. answered 18 Jun '16, 07:41 2★demo01 70●2 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"); } }

5★anushi
24617
accept rate: 15%

 0 hi! please help me with my code, it works fine on my computer but gives wrong answer when submitted...  #include #include 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>a[i][j]; sum[i]+=a[i][j]; } } sort(sum,sum+(n-1)); for(i=0;i>serscr[i]; sersum+=serscr[i]; } diff=sum[n-k-1]-sersum; if(sersum<0) { cout<<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;


}

1
accept rate: 0%

 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 answered 20 Jun '16, 13:49 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★
 0 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]. answered 20 Jun '16, 16:50 2★demo01 70●2 accept rate: 12%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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