Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Thursday, March 13, 2014

MSCHED-Milk Scheduling

Milk Scheduling

below given code is for msched spoj or milk scheduling spoj.
this is simple implementation of Job Sequencing
:happy coding..
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <bitset>
using namespace std;
class job
{
public:
 int deadline;
 int profit;
};
void mergesort(job a[],int low,int mid,int high)
{
 job temp[10009];
 int i=low,k=low,j=mid+1;
 while(i<=mid && j<=high)
 {
  if(a[i].profit >=a[j].profit)
   temp[k++]=a[i++];
  else
   temp[k++]=a[j++];
 }
 while(i<=mid)
  temp[k++]=a[i++];
 while(j<=high)
  temp[k++]=a[j++];
 for(i=low;i<=high;i++)
  a[i]=temp[i];
}
void partition(job a[],int low,int high)
{
 int mid;
 if(low<high)
 {
  mid=(low+high)/2;
  partition(a,low,mid);
  partition(a,mid+1,high);
  mergesort(a,low,mid,high);
 }
}
int main()
{
 int n;
 scanf("%d",&n);
 int i,max=-1,k;
 bool p[10009]={0};
 job j[n+9];
 for(i=0;i<n;i++)
 {
  scanf("%d",&j[i].profit);
  scanf("%d",&j[i].deadline);
 }
 partition(j,0,n-1);
 int total=0,temp;
 for(i=0;i<n;i++)
 {
     temp=j[i].deadline;
     while(temp)
     {
         if(!p[temp]){
   p[temp]=1;
   total+=j[i].profit;
   break;
            }
            else
                temp--;
     }
 }
 printf("%d",total);
 return 0;
}


No comments:

Post a Comment

Your comment is valuable to us