Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Војска Problem 2 (1 / 1)
- Пред командантот на војската наредени се сите војници и во двојно поврзана листа дадени се нивните ID-a. На командантот не му се допаѓа како се наредени војниците и решава да одбере два под-интервали од војници и да им ги замени местата, односно војниците што се наоѓаат во едниот под-интервал ќе ги смести во другиот, и обратно.
- Влез: Во првиот ред даден е бројот на војници. Во вториот ред дадени се ID-то на секој од војниците. Во третиот ред дадени се два броеви, ID на првиот војник и ID на последниот војник од првиот интервал. Во четвртиот ред дадени се два броеви, ID на првиот војник и ID на последниот војник од вториот интервал.
- Излез: Да се испечати новиот редослед на војниците (т.е. на нивните ID-a)
- Забелешка 1: Интервалите никогаш нема да се преклопуваат и ќе содржат барем еден војник. Целата низа ќе содржи најмалку два војника. Забелешка 2: Обратете посебно внимание кога интервалите се еден до друг и кога некој од интервалите почнува од првиот војник или завршува со последниот војник.
- Име на класата: DLLVojska
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- class DLLNode<E> {
- protected E element;
- protected DLLNode<E> pred, succ;
- public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
- this.element = elem;
- this.pred = pred;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return "<-"+element.toString()+"->";
- }
- }
- class DLL<E> {
- private DLLNode<E> first, last;
- public DLL() {
- // Construct an empty SLL
- this.first = null;
- this.last = null;
- }
- public void deleteList() {
- first = null;
- last = null;
- }
- public int length() {
- int ret;
- if (first != null) {
- DLLNode<E> tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- public void insertFirst(E o) {
- DLLNode<E> ins = new DLLNode<E>(o, null, first);
- if (first == null)
- last = ins;
- else
- first.pred = ins;
- first = ins;
- }
- public void insertLast(E o) {
- if (first == null)
- insertFirst(o);
- else {
- DLLNode<E> ins = new DLLNode<E>(o, last, null);
- last.succ = ins;
- last = ins;
- }
- }
- public void insertAfter(E o, DLLNode<E> after) {
- if(after==last){
- insertLast(o);
- return;
- }
- DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
- after.succ.pred = ins;
- after.succ = ins;
- }
- public void insertBefore(E o, DLLNode<E> before) {
- if(before == first){
- insertFirst(o);
- return;
- }
- DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
- before.pred.succ = ins;
- before.pred = ins;
- }
- public E deleteFirst() {
- if (first != null) {
- DLLNode<E> tmp = first;
- first = first.succ;
- if (first != null) first.pred = null;
- if (first == null)
- last = null;
- return tmp.element;
- } else
- return null;
- }
- public E deleteLast() {
- if (first != null) {
- if (first.succ == null)
- return deleteFirst();
- else {
- DLLNode<E> tmp = last;
- last = last.pred;
- last.succ = null;
- return tmp.element;
- }
- }
- // else throw Exception
- return null;
- }
- @Override
- public String toString() {
- String ret = new String();
- if (first != null) {
- DLLNode<E> tmp = first;
- ret += tmp + "<->";
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret += tmp + "<->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public DLLNode<E> getFirst() {
- return first;
- }
- public DLLNode<E> getLast() {
- return last;
- }
- }
- public class DLLVojska {
- public static void main(String[] args) throws IOException {
- DLL<Integer> lista = new DLL<Integer>();
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- String s = stdin.readLine();
- int N = Integer.parseInt(s);
- s = stdin.readLine();
- String[] ids = s.split(" ");
- for (int i = 0; i < N; i++) {
- lista.insertLast(Integer.parseInt(ids[i]));
- }
- s = stdin.readLine();
- String interval[] = s.split(" ");
- int a = Integer.parseInt(interval[0]);
- int b = Integer.parseInt(interval[1]);
- s = stdin.readLine();
- interval = s.split(" ");
- int c = Integer.parseInt(interval[0]);
- int d = Integer.parseInt(interval[1]);
- DLL<Integer> result = vojska(lista, a, b, c, d);
- DLLNode<Integer> node = result.getFirst();
- System.out.print(node.element);
- node = node.succ;
- while(node != null){
- System.out.print(" "+node.element);
- node = node.succ;
- }
- }
- private static DLL podeli_interval(DLL<Integer> lista, int a, int b){
- DLLNode<Integer> d1 = lista.getFirst();
- DLL<Integer> rez = new DLL();
- if(a == b){
- rez.insertLast(a);
- return rez;
- }
- boolean flag = false;
- while(d1 != null&&d1.element != b){
- if(d1.element == a){
- rez.insertLast(d1.element);
- flag = true;
- } else if(flag == true){
- rez.insertLast(d1.element);
- }
- d1 = d1.succ;
- }
- rez.insertLast(b);
- return rez;
- }
- private static DLL<Integer> vojska(DLL<Integer> lista, int a, int b, int c, int d) {
- DLL<Integer> rez = new DLL();
- DLL<Integer> prv_interval = podeli_interval(lista,a,b);
- DLL<Integer> vtor_interval = podeli_interval(lista,c,d);
- DLLNode<Integer> d1 = lista.getFirst();
- int n = lista.length();
- int brojac = 0;
- while(d1 != null && brojac < n){
- if(d1.element == a){
- //dodavaj od 2 interval
- DLLNode<Integer> d2 = vtor_interval.getFirst();
- while(d2 != null){
- rez.insertLast(d2.element);
- d2 = d2.succ;
- }
- while(d1.element != b){
- d1 = d1.succ;
- brojac++;
- }
- } else if(d1.element == c){
- //dodavaj od 1 interval
- DLLNode<Integer> d2 = prv_interval.getFirst();
- while(d2 != null){
- rez.insertLast(d2.element);
- d2 = d2.succ;
- }
- while(d1.element != d){
- d1 = d1.succ;
- brojac++;
- }
- } else {
- rez.insertLast(d1.element);
- }
- d1 = d1.succ;
- brojac++;
- }
- return rez;
- }
- }
- Input:
- 10
- 1561 516 13213 6515 16523 9498 54661 161561 1516 897
- 516 6515
- 9498 54661
- Output:
- 1561 9498 54661 16523 516 13213 6515 161561 1516 897
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement