AtCoderのABC
次のABCの問題へ
前のABCの問題へ
ABC470-C Inc, Dec, Xor
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("2 5");
WillReturn.Add("1 2");
WillReturn.Add("1 2");
WillReturn.Add("1 1");
WillReturn.Add("2");
WillReturn.Add("2");
//1
//2
//3
//1
//0
}
else if (InputPattern == "Input2") {
WillReturn.Add("3 8");
WillReturn.Add("1 2");
WillReturn.Add("1 3");
WillReturn.Add("1 1");
WillReturn.Add("1 2");
WillReturn.Add("1 1");
WillReturn.Add("2");
WillReturn.Add("1 3");
WillReturn.Add("1 1");
//1
//0
//1
//2
//1
//0
//1
//2
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static long[] GetSplitArr(string pStr)
{
return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => long.Parse(pX)).ToArray();
}
static void Main()
{
List<string> InputList = GetInputList();
long[] wkArr = { };
Action<string> SplitAct = (pStr) => wkArr = GetSplitArr(pStr);
// 値[添字]なDict
var ValDict = new Dictionary<long, long>();
long Answer = 0;
var sb = new System.Text.StringBuilder();
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
long Type = wkArr[0];
if (Type == 1) {
long Target = wkArr[1];
if (ValDict.ContainsKey(Target)) {
Answer ^= ValDict[Target];
}
else {
ValDict[Target] = 0;
}
ValDict[Target]++;
Answer ^= ValDict[Target];
}
if (Type == 2) {
var WillRemoveList = new List<long>();
foreach (long EachKey in ValDict.Keys.ToArray()) {
Answer ^= ValDict[EachKey];
ValDict[EachKey]--;
if (ValDict[EachKey] == 0) {
WillRemoveList.Add(EachKey);
}
else {
Answer ^= ValDict[EachKey];
}
}
WillRemoveList.ForEach(pX => ValDict.Remove(pX));
}
sb.Append(Answer);
sb.AppendLine();
}
Console.Write(sb.ToString());
}
}
解説
XORの世界では、足し算と引き算は、同じことをふまえて、
値[添字]なDictを用意して、解を差分更新してます。