Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody, I got this question from an online competition... i tried to solve it
But i am getting "TIMED OUT ERROR"...

The problem is...I have N packets of candy ...out of which i have to distribute k packets of candy to each person such that unfairness is minimised...

Suppose the K packets have (x1, x2, x3,….xk) candies in them, where xi denotes the number of candies in the ith packet, then we define unfairness as

sigma(Xi-Xj) = abs(a) where 1<j><k>

where |a| denotes the absolute value of a.

constraints 2 < N < 10 pow 5
2<= K <= N
0 <= no.of packets in each candy <= 10 pow 9

Sample Input :

N = 7
K = 3
packets = 10 100 300 200 1000 20 30

Sample Output :

40

Explanation :

I will choose packets having 10, 20 and 30 candies.So unfairness will be |10-20| + |20-30| + |30-10| = 40. It will be minimum as compared to any other distribution which can be verified

Thia is my code.... It runs fine..the problem is it takes too much time for large no. of inputs upto (10 pow 5)... I want to know how to optimize it...


this is the code...

C++
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void quicksort(long int [100000],long int,long int);

int main()
{
  long int packs[100000],n,k,i,j;
  long int key1=0,key2 = 999999999;
  long int temp = 999999999 ;
  long int l;

  scanf("%ld",&n);
  scanf("%ld",&k);

  for(i=0;i<n;i++)
    scanf("%ld",&packs[i]);
    // packs[i] = random(1000000);

     quicksort(packs,0,n-1);

  for(i=0;i<(n-k);i++)
   {
    for(j=i;j<(k-1+i);j++)
     {
       for(l=j+1;l<=(k-1+i);l++)
       {

     //     if(key2>key1)
       key1 += abs(packs[j]-packs[l]);
       //else
        //continue;
       }
     }
   if( key1 < temp)
    key2 = temp = key1;
      key1 = 0;
      }


printf("%ld",temp);

     return 0;
}



I tried reducing the loops...so i did this...the sum of the previous sequence is used to calculate the next sequence....


C++
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void quicksort(long int [100000],long int,long int);

long int main()
{
  long int packs[100000],n,k,i,j;
  long int key1 = 0,key2 = 0;
  long int temp = 0 ;
  long int l,m;

  scanf("%ld",&n);
  scanf("%ld",&k);

  for(i=0;i<n;i++)
   // scanf("%lld",&packs[i]);
    packs[i] = random(1000000);

     quicksort(packs,0,n-1);


  for(l=0;l<k;l++)
   for(m=l;m<k;m++)
  {
   key1 = temp += abs(packs[l] - packs[m]);
  }
//printf("\n\n%ld\n\n",temp);

  for(i=1;i<(n-k);i++)
   {
    for(j=i;j<(k+i-1);j++)
     {
     key1 += abs(packs[j] - packs[k+i-1]);
     key2 += abs(packs[j] - packs[i-1]);
     }
     key1 = key1-key2 ;
   if( key1 < temp)
    temp = key1;
      key2 = 0;
	 // printf("\n\n%ld",key1);
	  }


printf("%ld",temp);

     return 0;
}


But in this code i am getting the error as "WRONG ANSWER" when i submit it ,in the website's compiler...
Posted
Comments
Sergey Alexandrovich Kryukov 24-Oct-13 21:27pm    
Please tell me, is there a change to continue participation in this competition and to submit the answer to this problem again?
If so, asking about it is very incorrect...
—SA
007kurosaki 24-Oct-13 21:31pm    
No, I already submitted the answer and scored very less because of the "TIMED OUT" errors.....
so, i want to know if there is a way to optimize my code ...so that it wont be a hassle for me in the future.....
Sergey Alexandrovich Kryukov 24-Oct-13 21:53pm    
:-)
nv3 25-Oct-13 7:25am    
Before you ask the favor of someone else to look at your code you should take the time and indent the code properly. The way you have presented it, it's a mess. No wonder you don't see what's wrong in your optimized version.

A couple of comments in your code would help also. As far as I can see, you are assuming that the result set is a contiguous window of size k out of the ordered set of packages. This might be true, but can you prove it?

And as far as I can see, you got some of the index boundaries in the for-loops wrong in your optimized version. After ordering the code, run through it with a debugger for a simple case and you will see what's wrong.
007kurosaki 25-Oct-13 9:36am    
Thnx,will do that...

1 solution

Your solution is not efficient. First choose a less expensive sorting algorithm, there are better options to QuickSort, for instance MergeSort which runs in O(logn*n) and it has a better performance for large inputs. Try using dynamic programming, when you want to speed up a code you have to pay a price and that price is memory, use dynamic programming to store previously calculated solutions and get a better performance, create something like a table of calculated solutions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900