Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 39796

runtime error(SIGSEGV) for lowest sum

$
0
0
#include<stdio.h>
#include<stdlib.h>

//#define MAX 10001
void quicksort(long long[],int,int);
int partition(long long[],int,int);
void swap(long long[],int,int);

int main()
{
 int t,k,q,i,j,qi,c,p;
 long long *mot,*sat,*res;
 //printf("Enter no of test cases");
 scanf("%d",&t);
 for(i=0;i<t;i++)
 {
  p=0;
  //printf("Enter no of chefs and no of queries");
  scanf("\n%d %d",&k,&q);
  mot=(long long*)malloc(k*sizeof(long long));
  sat=(long long*)malloc(k*sizeof(long long));
  res=(long long*)malloc(10001*sizeof(long long));
  //printf("Enter motivation levels");
  for(j=0;j<k;j++)
  scanf("%lld",&mot[j]);
  //printf("Enter satisfaction levels");
  for(j=0;j<k;j++)
  scanf("%lld",&sat[j]);
  //calc sum[]
  for(c=0;c<k;c++)
  for(j=0;j<k;j++)
  res[p++]=mot[c]+sat[j];
free(mot);
free(sat);
  quicksort(res,0,--p);       //partition,swap
  for(j=0;j<q;j++)
  {
   //printf("\n qith lowest sum");
   scanf("%d",&qi);
   if(qi<=(k*k))
   printf("%lld\n",res[qi-1]);
  }
 free(res);
 }
 return 0;
}

void quicksort(long long *sum,int low,int high)
{
 int m;
 if(low<high)
 {
  m=partition(sum,low,high);
  quicksort(sum,low,m-1);
  quicksort(sum,m+1,high);
  }
}

int partition(long long *sum,int low,int high)
{
 long long pivot=sum[low];
 int i=low,j=high;
 while(i<j)
 {
  while(sum[i]<=pivot&&i<high)
  i++;
  while(sum[j]>pivot)
  j--;
  if(i<j)
  swap(sum,i,j);
  }
  swap(sum,low,j);
  return j;
}

void swap(long long *sum,int i,int j)
{
 long long temp;
 temp=sum[i];
 sum[i]=sum[j];
 sum[j]=temp;
}

modified but still getting an error


Viewing all articles
Browse latest Browse all 39796

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>