AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC200-D Happy Birthday! 2
C#のソース
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("5");
WillReturn.Add("180 186 189 191 218");
//Yes
//1 1
//2 3 4
}
else if (InputPattern == "Input2") {
WillReturn.Add("2");
WillReturn.Add("123 523");
//Yes
//1 1
//1 2
}
else if (InputPattern == "Input3") {
WillReturn.Add("6");
WillReturn.Add("2013 1012 2765 2021 508 6971");
//No
}
else if (InputPattern == "Input4") {
WillReturn.Add("7");
WillReturn.Add("1 2 4 8 16 32 63");
}
else if (InputPattern == "Input5") {
WillReturn.Add("2");
WillReturn.Add("1 200");
}
else if (InputPattern == "Input6") {
WillReturn.Add("2");
WillReturn.Add("200 1");
}
else if (InputPattern == "Input7") {
WillReturn.Add("5");
WillReturn.Add("1 2 4 8 186");
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
struct JyoutaiDef
{
internal int CurrInd;
internal int BMod;
internal int CMod;
internal List<int> BIndList;
internal List<int> CIndList;
}
static void Main()
{
List<string> InputList = GetInputList();
int[] AArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();
int UB = AArr.GetUpperBound(0);
for (int I = 0; I <= UB; I++) {
AArr[I] %= 200;
}
var Stk = new Stack<JyoutaiDef>();
JyoutaiDef WillPush;
WillPush.CurrInd = 0;
WillPush.BMod = 0;
WillPush.CMod = 0;
WillPush.BIndList = new List<int>();
WillPush.CIndList = new List<int>();
Stk.Push(WillPush);
while (Stk.Count > 0) {
JyoutaiDef Popped = Stk.Pop();
// クリア判定
if (Popped.BIndList.Count > 0 && Popped.CIndList.Count > 0) {
if (Popped.BIndList.SequenceEqual(Popped.CIndList) == false) {
if (Popped.BMod == Popped.CMod) {
Console.WriteLine("Yes");
Action<List<int>> OutAct = pIndList =>
{
string[] StrArr = Array.ConvertAll(pIndList.ToArray(), pX => pX.ToString());
Console.WriteLine("{0} {1}", StrArr.Length, string.Join(" ", StrArr));
};
OutAct(Popped.BIndList);
OutAct(Popped.CIndList);
return;
}
}
}
if (Popped.CurrInd > UB) continue;
// 経路1
WillPush.CurrInd = Popped.CurrInd + 1;
WillPush.BMod = Popped.BMod;
WillPush.BIndList = Popped.BIndList;
WillPush.CMod = Popped.CMod;
WillPush.CIndList = Popped.CIndList;
Stk.Push(WillPush);
// 経路2
WillPush.CurrInd = Popped.CurrInd + 1;
WillPush.BMod = Popped.BMod + AArr[Popped.CurrInd];
WillPush.BMod %= 200;
WillPush.BIndList = new List<int>(Popped.BIndList);
WillPush.BIndList.Add(Popped.CurrInd + 1);
WillPush.CMod = Popped.CMod;
WillPush.CIndList = Popped.CIndList;
Stk.Push(WillPush);
// 経路3
WillPush.CurrInd = Popped.CurrInd + 1;
WillPush.BMod = Popped.BMod;
WillPush.BIndList = Popped.BIndList;
WillPush.CMod = Popped.CMod + AArr[Popped.CurrInd];
WillPush.CMod %= 200;
WillPush.CIndList = new List<int>(Popped.CIndList);
WillPush.CIndList.Add(Popped.CurrInd + 1);
Stk.Push(WillPush);
// 経路4
WillPush.CurrInd = Popped.CurrInd + 1;
WillPush.BMod = Popped.BMod + AArr[Popped.CurrInd];
WillPush.BMod %= 200;
WillPush.CMod = Popped.CMod + AArr[Popped.CurrInd];
WillPush.CMod %= 200;
WillPush.BIndList = new List<int>(Popped.BIndList);
WillPush.BIndList.Add(Popped.CurrInd + 1);
WillPush.CIndList = new List<int>(Popped.CIndList);
WillPush.CIndList.Add(Popped.CurrInd + 1);
Stk.Push(WillPush);
}
Console.WriteLine("No");
}
}
解説
mod 200 での分類は、200通りしかないので
DFSで全探索してます。