Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Arrays;
  5. import java.util.NoSuchElementException;
  6. class Process implements Comparable<Process> {
  7. private String name;
  8. private int completeTime;
  9. private int arrivalTime;
  10.  
  11. public Process(String name, int completeTime, int arrivalTime) {
  12. this.name = name;
  13. this.completeTime = completeTime;
  14. this.arrivalTime = arrivalTime;
  15. }
  16.  
  17. public int getCompleteTime() {
  18. return completeTime;
  19. }
  20.  
  21. public void setCompleteTime(int completeTime) {
  22. this.completeTime = completeTime;
  23. }
  24.  
  25. public String getName() {
  26. return name;
  27. }
  28.  
  29. public int getArrivalTime() {
  30. return arrivalTime;
  31. }
  32.  
  33. @Override
  34. public int compareTo(Process p) {
  35. int cmp = Integer.compare(arrivalTime, p.arrivalTime);
  36. if (cmp == 0)
  37. cmp = Integer.compare(p.completeTime, completeTime);
  38. return cmp;
  39. }
  40.  
  41. }
  42. interface Queue<E> {
  43.  
  44. // Elementi na redicata se objekti od proizvolen tip.
  45.  
  46. // Metodi za pristap:
  47.  
  48. public boolean isEmpty();
  49. // Vrakja true ako i samo ako redicata e prazena.
  50.  
  51. public int size();
  52. // Ja vrakja dolzinata na redicata.
  53.  
  54. public E peek();
  55. // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  56.  
  57. // Metodi za transformacija:
  58.  
  59. public void clear();
  60. // Ja prazni redicata.
  61.  
  62. public void enqueue(E x);
  63. // Go dodava x na kraj od redicata.
  64.  
  65. public E dequeue();
  66.  
  67. // Go otstranuva i vrakja pochetniot element na redicata.
  68. public void sort();
  69.  
  70. }
  71. class ArrayQueue<E> implements Queue<E> {
  72.  
  73. // Redicata e pretstavena na sledniot nacin:
  74. // length go sodrzi brojot na elementi.
  75. // Ako length > 0, togash elementite na redicata se zachuvani vo
  76. // elems[front...rear-1]
  77. // Ako rear > front, togash vo elems[front...maxlength-1] i
  78. // elems[0...rear-1]
  79. E[] elems;
  80. int length, front, rear;
  81.  
  82. // Konstruktor ...
  83.  
  84. @SuppressWarnings("unchecked")
  85. public ArrayQueue(int maxlength) {
  86. this.elems = (E[]) new Object[maxlength];
  87. clear();
  88. }
  89.  
  90. public boolean isEmpty() {
  91. // Vrakja true ako i samo ako redicata e prazena.
  92. return (length == 0);
  93. }
  94.  
  95. public int size() {
  96. // Ja vrakja dolzinata na redicata.
  97. return length;
  98. }
  99.  
  100. public E peek() {
  101. // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
  102. if (length > 0)
  103. return elems[front];
  104. else
  105. throw new NoSuchElementException();
  106. }
  107.  
  108. public void clear() {
  109. // Ja prazni redicata.
  110. length = 0;
  111. front = rear = 0; // arbitrary
  112. }
  113.  
  114. public void enqueue(E x) {
  115. // Go dodava x na kraj od redicata.
  116. elems[rear++] = x;
  117. if (rear == elems.length)
  118. rear = 0;
  119. length++;
  120. }
  121.  
  122. public E dequeue() {
  123. // Go otstranuva i vrakja pochetniot element na redicata.
  124. if (length > 0) {
  125. E frontmost = elems[front];
  126. elems[front++] = null;
  127. if (front == elems.length)
  128. front = 0;
  129. length--;
  130. return frontmost;
  131. } else
  132. throw new NoSuchElementException();
  133. }
  134.  
  135. public void sort() {
  136. Arrays.sort(elems);
  137. }
  138. }
  139.  
  140. public class RoundRobin {
  141. public static void main(String args[]) throws IOException {
  142. Queue<Process> queue;
  143. BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  144. int n = Integer.parseInt(bf.readLine());
  145. queue = new ArrayQueue<Process>(n);
  146. for (int i = 0; i < n; i++) {
  147. String[] parts = bf.readLine().split(" ");
  148. Process p = new Process(parts[0], Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
  149. queue.enqueue(p);
  150. }
  151. int runTime = Integer.parseInt(bf.readLine());
  152. bf.close();
  153. queue.sort();
  154. while (!queue.isEmpty()) {
  155. Process p = queue.dequeue();
  156. p.setCompleteTime(p.getCompleteTime() - runTime);
  157. System.out.print(p.getName() + " ");
  158. if (p.getCompleteTime() > 0)
  159. queue.enqueue(p);
  160. }
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement