結果

問題 No.545 ママの大事な二人の子供
ユーザー fgwiebfaoishfgwiebfaoish
提出日時 2020-09-07 01:47:53
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 3,638 bytes
コンパイル時間 2,574 ms
コンパイル使用メモリ 106,232 KB
実行使用メモリ 23,380 KB
最終ジャッジ日時 2023-08-19 14:37:29
合計ジャッジ時間 6,068 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
20,900 KB
testcase_01 AC 58 ms
21,008 KB
testcase_02 AC 59 ms
20,944 KB
testcase_03 AC 60 ms
22,896 KB
testcase_04 AC 59 ms
20,908 KB
testcase_05 AC 58 ms
20,856 KB
testcase_06 AC 58 ms
20,856 KB
testcase_07 AC 58 ms
22,904 KB
testcase_08 AC 57 ms
22,948 KB
testcase_09 AC 59 ms
20,916 KB
testcase_10 AC 60 ms
20,916 KB
testcase_11 AC 59 ms
20,960 KB
testcase_12 AC 58 ms
20,796 KB
testcase_13 AC 58 ms
18,888 KB
testcase_14 AC 59 ms
20,936 KB
testcase_15 AC 59 ms
22,912 KB
testcase_16 AC 58 ms
18,820 KB
testcase_17 AC 59 ms
20,940 KB
testcase_18 AC 60 ms
20,816 KB
testcase_19 AC 60 ms
20,856 KB
testcase_20 AC 62 ms
20,920 KB
testcase_21 AC 59 ms
20,796 KB
testcase_22 AC 72 ms
20,996 KB
testcase_23 AC 86 ms
23,340 KB
testcase_24 AC 68 ms
21,000 KB
testcase_25 AC 82 ms
23,352 KB
testcase_26 AC 87 ms
21,284 KB
testcase_27 AC 79 ms
21,392 KB
testcase_28 AC 89 ms
23,380 KB
testcase_29 AC 87 ms
21,288 KB
testcase_30 AC 81 ms
21,328 KB
testcase_31 AC 84 ms
23,324 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.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
using nint=System.Int64;
static class Program{
	const int mod=(int)1e9+7;
	const double eps=1e-11;
	static void Main(){
		Sc sc=new Sc();
		var n=sc.I;
		var a=new (long,long)[n>>1];
		var b=new (long,long)[n-a.Length];
		for(int i = 0;i<n;i++) {
			var e=sc.La;
			if(i<a.Length){a[i]=(e[0],e[1]);}
			else{b[i-a.Length]=(e[0],e[1]);}
		}
		int m=1<<b.Length;
		var dp2=new long[m];
		for(int i = 0;i<b.Length;i++) {dp2[0]-=b[i].Item2;}
		for(int i = 1,j=0;i<m;i++) {
			if(1<<(j+1)==i){j++;}
			dp2[i]=dp2[i-(1<<j)]+b[j].Item2+b[j].Item1;
		}
		Array.Sort(dp2);
		m=1<<a.Length;
		var dp1=new long[m];
		for(int i = 0;i<a.Length;i++) {dp1[0]-=a[i].Item2;}
		long ans=long.MaxValue>>1;
		var p=dp2.Bs(-dp1[0],true);
		if(p>=0){ans=Min(ans,Abs(dp1[0]+dp2[p]));}
		if(p+1!=1<<b.Length){ans=Min(ans,Abs(dp1[0]+dp2[p+1]));}
		for(int i = 1,j=0;i<m;i++) {
			if(1<<(j+1)==i){j++;}
			dp1[i]=dp1[i-(1<<j)]+a[j].Item2+a[j].Item1;
			p=dp2.Bs(-dp1[i],true);
			if(p>=0){ans=Min(ans,Abs(dp1[i]+dp2[p]));}
			if(p+1!=1<<b.Length){ans=Min(ans,Abs(dp1[i]+dp2[p+1]));}
		}
		Console.WriteLine(ans);
	}
}
static class Ex1{
	public static int Bs(this nint[] a,nint x,bool b){return Bs2(a,x,b,a.Length);}
	public static int Bs(this nint[] a,nint x,bool b,int ub){return Bs2(a,x,b,ub+1);}
	private static int Bs2(nint[] a,nint x,bool b,int ub){
		int lb=-1,mid=0;
		while(ub-lb>1){
			mid=(ub+lb)/2;
			if(a[mid]>=x){ub=mid;}
			else{lb=mid;}
		}
		return b?lb:ub;
	}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),int.Parse);}
	public int[] Ia3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),long.Parse);}
	public long[] La3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}
0