Resource scheduling: Given a set of customers waiting for service A = {1, 2, …, n }, the expected service time for customer i is t i , and the expected completion time of this customer is d i , that is, T = { t 1 , t 2 ,…, t n }, D = { d 1 , d 2 ,…, d n }. If the service to client i ends before d i , then the service to client i is not delayed; if it ends after d i , then the service is delayed by the time of the service minus d i . Suppose both t i andd i is a positive integer, a schedule is a function f : A → N , where is the start time of service to client i , and all intervals are required to be non-overlapping. The maximum delay of a schedule f is the maximum delay time of all clients, for example:
A ={1, 2, 3, 4, 5}
T= {5, 8, 4, 10, 3}
D = {10, 12 , 15, 11,20}
Then for schedule f1
f 1 :{1, 2, 3, 4, 5} → N
f 1 (1) = 0, f 1 (2) = 5, f 1 (3) = 13, f 1 (4) = 17, f 1 (5) = 27
The latency of clients 1, 2, 3, 4, 5 is 0, 1, 2, 16, 10 respectively; the maximum latency is max{0, 1, 2, 16 ,10}= 16.
However, the maximum delay of different schedules is different, such as another schedule f 2 for the same instance
f2:{1, 2, 3, 4, 5} → N
f2(1)= 0, f2(2)= 15, f2(3) = 23, f2(4) = 5, f2(5) = 27
The delays of clients 1 to 5 are 0, 11, 12, 4, 10 respectively; the maximum delay is max{0, 11, 12, 4, 10}=12.
The arrangements of the above schedules f 1 and f 2 are shown in Figure 1, respectively:
Figure 1 Two different scheduling schemes
Obviously, scheduling f2 has a smaller delay than f1 .
Our problem is: given A = {1, 2, …, n }, T = { t 1 , t 2 , …, t n } and D = { d 1 , d 2 , …, d n }, with The greedy algorithm solves the schedule f with minimal delay (no proof of theoretical correctness is required).
import java.util.Scanner; public class resource scheduling { public static void main(String[] args) { Scanner in=new Scanner (System.in); System.out.println("Enter the number of customers n"); int n=in.nextInt();//Number of customers int t[]=new int[n];//Estimated completion time int d[]=new int[n];//hope completion time System.out.println("Enter the estimated time required for each customer to complete: "); for(int i=0;i<n;i++) { t[i]=in.nextInt(); } System.out.println("Enter the desired completion time for each customer: "); for(int i=0;i<n;i++) { d[i]=in.nextInt(); } int TempSumTime=0;//Total current completion time int maxDelayTime=0;//Maximum delay time int TempDelayTime=0;//Current delay time //Bubble Sort for(int i=1;i<n;i++) { for(int j=0;j<n-i;j++) { if(d[j]>d[j+1]) { swap(d,j,j+1); swap(t,j,j+1); } } } for(int i=0;i<n;i++) { TempSumTime+=t[i]; if(TempSumTime>d[i]) {//Determine whether there is a delay time TempDelayTime=TempSumTime-d[i];//Find the delay time } if(TempDelayTime>maxDelayTime) { maxDelayTime=TempDelayTime;//Update the maximum delay time } } System.out.println("The maximum delay of this schedule f is: "+maxDelayTime); } private static void swap(int[] d, int j, int i) { // TODO auto-generated method stub int temp=d[j]; d[j]=d[i]; d[i]=temp; } }