結果

問題 No.90 品物の並び替え
ユーザー TLwiegehttTLwiegehtt
提出日時 2015-07-13 19:15:10
言語 C90
(gcc 11.4.0)
結果
AC  
実行時間 335 ms / 5,000 ms
コード長 1,347 bytes
コンパイル時間 380 ms
コンパイル使用メモリ 26,208 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 15:26:53
合計ジャッジ時間 1,460 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <stdio.h>

/* n個の中からr個取り出す順列の中で、
/* 与えられた並びpの次に大きい並べ替えを返す */
int next_perm(int *p, int n){
  int i, j, k, tmp;
  /* 上位桁のほうが下位桁よりも小さいところまで移動 */
  for(i = n - 1; i > 0 && p[i-1] >= p[i]; i--);
  /* pが最大(次の並べ替えがない) */
  if(i == 0) return 0;
  /* p[i-1]より値の大きい最も下位の桁をp[j]とする */
  for(j = n - 1; j > i && p[i-1] >= p[j]; j--);
  /* p[i-1]とp[j]とを交換 */
  tmp = p[i-1], p[i-1] = p[j], p[j] = tmp;
  /* p[i]から最下位までを逆順 */
  for(k = 0; k <= ((n-1)-i)/2; k++)
    tmp = p[i+k], p[i+k] = p[(n-1)-k], p[(n-1)-k] = tmp;
  return 1;
}

int main(void)
{
	int maxSum = 0;
	int p[15];
	int n,m;
	int score[90];
	int i,j;
	
	scanf("%d %d", &n, &m);
	for(i=0;i<n;i++){p[i] = i;}
	for(i=0;i<m;i++){
		int a,b,c;
		scanf("%d %d %d", &a, &b, &c);
		c = c*100+a*10+b;
		score[i] = c;
	}
	
	do {
		int sum=0;
		for(i=0;i<m;i++){
			int s=0,t=0;
			int a,b,c;
			a = (score[i]/10)%10;
			b = score[i]%10;
			c = score[i]/100;
			
			for(j=0;j<n;j++){
				if( p[j] == a ){s=j;}
				if( p[j] == b ){t=j;}
			}
			
			if(s<t){
				sum += c;
			}
		}
		
		if(maxSum < sum){
			maxSum = sum;
		}
	} while(next_perm(p, n));
	
	
	printf("%d\n", maxSum);
	return 0;
}
0