Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. class DLLNode<E> {
  6. protected E element;
  7. protected DLLNode<E> pred, succ;
  8.  
  9. public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  10. this.element = elem;
  11. this.pred = pred;
  12. this.succ = succ;
  13. }
  14.  
  15. @Override
  16. public String toString() {
  17. return element.toString();
  18. }
  19. }
  20.  
  21. class DLL<E> {
  22. private DLLNode<E> first, last;
  23.  
  24. public DLL() {
  25. // Construct an empty SLL
  26. this.first = null;
  27. this.last = null;
  28. }
  29.  
  30. public void deleteList() {
  31. first = null;
  32. last = null;
  33. }
  34.  
  35. public int length() {
  36. int ret;
  37. if (first != null) {
  38. DLLNode<E> tmp = first;
  39. ret = 1;
  40. while (tmp.succ != null) {
  41. tmp = tmp.succ;
  42. ret++;
  43. }
  44. return ret;
  45. } else
  46. return 0;
  47.  
  48. }
  49.  
  50. public DLLNode<E> find(E o) {
  51. if (first != null) {
  52. DLLNode<E> tmp = first;
  53. while (tmp.element != o&&tmp.succ != null)
  54. tmp = tmp.succ;
  55. if (tmp.element == o) {
  56. return tmp;
  57. } else {
  58. System.out.println("Elementot ne postoi vo listata");
  59. }
  60. } else {
  61. System.out.println("Listata e prazna");
  62. }
  63. return first;
  64. }
  65.  
  66. public void insertFirst(E o) {
  67. DLLNode<E> ins = new DLLNode<E>(o, null, first);
  68. if (first == null)
  69. last = ins;
  70. else
  71. first.pred = ins;
  72. first = ins;
  73. }
  74.  
  75. public void insertLast(E o) {
  76. if (first == null)
  77. insertFirst(o);
  78. else {
  79. DLLNode<E> ins = new DLLNode<E>(o, last, null);
  80. last.succ = ins;
  81. last = ins;
  82. }
  83. }
  84.  
  85. public void insertAfter(E o, DLLNode<E> after) {
  86. if(after==last){
  87. insertLast(o);
  88. return;
  89. }
  90. DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  91. after.succ.pred = ins;
  92. after.succ = ins;
  93. }
  94.  
  95. public void insertBefore(E o, DLLNode<E> before) {
  96. if(before == first){
  97. insertFirst(o);
  98. return;
  99. }
  100. DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  101. before.pred.succ = ins;
  102. before.pred = ins;
  103. }
  104.  
  105. public E deleteFirst() {
  106. if (first != null) {
  107. DLLNode<E> tmp = first;
  108. first = first.succ;
  109. if (first != null) first.pred = null;
  110. if (first == null)
  111. last = null;
  112. return tmp.element;
  113. } else
  114. return null;
  115. }
  116.  
  117. public E deleteLast() {
  118. if (first != null) {
  119. if (first.succ == null)
  120. return deleteFirst();
  121. else {
  122. DLLNode<E> tmp = last;
  123. last = last.pred;
  124. last.succ = null;
  125. return tmp.element;
  126. }
  127. }
  128. // else throw Exception
  129. return null;
  130. }
  131.  
  132. public E delete(DLLNode<E> node) {
  133. if(node==first){
  134. deleteFirst();
  135. return node.element;
  136. }
  137. if(node==last){
  138. deleteLast();
  139. return node.element;
  140. }
  141. node.pred.succ = node.succ;
  142. node.succ.pred = node.pred;
  143. return node.element;
  144.  
  145. }
  146.  
  147. @Override
  148. public String toString() {
  149. String ret = new String();
  150. if (first != null) {
  151. DLLNode<E> tmp = first;
  152. ret += tmp + "\n";
  153. while (tmp.succ != null) {
  154. tmp = tmp.succ;
  155. ret += tmp + "\n";
  156. }
  157. } else
  158. ret = "nema";
  159. return ret;
  160. }
  161.  
  162. public String toStringR() {
  163. String ret = new String();
  164. if (last != null) {
  165. DLLNode<E> tmp = last;
  166. ret += tmp + "<->";
  167. while (tmp.pred != null) {
  168. tmp = tmp.pred;
  169. ret += tmp + "<->";
  170. }
  171. } else
  172. ret = "Prazna lista!!!";
  173. return ret;
  174. }
  175.  
  176. public DLLNode<E> getFirst() {
  177. return first;
  178. }
  179.  
  180. public DLLNode<E> getLast() {
  181.  
  182. return last;
  183. }
  184.  
  185. public void izvadiDupliIPrebroj(){
  186.  
  187. }
  188. }
  189.  
  190. class Vraboten {
  191. public int id;
  192. public int plata;
  193.  
  194. public Vraboten(int id, int plata) {
  195. this.id = id;
  196. this.plata = plata;
  197. }
  198.  
  199. @Override
  200. public String toString() {
  201. return id + " " + plata;
  202. }
  203.  
  204.  
  205. }
  206.  
  207. public class DLLKompanija {
  208.  
  209.  
  210. public static DLL<Vraboten> podrediVraboten(DLL<Vraboten> lista, int iznos)
  211. {
  212. DLLNode<Vraboten> tmp=lista.getFirst();
  213.  
  214. while(tmp!=null)
  215. {
  216. if(tmp.element.plata<iznos)
  217. {
  218. lista.delete(tmp);
  219. }
  220. tmp=tmp.succ;
  221. }
  222.  
  223. tmp=lista.getFirst();
  224. while(tmp!=null)
  225. {
  226. DLLNode<Vraboten> tmpNext=tmp.succ;
  227. while(tmpNext!=null)
  228. {
  229. if(tmp.element.id<tmpNext.element.id)
  230. {
  231. Vraboten v=tmp.element;
  232. tmp.element=tmpNext.element;
  233. tmpNext.element=v;
  234.  
  235. }
  236. tmpNext=tmpNext.succ;
  237. }
  238. tmp=tmp.succ;
  239. }
  240. return lista;
  241. }
  242. public static void main(String[] args) throws IOException {
  243. DLL<Vraboten> lista = new DLL<Vraboten>();
  244. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  245. String s = stdin.readLine();
  246. int N = Integer.parseInt(s);
  247.  
  248.  
  249. for(int i =0; i< N; i++)
  250. {
  251. s = stdin.readLine();
  252. int id=Integer.parseInt(s);
  253. s = stdin.readLine();
  254. int plata=Integer.parseInt(s);
  255. lista.insertLast(new Vraboten(id, plata));
  256. }
  257.  
  258. s = stdin.readLine();
  259. int iznos = Integer.parseInt(s);
  260.  
  261. System.out.println(podrediVraboten(lista, iznos));
  262. }
  263.  
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement