-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJobSequence.java
More file actions
94 lines (94 loc) · 2.04 KB
/
Copy pathJobSequence.java
File metadata and controls
94 lines (94 loc) · 2.04 KB
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
import java.util.*;
public class JobSequence {
private class Jobs{
int pr,ti;char id;Jobs next;boolean cch;
Jobs(int pr,int ti,char id){
this.pr=pr;
this.ti=ti;
this.id=id;
cch=false;
next=null;
}
}
Jobs head;Jobs last;
JobSequence(){
head=null;
}
void add(int pr,int ti,char id) {
if(head == null) {
head=new Jobs(pr,ti,id);
last=head;
return;
}
last.next=new Jobs(pr,ti,id);
last=last.next;
}
Jobs merge(Jobs a,Jobs b) {
if(a==null)
return b;
if(b==null)
return a;
Jobs result;
if(a.pr>=b.pr) {
result=a;
result.next=merge(a.next,b);
}
else {
result=b;
result.next=merge(a,b.next);
}
return result;
}
Jobs divide(Jobs x) {
if(x==null || x.next==null)
return x;
Jobs mid=findMid(x);
Jobs rmid=mid.next;
mid.next=null;
Jobs left=divide(x);
Jobs right=divide(rmid);
Jobs done=merge(left,right);
return done;
}
Jobs findMid(Jobs h) {
if(h==null)
return h;
Jobs fast=h,slow=h;
while(fast.next!=null && fast.next.next!=null) {
slow=slow.next;
fast=fast.next.next;
}
return slow;
}
void findSeq(int t) {
boolean seq[]=new boolean[t];
Jobs temp=head;
while(temp!=null) {
if(temp.ti<t&&seq[temp.ti-1]==false) {
seq[temp.ti-1]=true;
System.out.print(temp.id+" ");
}
temp=temp.next;
}
}
/*void printlist() {
Jobs temp=head;
while(temp!=null) {
System.out.println(temp.pr);
temp=temp.next;
}
}*/
public static void main(String args[]) {
JobSequence ob=new JobSequence();
System.out.println("Enter number of jobs in the list");
int n=new Scanner(System.in).nextInt();
for(int i=0;i<n;i++) {
System.out.println("Enter profit, deadline, JobID");
ob.add(new Scanner(System.in).nextInt(),new Scanner(System.in).nextInt(), new Scanner(System.in).next().charAt(0));
}
ob.head=ob.divide(ob.head);
//ob.printlist();
System.out.println("Enter possible number of jobs.");
ob.findSeq(new Scanner(System.in).nextInt());
}
}