結果

問題 No.968 引き算をして門松列(その3)
ユーザー 37zigen37zigen
提出日時 2019-12-30 01:53:22
言語 Java19
(openjdk 21)
結果
AC  
実行時間 1,043 ms / 2,000 ms
コード長 2,029 bytes
コンパイル時間 2,420 ms
コンパイル使用メモリ 85,332 KB
実行使用メモリ 67,228 KB
最終ジャッジ日時 2023-08-06 20:57:56
合計ジャッジ時間 10,617 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
55,492 KB
testcase_01 AC 134 ms
55,756 KB
testcase_02 AC 144 ms
57,852 KB
testcase_03 AC 1,043 ms
62,956 KB
testcase_04 AC 537 ms
61,364 KB
testcase_05 AC 561 ms
61,556 KB
testcase_06 AC 560 ms
61,752 KB
testcase_07 AC 618 ms
61,656 KB
testcase_08 AC 775 ms
63,104 KB
testcase_09 AC 873 ms
64,764 KB
testcase_10 AC 881 ms
65,324 KB
testcase_11 AC 810 ms
67,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// This file is a "Hello, world!" in Java language by OpenJDK for wandbox.
import java.util.*;
import java.io.*;

class Main
{
    public static void main(String[] args)
    {
		new Main().run();
	}
	
	boolean check(long[] a){
		if(a[0]==a[1]||a[1]==a[2]||a[2]==a[0]||a[0]<=0||a[1]<=0||a[2]<=0)return false;
		return a[1]==Math.min(a[0],Math.min(a[1],a[2])) || a[1]==Math.max(a[0],Math.max(a[1],a[2]));
	}
	
	long solve(long[] a,long[] c){
//		if(a[0]>a[2]){
//			a[0]^=a[2];a[2]^=a[0];a[0]^=a[2];
//		}
		if(check(a))return 0;
		ArrayList<Long> list=new ArrayList<>();
		list.add(0L);
		for(int i=0;i<3;++i){
			for(int d=-2;d<=2;++d){
				if(a[i]+d>0)
					list.add(a[i]+d);
			}
		}
		for(int i=0;i<a.length;++i){
			for(int j=0;j<a.length;++j){
				for(int d=-2;d<=2;++d){
					if(a[i]-a[j]+d>0)
						list.add(a[i]-a[j]+d);
				}
			}
		}
		Collections.sort(list);
		long ret=Long.MAX_VALUE;
		for(int i=0;i<list.size();++i){
			for(int j=0;j<list.size();++j){
				for(int k=0;k<list.size();++k){
				    if(i>0&&j>0&&k>0)break;
					long u=list.get(i),v=list.get(j),w=list.get(k);
					if(check(new long[]{a[0]-u-w,a[1]-u-v,a[2]-v-w})){
						ret=Math.min(ret,c[0]*u+c[1]*v+c[2]*w);
					}
				}
			}
		}
		return ret==Long.MAX_VALUE?-1:ret;
	}
	
	void run(){
		Scanner sc=new Scanner(System.in);
		PrintWriter pw=new PrintWriter(System.out);								
		int T=sc.nextInt();
		if(!(1<=T&&T<=1e4))throw new AssertionError();
		for(int t=0;t<T;++t){
			long[] a=new long[3];
			long[] c=new long[3];
			for(int i=0;i<3;++i){
			    a[i]=sc.nextLong();
			    if(!(1<=a[i]&&a[i]<=1e9)){
			        throw new AssertionError();
			    }
			}
			for(int i=0;i<3;++i){
			    c[i]=sc.nextLong();
		        if(!(1<=c[i]&&c[i]<=1e9))
		        throw new AssertionError();
			}
			
			pw.println(solve(a,c));
		}
		pw.close();
	}
	
	void tr(Object...objects){
		System.out.println(Arrays.deepToString(objects));
	}
}

// OpenJDK reference:
//   http://openjdk.java.net/

// Java language references:
//   http://docs.oracle.com/javase
0