AtCoderのARC
次のARCの問題へ
前のARCの問題へ
ARC133-B Dividing Subsequence
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("4");
WillReturn.Add("3 1 4 2");
WillReturn.Add("4 2 1 3");
//2
}
else if (InputPattern == "Input2") {
WillReturn.Add("5");
WillReturn.Add("1 2 3 4 5");
WillReturn.Add("5 4 3 2 1");
//3
}
else if (InputPattern == "Input3") {
WillReturn.Add("10");
WillReturn.Add("4 3 1 10 9 2 8 6 5 7");
WillReturn.Add("9 6 5 4 2 3 8 10 1 7");
//6
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static void Main()
{
List<string> InputList = GetInputList();
long[] PArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();
long[] QArr = InputList[2].Split(' ').Select(pX => long.Parse(pX)).ToArray();
// QのInd[Qの値]なDict
var QIndDict = new Dictionary<long, long>();
for (long I = 0; I <= QArr.GetUpperBound(0); I++) {
QIndDict[QArr[I]] = I;
}
// 可能なQのIndのList [Pの値] なDict
var QIndListDict = new Dictionary<long, List<long>>();
foreach (long EachQ in QArr) {
long[] YakusuuArr = DeriveYakusuuArr(EachQ);
foreach (long EachYakusuu in YakusuuArr) {
if (QIndListDict.ContainsKey(EachYakusuu) == false) {
QIndListDict[EachYakusuu] = new List<long>();
}
QIndListDict[EachYakusuu].Add(QIndDict[EachQ]);
}
}
var ValList = new List<long>();
foreach (long EachP in PArr) {
if (QIndListDict.ContainsKey(EachP)) {
for (long I = QIndListDict[EachP].Count - 1; 0 <= I; I--) {
ValList.Add(QIndListDict[EachP][(int)I]);
}
}
}
long LISLen = ClassDeriveStrictLISLength.DeriveLength(ValList);
Console.WriteLine(LISLen);
}
// 約数を列挙する
static long[] DeriveYakusuuArr(long pTarget)
{
var YakusuuSet = new HashSet<long>();
for (long I = 1; I * I <= pTarget; I++) {
if (pTarget % I == 0) {
YakusuuSet.Add(I);
YakusuuSet.Add(pTarget / I);
}
}
long[] YakusuuArr = YakusuuSet.ToArray();
Array.Sort(YakusuuArr);
return YakusuuArr;
}
}
#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
解説
Pが(3,1,4,2)
Qが(4,2,1,3)
で考察すると
Pの3はQのInd3ならOK
Pの1はQのInd0,1,2,3ならOK
Pの4はQのInd0ならOK
Pの2はQのInd0,1ならOK
以上により
(3 0 1 2 3 0 0 1)
という数列での狭義単調増加のLISを求める問題に帰着できます。
類題