import java.io.*;
import java.util.*;
public class JobSeq
{
public static InputStreamReader input=new InputStreamReader(System.in);
public static BufferedReader br =new BufferedReader(input);
static int job[][], n, maxd = 0;
public static void main(String[] args) throws IOException{
System.out.print("Enter total number of jobs: ");
n = Integer.parseInt( br.readLine() );
job = new int[n][3];
System.out.println("Enter job details: ");
for(int i =0; i
System.out.print("job #"+(i+1)+" :-\n\tProfit: ");
job[i][0]=Integer.parseInt( br.readLine() );
do
{
System.out.print("\tDead Line: ");
job[i][1]=Integer.parseInt( br.readLine() );
if(job[i][1] <= 0)
{
System.out.print("\tInvalid Deadline!\nEnter Again\n");
continue;
}
else if(maxd < job[i][1])
{
maxd = job[i][1];
}
break;
}while(true);
job[i][2]=i+1;
}
bubble_srt();
JobSeq();
int profit = 0;
System.out.print("\nThe optimal solution is J = {" );
for(int i=1; i<=k; i++)
{
System.out.print((job[J[i]-1][2])+", ");
profit += job[J[i]-1][0];
}
System.out.println("\b\b } with a profit of "+ profit + ".");
}
public static void bubble_srt(){
int i, j, t[][];
t = new int[1][3];
for(i = 0; i < n; i++)
for(j = 1; j < (n-i); j++)
if(job[j-1][0] < job[j][0])
{
t[0] = job[j-1];
job[j-1]=job[j];
job[j]=t[0];
}
}
static int J[], k;
static void JobSeq()
{
J = new int[maxd+1];
int temjob[] = new int[n+1];
int r;
for(int i=1; i<=n; i++)
temjob[i] = job[i-1][1];
for(int i=0; i<=maxd; i++)
J[i]=0;
temjob[0] = J[0] = 0;
J[1] = 1;
k=1;
for(int i=2; i<=n; i++)
{
r=k;
while((temjob[J[r]] > temjob[i]) && (temjob[J[r]] != r))
r--;
if((temjob[J[r]] <= temjob[i]) && (temjob[i] > r) )
{
for(int q = k; q>= r+1; q--)
J[q+1] = J[q];
J[r+1] = i; k++;
}
}
return;
}
}
/*Output:
Enter total number of jobs: 4
Enter job details:
job #1 :-
Profit: 100
Dead Line: 2
job #2 :-
Profit: 10
Dead Line: 1
job #3 :-
Profit: 15
Dead Line: 2
job #4 :-
Profit: 27
Dead Line: 1
The optimal solution is J = {4, 1} with a profit of 127.
Process Exit...*/
0 comments:
Post a Comment