結果

問題 No.90 品物の並び替え
ユーザー tsunabittsunabit
提出日時 2020-02-04 22:34:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,128 ms / 5,000 ms
コード長 1,645 bytes
コンパイル時間 5,688 ms
コンパイル使用メモリ 79,436 KB
実行使用メモリ 67,316 KB
最終ジャッジ日時 2023-10-21 06:38:07
合計ジャッジ時間 9,403 ms
ジャッジサーバーID
(参考情報)
judge9 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 176 ms
58,344 KB
testcase_01 AC 446 ms
66,592 KB
testcase_02 AC 168 ms
58,408 KB
testcase_03 AC 266 ms
62,564 KB
testcase_04 AC 268 ms
64,040 KB
testcase_05 AC 477 ms
67,288 KB
testcase_06 AC 454 ms
67,316 KB
testcase_07 AC 227 ms
56,524 KB
testcase_08 AC 172 ms
58,332 KB
testcase_09 AC 1,128 ms
66,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;
import java.math.*;

public class No90 {
//	public static int[] ary;
	public static String[] ary;
	public static int MAX = 0;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int y = 3 * m;
//		int[] tmp = new int[y];
		
		ary = new String[y];
		for(int i = 0; i < y; i++) {
//			tmp[i] = sc.nextInt();
//			ary[i] = sc.nextInt();
			ary[i] = sc.next();
		}
		
		
		// 商品の文字列作成
		String str = "";
		for(int i = 0; i < n; i++) {
			str = str + i;
		}
//		System.out.println("str = " + str);
		
		permutation(str, "");
		
		System.out.println(MAX);
		
//		for(int i = 0; i < y; i=i+3) {
//			System.out.print(ary[i] + " " + ary[i+1] + " " + ary[i+2]);
//			System.out.println("");
//		}
		
	}
	public static void permutation(String q, String ans){
		// Base Case
		if(q.length() <= 1) {
			// System.out.println(ans + q);
			String str = ans + q;
//			System.out.println("str = " + str);
			
			int total = 0;
			for(int i = 0; i < ary.length; i=i+3) {
				String it1 = ary[i];
				String it2 = ary[i+1];
				int sc = Integer.parseInt(ary[i+2]);
//				System.out.println(ary[i]   + "のindex = " + str.indexOf(it1));
//				System.out.println(ary[i+1] + "のindex = " + str.indexOf(it2));
				
				if(str.indexOf(it1) < str.indexOf(it2)) {
					total += sc;
				}
			}
//			System.out.println("total = " + total);
			MAX = MAX < total? total : MAX;
 		}
		// General Case
		else {
			for (int i = 0; i < q.length(); i++) {
				permutation(q.substring(0, i) + q.substring(i + 1), ans + q.charAt(i));
			}
		}
	}
}
0