Advertisement
Guest User

Untitled

a guest
Jan 5th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Numerics;
  7.  
  8. namespace Timus
  9. {
  10.  
  11.     class Program
  12.     {
  13.         static int[] parseInt(string s)
  14.         {
  15.             string[] temp = s.Split(' ');
  16.             List<int> res = new List<int>();
  17.             for (int i = 0; i < temp.Length; i++)
  18.             {
  19.                 res.Add(Convert.ToInt32(temp[i]));
  20.             }
  21.             return res.ToArray();
  22.         }
  23.  
  24.         public static IEnumerable<T[]> NextPermutation<T>(T[] values, int fromInd = 0)
  25.         {
  26.             if (fromInd + 1 == values.Length)
  27.                 yield return values;
  28.             else
  29.             {
  30.                 foreach (var v in NextPermutation(values, fromInd + 1))
  31.                     yield return v;
  32.  
  33.                 for (var i = fromInd + 1; i < values.Length; i++)
  34.                 {
  35.                     SwapValues(values, fromInd, i);
  36.                     foreach (var v in NextPermutation(values, fromInd + 1))
  37.                         yield return v;
  38.                     SwapValues(values, fromInd, i);
  39.                 }
  40.             }
  41.         }
  42.  
  43.         private static void SwapValues<T>(T[] values, int pos1, int pos2)
  44.         {
  45.             if (pos1 != pos2)
  46.             {
  47.                 T tmp = values[pos1];
  48.                 values[pos1] = values[pos2];
  49.                 values[pos2] = tmp;
  50.             }
  51.         }
  52.  
  53.         static bool Emulate(List<int> a, List<int> b)
  54.         {
  55.             while (true)
  56.             {
  57.                 if (a.Count == 0 && b.Count == 0)
  58.                     return false;
  59.                 if (a.Count == 0 && b.Count > 0)
  60.                     return true;
  61.                 if (a.Count > 0 && b.Count == 0)
  62.                     return false;
  63.                 int l = a[0];
  64.                 int r = b[0];
  65.                 a.RemoveAt(0);
  66.                 b.RemoveAt(0);
  67.                 if (l > r)
  68.                 {
  69.                     a.Add(r);
  70.                     a.Add(l);
  71.                 }
  72.                 if (r > l)
  73.                 {
  74.                     b.Add(l);
  75.                     b.Add(r);
  76.                 }
  77.             }
  78.         }
  79.  
  80.         static void Main(string[] args)
  81.         {
  82.             int n = Convert.ToInt32(Console.ReadLine());
  83.             int[] arr = parseInt(Console.ReadLine());
  84.  
  85.             if (arr.Length > 4)
  86.             {
  87.                 var basic = new[] { arr[arr.Length - 1], arr[arr.Length - 2], arr[arr.Length - 3], arr[arr.Length - 4], arr[arr.Length - 5] };
  88.                 var vals = new[] { arr[arr.Length - 1], arr[arr.Length - 2], arr[arr.Length - 3], arr[arr.Length - 4], arr[arr.Length - 5] };
  89.  
  90.                 foreach (var v in NextPermutation(vals))
  91.                 {
  92.                     if (Emulate(basic.ToList(), v.ToList()))
  93.                     {
  94.                         Console.WriteLine("YES");
  95.                         for (int i = 0; i < arr.Length - 5; i++)
  96.                         {
  97.                             Console.Write(arr[i] + " ");
  98.                         }
  99.                         Console.WriteLine(string.Join(" ", v));
  100.                         return;
  101.                     }
  102.                    
  103.                 }
  104.             }
  105.             else
  106.             {
  107.                 int[] basic = new int[arr.Length];
  108.                 for (int i = 0; i < arr.Length; i++)
  109.                 {
  110.                     basic[i] = arr[i];
  111.                 }
  112.                 foreach (var v in NextPermutation(arr))
  113.                 {
  114.                     if (Emulate(basic.ToList(), v.ToList()))
  115.                     {
  116.                         Console.WriteLine("YES");
  117.                         Console.WriteLine(string.Join(" ", v));
  118.                         return;
  119.                     }
  120.                 }
  121.                 Console.WriteLine("NO");
  122.             }
  123.         }
  124.     }
  125. }
  126.  
  127. /*
  128. 6
  129. 2 4 3 5 6 1
  130.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement