結果

問題 No.543 命題
ユーザー 14番14番
提出日時 2017-07-14 23:41:36
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 3,318 bytes
コンパイル時間 2,958 ms
コンパイル使用メモリ 108,720 KB
実行使用メモリ 23,532 KB
最終ジャッジ日時 2023-07-29 14:49:49
合計ジャッジ時間 3,245 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
23,532 KB
testcase_01 AC 56 ms
21,600 KB
testcase_02 AC 54 ms
21,496 KB
testcase_03 AC 53 ms
21,500 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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()
	{
        string[] tmp = Reader.ReadLine().Split(' ').Reverse().ToArray();
        Console.WriteLine(string.Join(" ", tmp));
    }

    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 Dictionary<int, Dictionary<long, Dictionary<long, Dictionary<long, bool>>>> dic = new Dictionary<int, Dictionary<long, Dictionary<long, Dictionary<long, bool>>>>();
    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(!dic.ContainsKey(idx)) {
            dic.Add(idx, new Dictionary<long, Dictionary<long, Dictionary<long, bool>>>());
        }
        if(!dic[idx].ContainsKey(subtotalA)) {
            dic[idx].Add(subtotalA, new Dictionary<long, Dictionary<long, bool>>());
        }
        if(!dic[idx][subtotalA].ContainsKey(subtotalB)) {
            dic[idx][subtotalA].Add(subtotalB, new Dictionary<long, bool>());
        }
        if(dic[idx][subtotalA][subtotalB].ContainsKey(diff)) {
            return dic[idx][subtotalA][subtotalB][diff];
        }
        bool ans = false;
        if(IsValid(idx + 1, subtotalA + this.ShinamonoList[idx].ValueA, subtotalB, diff)) {
            ans = true;
        }
        else if( IsValid(idx + 1, subtotalA, subtotalB + this.ShinamonoList[idx].ValueB, diff)) {
            ans = true;
        }
        dic[idx][subtotalA][subtotalB][diff] = 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();
	}
}
0