Created
September 18, 2017 10:29
-
-
Save basp1/b69511cfce7e0f4fc3f8774cf62bc57d to your computer and use it in GitHub Desktop.
Яндекс.Блиц. A
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://contest.yandex.ru/hiring/contest/5048/problems/A/ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace yandex.blitz | |
{ | |
public class A_Game | |
{ | |
public enum IO_Type { FILE, CONSOLE }; | |
public static IO_Type ioType = IO_Type.CONSOLE; | |
public static readonly string petyaName = "Petya"; | |
public static readonly string vasyaName = "Vasya"; | |
public static void Main(string[] args) | |
{ | |
HashSet<int> petya; | |
HashSet<int> vasya; | |
LoadFromInput(out petya, out vasya); | |
int petyaSum = petya.Sum(); | |
int vasyaSum = vasya.Sum(); | |
CalcWinner(petya, vasya, petyaSum, vasyaSum); | |
} | |
static bool LoadFromFile(string fname, out HashSet<int> petya, out HashSet<int> vasya) | |
{ | |
using (var file = new System.IO.StreamReader(fname)) | |
{ | |
var firstLine = file.ReadLine(); | |
var secondLine = file.ReadLine(); | |
LoadData(firstLine, secondLine, out petya, out vasya); | |
return true; | |
} | |
} | |
static bool LoadFromInput(out HashSet<int> petya, out HashSet<int> vasya) | |
{ | |
var firstLine = System.Console.ReadLine(); | |
var secondLine = System.Console.ReadLine(); | |
LoadData(firstLine, secondLine, out petya, out vasya); | |
return true; | |
} | |
static bool LoadData(string firstLine, string secondLine, out HashSet<int> petya, out HashSet<int> vasya) | |
{ | |
int n; | |
petya = new HashSet<int>(); | |
vasya = new HashSet<int>(); | |
if (!int.TryParse(firstLine, out n)) | |
{ | |
return false; | |
} | |
if (n < 2 || n > 1000) | |
{ | |
return false; | |
} | |
var values = secondLine.Split(' '); | |
for (int i = 0; i < values.Length; i += 2) | |
{ | |
int odd = int.Parse(values[i]); | |
int even = int.Parse(values[i + 1]); | |
petya.Add(odd); | |
vasya.Add(even); | |
} | |
return true; | |
} | |
static void PrintWinner(string winner) | |
{ | |
switch (ioType) | |
{ | |
case IO_Type.CONSOLE: | |
Console.WriteLine(winner); | |
break; | |
case IO_Type.FILE: | |
using (var file = new System.IO.StreamWriter("output.txt")) | |
{ | |
file.WriteLine(winner); | |
} | |
break; | |
} | |
} | |
static void CalcWinner(HashSet<int> petya, HashSet<int> vasya, int petyaVal, int vasyaVal) | |
{ | |
if (petyaVal > vasyaVal) | |
{ | |
PrintWinner(petyaName); | |
} | |
else if (petyaVal < vasyaVal) | |
{ | |
PrintWinner(vasyaName); | |
} | |
else if (0 == petya.Count || 0 == vasya.Count) | |
{ | |
PrintWinner(petyaName); | |
} | |
else | |
{ | |
petyaVal = petya.Max(); petya.Remove(petyaVal); | |
vasyaVal = vasya.Max(); vasya.Remove(vasyaVal); | |
CalcWinner(petya, vasya, petyaVal, vasyaVal); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment