Advertisement
dmilosavleski

Шалтерот на МВР

Jan 16th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.NoSuchElementException;
  3. import java.util.Queue;
  4. import java.util.Scanner;
  5.  
  6. class SLLNode<E>{
  7. E data;
  8. SLLNode<E> succ;
  9. SLLNode(E data, SLLNode<E> succ){
  10. this.data = data;
  11. this.succ = succ;
  12. }
  13. }
  14.  
  15. class QueueList<E>{
  16. SLLNode<E> front, end;
  17. int lenght;
  18. QueueList(){
  19. lenght = 0;
  20. front = end = null;
  21. }
  22. public boolean isEmpty(){return (front == null);}
  23. public int size(){return lenght;}
  24. public E peek(){
  25. if(lenght > 0)
  26. return front.data;
  27. else throw new NoSuchElementException();
  28. }
  29. public void clear(){
  30. lenght = 0;
  31. front = end = null;
  32. }
  33. public void enqueue(E data){
  34. SLLNode<E> ins = new SLLNode<>(data, null);
  35. if(end != null){
  36. end.succ = ins;
  37. end = ins;
  38. }
  39. else front = end = ins;
  40. lenght++;
  41. }
  42. public E dequeue(){
  43. if(front != null){
  44. E ret = front.data;
  45. front = front.succ;
  46. if(front == null) end = null;
  47. lenght--;
  48. return ret;
  49. }
  50. else throw new NoSuchElementException();
  51. }
  52. public E poll(){
  53. if(front == null) throw new NoSuchElementException();
  54. return front.data;
  55. }
  56. }
  57.  
  58. class Gragjanin{
  59. private String ip;
  60. private int lk;
  61. private int pasos;
  62. private int vd;
  63. public Gragjanin(String ip, int lk, int pasos, int vd){
  64. this.ip = ip;
  65. this.lk = lk;
  66. this.pasos = pasos;
  67. this.vd = vd;
  68. }
  69.  
  70. public String getIp() {
  71. return ip;
  72. }
  73.  
  74. public int getLk() {
  75. return lk;
  76. }
  77.  
  78. public int getPasos() {
  79. return pasos;
  80. }
  81.  
  82. public int getVd() {
  83. return vd;
  84. }
  85. }
  86.  
  87.  
  88. public class MVR {
  89.  
  90. public static void main(String[] args) {
  91.  
  92. Scanner br = new Scanner(System.in);
  93. int N = Integer.parseInt(br.nextLine());
  94.  
  95. ArrayList<Gragjanin> fin = new ArrayList<Gragjanin>(N);
  96. QueueList<Gragjanin> l = new QueueList<>();
  97. QueueList<Gragjanin> p = new QueueList<>();
  98. QueueList<Gragjanin> v = new QueueList<>();
  99.  
  100. for(int i=1;i<=N;i++){
  101. String imePrezime = br.nextLine();
  102. int lKarta = Integer.parseInt(br.nextLine());
  103. int pasos = Integer.parseInt(br.nextLine());
  104. int vozacka = Integer.parseInt(br.nextLine());
  105. Gragjanin covek = new Gragjanin(imePrezime,lKarta,pasos,vozacka);
  106. if(covek.getLk() == 1)
  107. l.enqueue(covek);
  108. else if(covek.getPasos() == 1)
  109. p.enqueue(covek);
  110. else v.enqueue(covek);
  111. }
  112.  
  113.  
  114. while(!l.isEmpty()){
  115. Gragjanin covek = l.dequeue();
  116. if(covek.getPasos() == 1)
  117. p.enqueue(covek);
  118. else if(covek.getVd() == 1)
  119. v.enqueue(covek);
  120. else fin.add(covek); //koga zavrsuva so lk, ako saka da vadi pasos odi vo red za pasos inaku proveri dali e za vd i smesti go vo toj red, inaku zavrsi
  121. }
  122.  
  123. while(!p.isEmpty()){
  124. Gragjanin covek = p.dequeue(); //koga zavrsuva so pasos, ako mu se ceka i za vozacka odi vo toj red, inaku zavrsuva;
  125. if(covek.getVd() == 1)
  126. v.enqueue(covek);
  127. else fin.add(covek);
  128. }
  129.  
  130. while(!v.isEmpty()) fin.add(v.dequeue()); //izvadi gi site od redot za vozacka
  131.  
  132. for(Gragjanin x : fin){
  133. System.out.println(x.getIp());
  134. }
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement