Как оптимизировать? - C#
Формулировка задачи:
Прохожу [cut] (acmp . ru). Мне выдает при тестах Time Limit Exceed. Как оптимизировать программу?
using System.Linq;
using System.IO;
namespace stats
{
class MainClass
{
public static void Main (string[] args)
{
string[] file = File.ReadAllLines("INPUT.txt");
int[] ass = file[1].Split(' ').Select(x => int.Parse(x)).ToArray();
string three = "";
string four = "";
int Three = 0;
int Four = 0;
foreach (int i in ass) {
if (i % 2 == 0) {
Four++;
four += " " + i;
} else {
Three++;
three += " " + i;
}
}
var a = new StreamWriter("OUTPUT.txt");
a.WriteLine (three);
a.WriteLine (four);
if (Four > Three)
a.WriteLine ("YES");
else
a.WriteLine ("NO");
}
}
}Решение задачи: «Как оптимизировать?»
textual
Листинг программы
string[] file = File.ReadAllLines("INPUT.txt");
int[] arr = file[1].Split(' ').Select(x => int.Parse(x)).ToArray();
int[] threes, fours;
threes = arr.Where(x => x % 2 == 1).ToArray();
fours = arr.Where(x => x % 2 == 0).ToArray();
using (StreamWriter a = new StreamWriter("OUTPUT.txt"))
{
a.WriteLine(String.Join(" ", threes));
a.WriteLine(String.Join(" ", fours));
a.WriteLine(fours.Count() > threes.Count() ? "Yes" : "No");
}