結果
| 問題 |
No.545 ママの大事な二人の子供
|
| コンテスト | |
| ユーザー |
14番
|
| 提出日時 | 2017-07-14 23:12:04 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,704 bytes |
| コンパイル時間 | 1,075 ms |
| コンパイル使用メモリ | 114,936 KB |
| 実行使用メモリ | 531,248 KB |
| 最終ジャッジ日時 | 2024-10-07 23:50:26 |
| 合計ジャッジ時間 | 8,007 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 18 TLE * 1 MLE * 1 -- * 12 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text;
public class Program
{
public void Proc()
{
int itemCount = int.Parse(Reader.ReadLine());
for (int i = 0; i < itemCount; i++) {
this.ShinamonoList.Add(new Shinamono(Reader.ReadLine()));
}
long ans = GetAns(0, 0, 0);
Console.WriteLine(ans);
}
private Dictionary<int, Dictionary<long, Dictionary<long, long>>> dic = new Dictionary<int, Dictionary<long, Dictionary<long, long>>>();
private long Min = long.MaxValue;
private long GetAns(int idx, long subtotalA, long subtotalB) {
if(idx>=this.ShinamonoList.Count) {
Min = Math.Min(Min, Math.Abs(subtotalA - subtotalB));
return Min;
}
if(subtotalA > subtotalB) {
if(subtotalA-(subtotalB+ShinamonoList.Skip(idx).Sum(a=>a.ValueB))>=Min) {
return long.MaxValue;
}
}
if(subtotalB > subtotalA) {
if(subtotalB-(subtotalA+ShinamonoList.Skip(idx).Sum(a=>a.ValueA))>=Min) {
return long.MaxValue;
}
}
if(!dic.ContainsKey(idx)) {
dic.Add(idx, new Dictionary<long, Dictionary<long, long>>());
}
if(!dic[idx].ContainsKey(subtotalA)) {
dic[idx].Add(subtotalA, new Dictionary<long, long>());
}
if(dic[idx][subtotalA].ContainsKey(subtotalB)) {
return dic[idx][subtotalA][subtotalB];
}
long ans = long.MaxValue;
ans = Math.Min(ans, GetAns(idx + 1, subtotalA + this.ShinamonoList[idx].ValueA, subtotalB));
ans = Math.Min(ans, GetAns(idx + 1, subtotalA, subtotalB + ShinamonoList[idx].ValueB));
dic[idx][subtotalA][subtotalB] = ans;
return ans;
}
private List<Shinamono> ShinamonoList = new List<Shinamono>();
private class Shinamono {
public long ValueA;
public long ValueB;
public Shinamono(string init) {
long[] tmp = init.Split(' ').Select(a => long.Parse(a)).ToArray();
this.ValueA = tmp[0];
this.ValueB = tmp[1];
}
}
public class Reader
{
private static StringReader sr;
public static bool IsDebug = false;
public static string ReadLine()
{
if (IsDebug)
{
if (sr == null)
{
sr = new StringReader(InputText.Trim());
}
return sr.ReadLine();
}
else
{
return Console.ReadLine();
}
}
private static string InputText = @"
1
10 15
";
}
public static void Main(string[] args)
{
#if DEBUG
Reader.IsDebug = true;
#endif
Program prg = new Program();
prg.Proc();
}
}
14番