JavaAlgorithms/MinimizingLateness/MinimizingLateness.java

61 lines
1.9 KiB
Java
Raw Normal View History

package MinimizingLateness;
2019-05-22 10:00:47 +08:00
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
2017-11-13 14:16:15 +08:00
public class MinimizingLateness {
2020-10-24 18:23:28 +08:00
private static class Schedule { // Schedule class
int t = 0; // Time required for the operation to be performed
int d = 0; // Time the job should be completed
int s = 0; // Start time of the task
int f = 0; // End time of the operation
2017-11-13 14:16:15 +08:00
2020-10-24 18:23:28 +08:00
public Schedule(int t, int d) {
this.t = t;
this.d = d;
}
2020-10-24 18:23:28 +08:00
}
2017-11-13 14:16:15 +08:00
2020-10-24 18:23:28 +08:00
public static void main(String[] args) throws IOException {
StringTokenizer token;
2017-11-13 14:16:15 +08:00
2020-10-24 18:23:28 +08:00
BufferedReader in = new BufferedReader(new FileReader("MinimizingLateness/lateness_data.txt"));
String ch = in.readLine();
if (ch == null || ch.isEmpty()) {
in.close();
return;
}
int indexCount = Integer.parseInt(ch);
System.out.println("Input Data : ");
System.out.println(indexCount); // number of operations
Schedule[] array = new Schedule[indexCount]; // Create an array to hold the operation
int i = 0;
while ((ch = in.readLine()) != null) {
token = new StringTokenizer(ch, " ");
// Include the time required for the operation to be performed in the array and the time it
// should be completed.
array[i] =
new Schedule(Integer.parseInt(token.nextToken()), Integer.parseInt(token.nextToken()));
i++;
System.out.println(array[i - 1].t + " " + array[i - 1].d);
}
2017-11-13 14:16:15 +08:00
2020-10-24 18:23:28 +08:00
int tryTime = 0; // Total time worked
int lateness = 0; // Lateness
for (int j = 0; j < indexCount - 1; j++) {
array[j].s = tryTime; // Start time of the task
array[j].f = tryTime + array[j].t; // Time finished
tryTime = tryTime + array[j].t; // Add total work time
// Lateness
lateness = lateness + Math.max(0, tryTime - array[j].d);
}
2020-10-24 18:23:28 +08:00
System.out.println();
System.out.println("Output Data : ");
System.out.println(lateness);
in.close();
}
2017-11-13 14:16:15 +08:00
}