Advertisement
Guest User

Untitled

a guest
May 27th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _5.Swappings
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             int n = int.Parse(Console.ReadLine());
  15.  
  16.             int[] arr = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  17.  
  18.             var dict = new Dictionary<int, Node>();
  19.  
  20.             Node head = null;
  21.             Node tail = null;
  22.             Node prev = null;
  23.  
  24.             for (int i = 1; i <= n; i++)
  25.             {
  26.  
  27.                 var node = new Node(i, prev);
  28.                 prev = node;
  29.                 if (head == null)
  30.                 {
  31.                     head = node;
  32.                 }
  33.  
  34.                 tail = node;
  35.                 dict.Add(i, node);
  36.             }
  37.  
  38.             for (int i = 0; i < arr.Length; i++)
  39.             {
  40.                 Swap(dict[arr[i]], ref head, ref tail);
  41.             }
  42.  
  43.             var current = head;
  44.  
  45.             var result = new List<int>(n);
  46.  
  47.             while (current != null)
  48.             {
  49.                 result.Add(current.Value);
  50.                 current = current.Next;
  51.             }
  52.  
  53.             Console.WriteLine(string.Join(" ", result));
  54.  
  55.  
  56.  
  57.         }
  58.  
  59.         public static void Swap(Node node, ref Node head, ref Node tail)
  60.         {
  61.  
  62.             var nextHead = node.Next;
  63.             var nextTail = node.Previous;
  64.  
  65.             if (node.Next == null)
  66.             {
  67.                 nextHead = node;
  68.             }
  69.             if (node.Previous == null)
  70.             {
  71.                 nextTail = node;
  72.             }
  73.  
  74.             if (nextTail == node)
  75.             {
  76.                 node.Next = null;
  77.             }
  78.             else
  79.             {
  80.                 node.Next = head;
  81.                 head.Previous = node;
  82.             }
  83.  
  84.             if (nextHead == node)
  85.             {
  86.                 node.Previous = null;
  87.             }
  88.             else
  89.             {
  90.                 node.Previous = tail;
  91.                 tail.Next = node;
  92.             }
  93.  
  94.             nextHead.Previous = null;
  95.  
  96.             nextTail.Next = null;
  97.  
  98.             head = nextHead;
  99.             tail = nextTail;
  100.  
  101.         }
  102.     }
  103.  
  104.     class Node
  105.     {
  106.         public Node(int value, Node prev)
  107.         {
  108.             this.Value = value;
  109.             this.Previous = prev;
  110.             if (prev != null)
  111.             {
  112.                 this.Previous.Next = this;
  113.  
  114.             }
  115.         }
  116.  
  117.         public int Value { get; set; }
  118.  
  119.         public Node Next { get; set; }
  120.  
  121.         public Node Previous { get; set; }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement