AtCoderのABC
前のABCの問題へ
ABC439-E Kite
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");
WillReturn.Add("3 5");
WillReturn.Add("1 4");
WillReturn.Add("2 6");
//2
}
else if (InputPattern == "Input2") {
WillReturn.Add("5");
WillReturn.Add("1 2");
WillReturn.Add("1 3");
WillReturn.Add("1 4");
WillReturn.Add("1 5");
WillReturn.Add("1 6");
//1
}
else if (InputPattern == "Input3") {
WillReturn.Add("10");
WillReturn.Add("440423913 766294629");
WillReturn.Add("725560240 59187619");
WillReturn.Add("965580535 585990756");
WillReturn.Add("550925213 623321125");
WillReturn.Add("549392044 122410708");
WillReturn.Add("21524934 690874816");
WillReturn.Add("529970099 244587368");
WillReturn.Add("757265587 736247509");
WillReturn.Add("576136367 993115118");
WillReturn.Add("219853537 21553211");
//4
}
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が降順になるようにする必要があります)
類題