Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Numerics;
- namespace Timus
- {
- class Program
- {
- static int[] parseInt(string s)
- {
- string[] temp = s.Split(' ');
- List<int> res = new List<int>();
- for (int i = 0; i < temp.Length; i++)
- {
- res.Add(Convert.ToInt32(temp[i]));
- }
- return res.ToArray();
- }
- public static IEnumerable<T[]> NextPermutation<T>(T[] values, int fromInd = 0)
- {
- if (fromInd + 1 == values.Length)
- yield return values;
- else
- {
- foreach (var v in NextPermutation(values, fromInd + 1))
- yield return v;
- for (var i = fromInd + 1; i < values.Length; i++)
- {
- SwapValues(values, fromInd, i);
- foreach (var v in NextPermutation(values, fromInd + 1))
- yield return v;
- SwapValues(values, fromInd, i);
- }
- }
- }
- private static void SwapValues<T>(T[] values, int pos1, int pos2)
- {
- if (pos1 != pos2)
- {
- T tmp = values[pos1];
- values[pos1] = values[pos2];
- values[pos2] = tmp;
- }
- }
- static bool Emulate(List<int> a, List<int> b)
- {
- while (true)
- {
- if (a.Count == 0 && b.Count == 0)
- return false;
- if (a.Count == 0 && b.Count > 0)
- return true;
- if (a.Count > 0 && b.Count == 0)
- return false;
- int l = a[0];
- int r = b[0];
- a.RemoveAt(0);
- b.RemoveAt(0);
- if (l > r)
- {
- a.Add(r);
- a.Add(l);
- }
- if (r > l)
- {
- b.Add(l);
- b.Add(r);
- }
- }
- }
- static void Main(string[] args)
- {
- int n = Convert.ToInt32(Console.ReadLine());
- int[] arr = parseInt(Console.ReadLine());
- if (arr.Length > 4)
- {
- var basic = new[] { arr[arr.Length - 1], arr[arr.Length - 2], arr[arr.Length - 3], arr[arr.Length - 4], arr[arr.Length - 5] };
- var vals = new[] { arr[arr.Length - 1], arr[arr.Length - 2], arr[arr.Length - 3], arr[arr.Length - 4], arr[arr.Length - 5] };
- foreach (var v in NextPermutation(vals))
- {
- if (Emulate(basic.ToList(), v.ToList()))
- {
- Console.WriteLine("YES");
- for (int i = 0; i < arr.Length - 5; i++)
- {
- Console.Write(arr[i] + " ");
- }
- Console.WriteLine(string.Join(" ", v));
- return;
- }
- }
- }
- else
- {
- int[] basic = new int[arr.Length];
- for (int i = 0; i < arr.Length; i++)
- {
- basic[i] = arr[i];
- }
- foreach (var v in NextPermutation(arr))
- {
- if (Emulate(basic.ToList(), v.ToList()))
- {
- Console.WriteLine("YES");
- Console.WriteLine(string.Join(" ", v));
- return;
- }
- }
- Console.WriteLine("NO");
- }
- }
- }
- }
- /*
- 6
- 2 4 3 5 6 1
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement