競技プログラミングの鉄則
次の問題へ
前の問題へ
A44 Change and Reverse
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 4");
WillReturn.Add("1 4 8");
WillReturn.Add("3 2");
WillReturn.Add("2");
WillReturn.Add("3 2");
//2
//8
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
SplitAct(InputList[0]);
int N = wkArr[0];
int UB = N - 1;
int[] AArr = new int[UB + 1];
for (int I = 0; I <= UB; I++) {
AArr[I] = I + 1;
}
bool IsReverse = false;
Func<int, int> GetReverseInd = (pInd) =>
{
return UB - pInd;
};
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
int T = wkArr[0];
if (T == 1) {
int X = wkArr[1] - 1;
int Y = wkArr[2];
if (IsReverse) {
X = GetReverseInd(X);
}
AArr[X] = Y;
}
if (T == 2) {
IsReverse = (IsReverse == false);
}
if (T == 3) {
int X = wkArr[1] - 1;
if (IsReverse) {
X = GetReverseInd(X);
}
Console.WriteLine(AArr[X]);
}
}
}
}
解説
配列を反転中かを、bool型に持たせてます。