From 0209b07a352e7ccec09d4435c3298a3ca88b6b11 Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Wed, 17 Oct 2018 16:06:06 +0530 Subject: [PATCH 1/3] Almost Sorted solution --- almost-sorted.cs | 99 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 almost-sorted.cs diff --git a/almost-sorted.cs b/almost-sorted.cs new file mode 100644 index 00000000..332be220 --- /dev/null +++ b/almost-sorted.cs @@ -0,0 +1,99 @@ +using System; +using System.Linq; +using System.Collections.Generic; +using System.Text; + +namespace ConsoleApplication +{ + class Program + { + public static void Main(String[] args) + { + int n = int.Parse(Console.ReadLine()); + int[] ar = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), e => int.Parse(e)); + + if(isSorted(ar, n)) + { + Console.WriteLine("yes"); + return; + } + + int a = -1; + int b = -1; + + for(int i = 0; i < n - 1; i++) + { + if(ar[i] > ar[i + 1]) + { + a = i; + break; + } + } + + for(int i = n - 1; i > 0; i--) + { + if(ar[i] < ar[i - 1]) + { + b = i; + break; + } + } + + if(a != -1 && b != -1) + { + int temp = ar[a]; + ar[a] = ar[b]; + ar[b] = temp; + + if(isSorted(ar, n)) + { + Console.WriteLine("yes"); + Console.WriteLine($"swap {a + 1} {b + 1}"); + return; + } + + ar[b] = ar[a]; + ar[a] = temp; + } + + int[] nar = new int[n]; + + for(int i = 0; i < a; i++) + { + nar[i] = ar[i]; + } + + for(int i = 0; i <= b - a; i++) + { + nar[a + i] = ar[b - i]; + } + + for(int i = b + 1; i < n; i++) + { + nar[i] = ar[i]; + } + + if(isSorted(nar, n)) + { + Console.WriteLine("yes"); + Console.WriteLine($"reverse {a + 1} {b + 1}"); + return; + } + + Console.WriteLine("no"); + } + + public static bool isSorted(int[] ar, int n) + { + for(int i = 1; i < n; i++) + { + if(ar[i] < ar[i - 1]) + { + return false; + } + } + + return true; + } + } +} From f6246d95a3bcb8b0c9c3026d1f4665acacd787be Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Wed, 17 Oct 2018 16:10:59 +0530 Subject: [PATCH 2/3] Angry Children 2 solution --- angry-children-2.cs | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 angry-children-2.cs diff --git a/angry-children-2.cs b/angry-children-2.cs new file mode 100644 index 00000000..c70cbaa4 --- /dev/null +++ b/angry-children-2.cs @@ -0,0 +1,57 @@ +using System; +using System.Linq; +using System.Collections.Generic; +using System.Text; +using System.Numerics; +using System.Diagnostics; + +namespace ConsoleApplication +{ + + class Program + { + public static void Main(String[] args) + { + int n = int.Parse(Console.ReadLine()); + int k = int.Parse(Console.ReadLine()); + decimal[] ar = new decimal[n + 1]; + for(int i = 1; i <= n; i++) + { + ar[i] = int.Parse(Console.ReadLine()); + } + decimal[] s = new decimal[n + 1]; + Array.Sort(ar); + + for(int i = 1; i < n; i++) + { + s[i] = ar[i] + s[i - 1]; + } + + int val = 1 - k; + decimal sum = 0; + for(int i = 1; i <= k; i++) + { + sum += val*ar[i]; + val += 2; + } + + decimal prev = sum; + + for(int i = k + 1; i <= n; i++) + { + decimal newSum = (k - 1) * ar[i]; + newSum += prev; + newSum += (k - 1) * ar[i - k]; + newSum -= 2 * (s[i - 1] - s[i - k]); + + if(newSum <= sum) + { + sum = newSum; + } + prev = newSum; + } + + Console.WriteLine(sum); + } + } +} \ No newline at end of file From 293d1c07067e89692079e1c8d0c95587922d55ed Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Wed, 17 Oct 2018 16:15:10 +0530 Subject: [PATCH 3/3] Delete almost-sorted.cs --- almost-sorted.cs | 99 ------------------------------------------------ 1 file changed, 99 deletions(-) delete mode 100644 almost-sorted.cs diff --git a/almost-sorted.cs b/almost-sorted.cs deleted file mode 100644 index 332be220..00000000 --- a/almost-sorted.cs +++ /dev/null @@ -1,99 +0,0 @@ -using System; -using System.Linq; -using System.Collections.Generic; -using System.Text; - -namespace ConsoleApplication -{ - class Program - { - public static void Main(String[] args) - { - int n = int.Parse(Console.ReadLine()); - int[] ar = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), e => int.Parse(e)); - - if(isSorted(ar, n)) - { - Console.WriteLine("yes"); - return; - } - - int a = -1; - int b = -1; - - for(int i = 0; i < n - 1; i++) - { - if(ar[i] > ar[i + 1]) - { - a = i; - break; - } - } - - for(int i = n - 1; i > 0; i--) - { - if(ar[i] < ar[i - 1]) - { - b = i; - break; - } - } - - if(a != -1 && b != -1) - { - int temp = ar[a]; - ar[a] = ar[b]; - ar[b] = temp; - - if(isSorted(ar, n)) - { - Console.WriteLine("yes"); - Console.WriteLine($"swap {a + 1} {b + 1}"); - return; - } - - ar[b] = ar[a]; - ar[a] = temp; - } - - int[] nar = new int[n]; - - for(int i = 0; i < a; i++) - { - nar[i] = ar[i]; - } - - for(int i = 0; i <= b - a; i++) - { - nar[a + i] = ar[b - i]; - } - - for(int i = b + 1; i < n; i++) - { - nar[i] = ar[i]; - } - - if(isSorted(nar, n)) - { - Console.WriteLine("yes"); - Console.WriteLine($"reverse {a + 1} {b + 1}"); - return; - } - - Console.WriteLine("no"); - } - - public static bool isSorted(int[] ar, int n) - { - for(int i = 1; i < n; i++) - { - if(ar[i] < ar[i - 1]) - { - return false; - } - } - - return true; - } - } -}