競技プログラミングの鉄則
次の問題へ
前の問題へ
A40 Triangle
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("7");
WillReturn.Add("1 2 1 2 1 2 1");
//5
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
long[] AArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();
var CntDict = new Dictionary<long, long>();
foreach (long EachA in AArr) {
if (CntDict.ContainsKey(EachA) == false) {
CntDict[EachA] = 0;
}
CntDict[EachA]++;
}
long Answer = 0;
foreach (long EachVal in CntDict.Values) {
if (EachVal < 3) continue;
Answer += EachVal * (EachVal - 1) * (EachVal - 2) / 6;
}
Console.WriteLine(Answer);
}
}
解説
度数分布表を求めてから、Chooseの公式を使ってます。