Uploaded by Jai Mahajan

prob-Q1

advertisement
Many processes
You are on a system with the following scheduling policy - “At any point of time,
schedule the process with maximum priority”.
You are given a list of N processes with their id, arrival time and priority. Each process
needs to run for 5 secs (i.e. the process will exit once it has been scheduled for a total
of 5 secs). You need to tell the completion time for each process.
Input
First line contains number of processes N
Next N lines contain process id, start time and priority
Output
For each process print the time they exited at, in the order 1 to N
Constraints
The process IDs are from 1 to N in input
Start time >= 0
N <= 10^5
Priority <= 10^9
Example
Input
4
1 10 5
200
343
474
Output
15
20
19
17
Explanation
Time
0-1
1-2
2-3
3-4
4-5
5-6
6-7
7-8
8-9
9-10
10-11
11-12
12-13
13-14
14-15
15-16
16-17
17-18
18-19
19-20
Processes
2
2
2
2
2,3
2,3
2,3
2,3,4
2,3,4
2,3,4
2,3,4,1
2,3,4,1
2,3,4,1
2,3,4,1
2,3,4,1
2,3,4
2,3,4
2,3
2,3
2
Scheduled
2
2
2
2
3
3
3
4
4
4
1
1
1
1
1
4
4
3
3
2
Sorting
For this problem, since sorting is not the main idea, we’re allowing you to use Java collections
ArrayList and Sorting to sort the processes by start time. However, you’re not allowed to use
any other collection, or use any more ArrayList that the one we’re providing in codeclass ​Process{
​int ​pid​;
​int ​start​;
​int ​priority​;
​int ​left​; ​// 5 seconds left to be scheduled
​public ​Process(​int ​pid, ​int ​start, ​int ​priority){
​this​.​pid ​= pid;
​this​.​start ​= start;
​this​.​priority ​= priority;
​this​.​left ​= ​5​;
}
}
class ​Sortbystart ​implements ​Comparator<Process>
{
​public int ​compare(Process a, Process b)
{
​return ​a.​start ​- b.​start​;
}
}
ArrayList<Process> processes = ​new ​ArrayList<Process>();
for ​(​int ​i=​0​;i<n;i++){
​int ​id = s.nextInt();
​int ​st = s.nextInt();
​int ​pr = s.nextInt();
processes.add(​new ​Process(id,st,pr));
}
Collections.​sort​(processes, ​new ​Sortbystart());
Note : To get element i of arraylist, instead of ​processes[i] ​arraylist, you use ​processes.get(i)
Download