結果

問題 No.545 ママの大事な二人の子供
ユーザー 14番14番
提出日時 2017-07-14 23:26:41
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 2,854 bytes
コンパイル時間 1,198 ms
コンパイル使用メモリ 109,568 KB
実行使用メモリ 28,416 KB
最終ジャッジ日時 2024-04-16 20:43:56
合計ジャッジ時間 5,782 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
24,832 KB
testcase_01 AC 32 ms
19,072 KB
testcase_02 AC 32 ms
19,072 KB
testcase_03 AC 32 ms
19,200 KB
testcase_04 AC 32 ms
19,072 KB
testcase_05 AC 31 ms
19,072 KB
testcase_06 AC 31 ms
19,200 KB
testcase_07 AC 32 ms
19,328 KB
testcase_08 AC 32 ms
19,328 KB
testcase_09 AC 32 ms
19,328 KB
testcase_10 AC 32 ms
19,200 KB
testcase_11 AC 32 ms
19,328 KB
testcase_12 AC 36 ms
20,224 KB
testcase_13 AC 37 ms
20,480 KB
testcase_14 AC 60 ms
23,168 KB
testcase_15 AC 36 ms
20,224 KB
testcase_16 AC 64 ms
23,296 KB
testcase_17 AC 232 ms
23,296 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

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, Math.Max(this.ShinamonoList.Sum(a => a.ValueA), this.ShinamonoList.Sum(a => a.ValueB)));
        Console.WriteLine(ans);
    }

    private Dictionary<int, Dictionary<long, Dictionary<long, long>>> dic = new Dictionary<int, Dictionary<long, Dictionary<long, long>>>();

    private long GetAns(long min, long max) {
        if(max-min<=1) {
            if(IsValid(0, 0, 0, min)) {
                return min;
            }
            return max;
        }
        long mid = (max + min) / 2;
        if(IsValid(0,0,0,mid)) {
            return GetAns(min, mid);
        }
        return GetAns(mid, max);
        
    }
    private bool IsValid(int idx, long subtotalA, long subtotalB, long diff) {
        if(idx >= this.ShinamonoList.Count) {
            return Math.Abs(subtotalA - subtotalB) <= diff;
        }
        long newSubtotalB = subtotalB + this.ShinamonoList.Skip(idx).Sum(a => a.ValueB);
        if (subtotalA - newSubtotalB > diff) {
            return false;
        }
        long newSubtotalA = subtotalA + this.ShinamonoList.Skip(idx).Sum(a => a.ValueA);
        if(subtotalB - newSubtotalA>diff) {
            return false;
        }
        if (Math.Abs(subtotalA - newSubtotalB) <= diff) {
            return true;
        }
        if (Math.Abs(newSubtotalA - subtotalB) <= diff) {
            return true;
        }
        if(IsValid(idx + 1, subtotalA + this.ShinamonoList[idx].ValueA, subtotalB, diff)) {
            return true;
        }
        if(IsValid(idx + 1, subtotalA, subtotalB + this.ShinamonoList[idx].ValueB, diff)) {
            return true;
        }
        return false;
	}

    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();
	}
}
0