File size: 2,520 Bytes
c574d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
import java.util.*;
public class Job{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of Jobs");
int n = sc.nextInt();
String a[] = new String[n];
int b[] = new int[n];
int c[] = new int[n];
for(int i=0;i<n;++i){
System.out.println("Enter the jobs");
a[i] = sc.next();
System.out.println("Enter the profits");
b[i] = sc.nextInt();
System.out.println("Enter the DeadLine");
c[i] = sc.nextInt();
}
System.out.println("Arranged Order--");
System.out.print("Jobs: ");
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
System.out.println();
System.out.print("Profits: ");
for(int i=0;i<n;i++){
System.out.print(b[i]+" ");
}
System.out.println();
System.out.print("Deadline: ");
for(int i=0;i<n;i++){
System.out.print(c[i]+" ");
}
System.out.println();
// Sort the job based on the profit
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if(b[i]<b[j]){
int temp = b[i];
b[i] = b[j];
b[j] = temp;
temp = c[i];
c[i] = c[j];
c[j] = temp;
String temp1 = a[i];
a[i] = a[j];
a[j] = temp1;
}
}
}
System.out.println();
System.out.println("Sorted Order--");
System.out.print("Jobs: ");
for(int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
System.out.println();
System.out.print("Profits: ");
for(int i=0;i<n;i++){
System.out.print(b[i]+" ");
}
System.out.println();
System.out.print("Deadline: ");
for(int i=0;i<n;i++){
System.out.print(c[i]+" ");
}
System.out.println();
// main algorithm
int max = c[0];
for(int i=0;i<n;++i){
if(c[i] >max){
max = c[i];
}
}
String x[] = new String[max];
int xx[] = new int[max];
int profit = 0;
for(int i = 0; i<n; i++){
int pp = c[i];
pp = pp-1;
if(x[pp]==null){
x[pp] = a[i];
profit+=b[i];
}
else{
while(pp!=-1){
if(x[pp]==null){
x[pp] = a[i];
profit+=b[i];
break;
}
pp = pp-1;
}
}
}
for(int i=0; i<max;++i){
System.out.print("-->"+x[i]);
}
System.out.println();
System.out.println("Profit earned "+profit);
}
}
|