Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.NoSuchElementException;
- interface Queue<E> {
- // Elementi na redicata se objekti od proizvolen tip.
- // Metodi za pristap:
- public boolean isEmpty ();
- // Vrakja true ako i samo ako redicata e prazena.
- public int size ();
- // Ja vrakja dolzinata na redicata.
- public E peek ();
- // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
- // Metodi za transformacija:
- public void clear ();
- // Ja prazni redicata.
- public void enqueue (E x);
- // Go dodava x na kraj od redicata.
- public E dequeue ();
- // Go otstranuva i vrakja pochetniot element na redicata.
- }
- class SLLNode<E> {
- protected E element;
- protected SLLNode<E> succ;
- public SLLNode(E elem, SLLNode<E> succ) {
- this.element = elem;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- class LinkedQueue<E> implements Queue<E> {
- // Redicata e pretstavena na sledniot nacin:
- // length go sodrzi brojot na elementi.
- // Elementite se zachuvuvaat vo jazli dod SLL
- // front i rear se linkovi do prviot i posledniot jazel soodvetno.
- SLLNode<E> front, rear;
- int length;
- // Konstruktor ...
- public LinkedQueue () {
- clear();
- }
- public boolean isEmpty () {
- // Vrakja true ako i samo ako redicata e prazena.
- return (length == 0);
- }
- public int size () {
- // Ja vrakja dolzinata na redicata.
- return length;
- }
- public E peek () {
- // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
- if (front == null)
- throw new NoSuchElementException();
- return front.element;
- }
- public void clear () {
- // Ja prazni redicata.
- front = rear = null;
- length = 0;
- }
- public void enqueue (E x) {
- // Go dodava x na kraj od redicata.
- SLLNode<E> latest = new SLLNode<E>(x, null);
- if (rear != null) {
- rear.succ = latest;
- rear = latest;
- } else
- front = rear = latest;
- length++;
- }
- public E dequeue () {
- // Go otstranuva i vrakja pochetniot element na redicata.
- if (front != null) {
- E frontmost = front.element;
- front = front.succ;
- if (front == null) rear = null;
- length--;
- return frontmost;
- } else
- throw new NoSuchElementException();
- }
- }
- class ArrayQueue<E> implements Queue<E> {
- // Redicata e pretstavena na sledniot nacin:
- // length go sodrzi brojot na elementi.
- // Ako length > 0, togash elementite na redicata se zachuvani vo elems[front...rear-1]
- // Ako rear > front, togash vo elems[front...maxlength-1] i elems[0...rear-1]
- E[] elems;
- int length, front, rear;
- // Konstruktor ...
- @SuppressWarnings("unchecked")
- public ArrayQueue (int maxlength) {
- elems = (E[]) new Object[maxlength];
- clear();
- }
- public boolean isEmpty () {
- // Vrakja true ako i samo ako redicata e prazena.
- return (length == 0);
- }
- public int size () {
- // Ja vrakja dolzinata na redicata.
- return length;
- }
- public E peek () {
- // Go vrakja elementot na vrvot t.e. pocetokot od redicata.
- if (length > 0)
- return elems[front];
- else
- throw new NoSuchElementException();
- }
- public void clear () {
- // Ja prazni redicata.
- length = 0;
- front = rear = 0; // arbitrary
- }
- public void enqueue (E x) {
- // Go dodava x na kraj od redicata.
- elems[rear++] = x;
- if (rear == elems.length) rear = 0;
- length++;
- }
- public E dequeue () {
- // Go otstranuva i vrakja pochetniot element na redicata.
- if (length > 0) {
- E frontmost = elems[front];
- elems[front++] = null;
- if (front == elems.length) front = 0;
- length--;
- return frontmost;
- } else
- throw new NoSuchElementException();
- }
- }
- class Procesori
- {
- String ime;
- int vremeIzvrshuvanje;
- int vremePristignuvanje;
- public Procesori(String ime, int vremeIzvrshuvanje, int vremePristignuvanje) {
- super();
- this.ime = ime;
- this.vremeIzvrshuvanje = vremeIzvrshuvanje;
- this.vremePristignuvanje = vremePristignuvanje;
- }
- public String getIme() {
- return ime;
- }
- public void setIme(String ime) {
- this.ime = ime;
- }
- public int getVremeIzvrshuvanje() {
- return vremeIzvrshuvanje;
- }
- public void setVremeIzvrshuvanje(int vremeIzvrshuvanje) {
- this.vremeIzvrshuvanje = vremeIzvrshuvanje;
- }
- public int getVremePristignuvanje() {
- return vremePristignuvanje;
- }
- public void setVremePristignuvanje(int vremePristignuvanje) {
- this.vremePristignuvanje = vremePristignuvanje;
- }
- }
- public class RoundRobin {
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- //treba prvo vo lsita da se sortiraat spored vreme na pristignuvanje i izvrshuvanje, pa posle vo queue
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- LinkedQueue<Procesori> redica = new LinkedQueue<Procesori>();
- LinkedQueue<Procesori> sortiraj = new LinkedQueue<Procesori>();
- String s, ime;
- String[] pom;
- int n, vrI, vrP, t;
- s = br.readLine();
- n = Integer.parseInt(s);
- for(int i=0; i<n; i++)
- {
- s = br.readLine();
- pom = s.split(" ");
- ime = pom[0];
- vrI = Integer.parseInt(pom[1]);
- vrP = Integer.parseInt(pom[2]);
- Procesori p = new Procesori(ime, vrI, vrP);
- redica.enqueue(p);
- }
- s = br.readLine();
- t = Integer.parseInt(s);
- Procesori max;
- while(!redica.isEmpty())
- {
- max = redica.dequeue();
- for(int i=0; i<redica.size(); i++)
- {
- Procesori novo = redica.dequeue();
- if(max.vremePristignuvanje > novo.vremePristignuvanje)
- {
- redica.enqueue(max);
- max = novo;
- }
- else
- if(max.vremePristignuvanje == novo.vremePristignuvanje && max.vremeIzvrshuvanje < novo.vremeIzvrshuvanje)
- {
- redica.enqueue(max);
- max = novo;
- }
- else
- redica.enqueue(novo);
- }
- sortiraj.enqueue(max);
- }
- while(!sortiraj.isEmpty())
- {
- Procesori novo = sortiraj.dequeue();
- novo.vremeIzvrshuvanje-=t;
- System.out.print(novo.ime + " ");
- if(novo.vremeIzvrshuvanje > 0)
- sortiraj.enqueue(novo);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement