結果

問題 No.90 品物の並び替え
ユーザー iicafiaxusiicafiaxus
提出日時 2019-05-14 21:44:48
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 148 ms / 5,000 ms
コード長 1,687 bytes
コンパイル時間 2,031 ms
コンパイル使用メモリ 167,316 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-04 01:06:10
合計ジャッジ時間 3,338 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 12 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 14 ms
4,376 KB
testcase_06 AC 9 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 148 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.conv, std.string;
import std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }

int DEBUG_LEVEL = 0;
void print()(){ writeln(""); }
void print(T, A ...)(T t, lazy A a){ write(t), print(a); }
void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }

void main(string[] args){
	if(args.length > 1 && args[1] == "-debug"){
		if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;
		else DEBUG_LEVEL = 1;
	}
	
	long n = read.to!long;
	long m = read.to!long;
	
	X[] xs;
	foreach(i; 0 .. m){
		long from = read.to!long;
		long to = read.to!long;
		long value = read.to!long;
		xs ~= new X(from, to, value);
	}
	
	long bestscore = -1;
	long[] as;
	foreach(i; 0 .. n) as ~= i;
	while(1){
		long score;
		foreach(x; xs) if(as[x.from] < as[x.to]) score += x.value;
		if(score > bestscore) bestscore = score;
		
		// next permutation 順列 next_permutation
		long i0 = -1;
		foreach(i; 0 .. as.length - 1) if(as[i] < as[i + 1]) i0 = i;
		if(i0 < 0) break;
		
		long i1 = -1;
		foreach(i; i0 + 1 .. as.length) if(as[i0] < as[i]) i1 = i;
		
		long z;
		z = as[i1], as[i1] = as[i0], as[i0] = z;
		
		foreach(k; 0 .. (as.length - i0 - 1) / 2){
			z = as[i0 + 1 + k], as[i0 + 1 + k] = as[$ - 1 - k], as[$ - 1 - k] = z;
		}
		
		print!1("i0:", i0, " i1:", i1, " as:", as);
		
	}
	
	bestscore.writeln;
	
}

class X{
	long from, to;
	long value;
	this(long from, long to, long value){
		this.from = from, this.to = to;
		this.value = value;
	}
}
0