AtCoderのARC
次のARCの問題へ
前のARCの問題へ
ARC126-B Cross-free Matching
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("3 3");
WillReturn.Add("1 2");
WillReturn.Add("2 3");
WillReturn.Add("3 1");
//2
}
else if (InputPattern == "Input2") {
WillReturn.Add("3 5");
WillReturn.Add("1 1");
WillReturn.Add("2 1");
WillReturn.Add("2 2");
WillReturn.Add("3 2");
WillReturn.Add("3 3");
//3
}
else if (InputPattern == "Input3") {
WillReturn.Add("7 5");
WillReturn.Add("1 7");
WillReturn.Add("7 1");
WillReturn.Add("3 4");
WillReturn.Add("2 6");
WillReturn.Add("5 2");
//1
}
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();
}
struct ItemInfoDef
{
internal long StaX;
internal long EndX;
}
static List<ItemInfoDef> mItemInfoList = new List<ItemInfoDef>();
static void Main()
{
List<string> InputList = GetInputList();
long[] wkArr = { };
Action<string> SplitAct = (pStr) => wkArr = GetSplitArr(pStr);
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
ItemInfoDef WillAdd;
WillAdd.StaX = wkArr[0];
WillAdd.EndX = wkArr[1];
mItemInfoList.Add(WillAdd);
}
var ValList = new List<long>();
foreach (var Item in mItemInfoList.OrderBy(pX => pX.StaX).ThenByDescending(pX => pX.EndX)) {
ValList.Add(Item.EndX);
}
long Answer = ClassDeriveStrictLISLength.DeriveLength(ValList);
Console.WriteLine(Answer);
}
}
#region ClassDeriveStrictLISLength
// LIS(狭義単調増加)の長さを返す
internal class ClassDeriveStrictLISLength
{
// 引数 long型の列挙
// 戻り値 LISの長さ
internal static long DeriveLength(IEnumerable<long> pEnum)
{
// LISの最終値の最小値[LISの長さ] なDP表
var DPSortedList = new SortedList<long, long>();
foreach (long EachVal in pEnum) {
if (DPSortedList.Count == 0) {
DPSortedList[1] = EachVal;
continue;
}
long UpsertKeyInd = ExecNibunhou(DPSortedList, EachVal);
long CurrUB = DPSortedList.Count - 1;
var Keys = DPSortedList.Keys;
// 更新する位置によって分岐
if (UpsertKeyInd <= CurrUB) {
DPSortedList[Keys[(int)UpsertKeyInd]] = EachVal;
}
else {
long PrevKey = Keys[(int)CurrUB];
DPSortedList[PrevKey + 1] = EachVal;
}
}
if (DPSortedList.Count == 0) {
return 0;
}
int UB = DPSortedList.Count - 1;
return DPSortedList.Keys[UB];
}
// 二分法で、引数の値を設定する、キーの配列の添字を返す
private static long ExecNibunhou(SortedList<long, long> pDPSortedList, long pTargetVal)
{
int UB = pDPSortedList.Count - 1;
var Keys = pDPSortedList.Keys;
// 最小値以下の場合
if (pTargetVal <= pDPSortedList[Keys[0]]) {
return 0;
}
// 最大値より大きい場合
if (pTargetVal > pDPSortedList[Keys[UB]]) {
return UB + 1;
}
int L = 0;
int R = UB;
while (L + 1 < R) {
int Mid = (L + R) / 2;
if (pDPSortedList[Keys[Mid]] < pTargetVal) {
L = Mid;
}
else {
R = Mid;
}
}
return R;
}
}
#endregion
解説
考察すると、
StaXの昇順でのEndXのLISを求めれば、分かります。
(StaXが同じ場合は、EndXが降順になるようにする必要があります)
類題