Hi,i wrote this program and it runs fine on my pc with the given example values but when i upload it, it shows wrong answer.can anybody please find the bug,please !!!
#include<stdio.h>
#include<stdlib.h>
void qs(int*,int,int);
int main()
{
int t;
scanf("%d",&t);
(t<=0||t>5)?exit(0):1;//constraint
while(t–)
{
int n,k,e,m;
scanf("%d%d%d%d",&n,&k,&e,&m);
(k<=0||k>1000)?exit(0):1;//constraint
(n<=0||n>1000)?exit(0):1;//constraint
(n<=k)?exit(0):1;//constraint
(e<=0||e>4)?exit(0):1;//constraint
(m<=0||m>1000000000)?exit(0):1;//constraint
int g[n][e];
int i,j;
for(i=0;i<n;i++)
for(j=0;j<e;j++)
{
if((i==(n-1))&&(j==(e-1))) break;
scanf("%d",&g[i][j]);
}
int h[n];
for(i=0;i<n;i++) //summation of each student grades
{
int sum=0;
for(j=0;j<e;j++)
{
if((i==(n-1))&&(j==(e-1))) break;
sum=sum+g[i][j];
}
h[i]=sum;
}
qs(h,0,(n-2));
int check;//keeps difference
if(h[n-1]>h[k-1]) printf(“0\n”);
else
{
check=(h[k-1]-h[n-1])+1;
if(check>m) printf(“Impossible\n”);
else printf("%d\n",check);
#include #include
using namespace std;
int main()
{
int t;
cin>>t;
int t1=t;
long long int n,k,e,m,temp,sum;
while(t–)
{
cin>>n>>k>>e>>m;
long long int a[n];
//Take input
for(long long int i=0;i<n-1;i++)
{
sum = 0;
for(long long int j=0;j<e;j++)
{
cin>>temp;
sum += temp;
}
a[i] = sum;
}
sum = 0;
for(long long int j=0;j<e-1;j++)
{
cin>>temp;
sum += temp;
}
a[n-1] = sum;
//Do processing
sort(a,a+n-1,greater<int>());
long long int value = a[k-1];
if(value<a[n-1])
{
cout<<"0"<<endl;
}
else if(value-a[n-1]+1<=m)
{
cout<<value-a[n-1]+1<<endl;
}
else
{
cout<<"Impossible"<<endl;
}
}
return 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.
Hi,
Can you please help me find the bug in my code sumbmitted in the following link. CodeChef: Practical coding for everyone
It works fine, but not accepted when submittd.
Thanks in advance!
Please tell me why my code is giving a wrong answer`import java.util.*;
class Entexam{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
int N,K,E,M;
for (int p=0;p<T;p++){
N=scanner.nextInt(); //No. of students
K=scanner.nextInt(); //These many students shall pass
E=scanner.nextInt(); //no of tests
M=scanner.nextInt(); // max marks in a test
int[] marksOfOthers = new int[N-1];
for(int j=0;j<N-1;j++){
marksOfOthers[j]=0;
for(int k=0;k<E;k++){
marksOfOthers[j]+=scanner.nextInt();
}
}
//Now marks have been taken of N-1 guys
int mySum=0;
for(int i=0;i<E-1;i++){
mySum+=scanner.nextInt();
}
//I need to sort the marks array - I am using bubble sort for now
Arrays.sort(marksOfOthers);
//Now array sorted , now I just need to get its score more than the kth person's score
int targetScore = marksOfOthers[N-K-1] - mySum + 1;
if(targetScore>M){
//impossible
System.out.println("Impossible");
}else if (targetScore<0){
//possible
System.out.println(0);
}else{
System.out.println(targetScore);
}
}
scanner.close();
}
This ruby code works fine for me elsewhere but gives NZEC while submission, any suggestions ?
t = gets.to_i
t.times do
merit_list = []
input = gets.split.map(&:to_i)
n, k, e, m = input[0], input[1], input[2], input[3]
(n-1).times do
student_marks = gets.split.map(&:to_i)
merit_list.push(student_marks.sum)
end
my_marks = gets.split.map(&:to_i)
my_sum = my_marks.sum
merit_list = merit_list.sort.reverse
marks_to_get = merit_list[k-1] - my_sum + 1
if (marks_to_get <= m)
puts marks_to_get
else
puts "Impossible"
end
end
using namespace std;
int main()
{
int t,k,n,e;
long int o,ma;
vector<long int> m(100011);
cin>>t;
while(t--)
{
long int total;
cin >> n>>k>>e>>ma;
for(int i=0;i<n-1;i++)
{
total=0;
for(int j=0;j<e;j++)
{
cin>>o;
total+=o;
}
m[i]=total;
}
total=0;
sort(m.begin(),m.end());
reverse(m.begin(),m.end());
for(int i=0;i<e-1;i++)
{
cin>>o;
total+=o;
}
long int x=total;
long int y=m[n-k-1];
long int mx=max(0L,y-x+1);
//cout<<mx;
if(mx>ma)
cout<<"Impossible"<<endl;
else
cout<<mx<<endl;
m.clear();
}
return 0;
}
Can someone take a look at my code. I’ve been trying to refine it all day!
If you want to be updated with my changes, kindly take a look over https://gist.github.com/salman-bhai/f51412bd45aa4c61c07071e089f9954e:
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d",&t);
while(t--) {
long long int n, k, e;
long long int m;
scanf("%lld %lld %lld %lld",&n,&k,&e,&m);
long long int arr[n][e];
long long int total[n-1]={0};
for(long long int i = 0; i < n-1; i ++) {
for(long long int j = 0; j < e; j ++) {
scanf("%lld",&arr[i][j]);
total[i] += arr[i][j];
}
}
long long int sergey=0;
for(long long int i = 0; i < e-1; i ++) {
long long int temp;
scanf("%lld",&temp);
sergey += temp;
}
sort(total, total+n-1);
if(sergey > total[n-k-1])
printf("1\n");
else if(sergey <= total[n-k-1] && (total[n-k-1]-sergey+1 <= m))
printf("%lld\n",(total[n-k-1]-sergey+1));
else
printf("Impossible\n");
}
return 0;
}
The first error I noticed in your code is for cases where his total score is already large enough, that he can take get admission despite getting 0 in final exam.
In case where his total score exceeds everyone else’s score by such a large factor, that he need not score any mark in final exam, the output should be 0, as its possible to score 0 in exam. Your program is printing 1 for that case which is incorrect (if my interpretation of Q’s line “In total there are E entrance exams, in each of them one can score between 0 and M points, inclusively” is correct)
**Anybody help me out with my code it gives wrong answers on submission **
`#include<stdio.h>
int main()
{
long long int t,n,k,e,m,numb,i,j,z;
long long int na[10000];
scanf("%d",&t);
while(t–)
{
scanf("%lld%lld%lld%lld",&n,&k,&e,&m);
for(i=0;i<n;i++)
{ na[i]=0;
if(i==(n-1))
{ for(j=0;j<e-1;j++)
{
scanf("%lld",&z);
na[i]=na[i]+z;
}
}
else
{
for(j=0;j<e;j++)
{
scanf("%lld",&z);
na[i]=na[i]+z;
}
}
int main(){
long long t,n,m,e,k;
long long sum[10000]={};
long s;
int pos=0;
cin>>t;
for(int i=0;i<t;i++){
cin >> n >> k>>e>>m;
long int j;
for(j=0;j<n-1;j++){
s=0;
for(int l=0;l<e;l++){
cin>>s;
sum[j]+=s;
}
}
s=0;pos=j;
for(int l=0;l<e-1;l++){
cin>>s;
sum[j]+=s;
}
sort(sum,sum+pos);
long long x = sum[n-k-1]-sum[pos]+1;
if(x<0) cout<<0<<endl;
else if(x<m) cout<<x<<endl;
else cout<<"Impossible"<<endl;
}
return 0;
}