結果

問題 No.90 品物の並び替え
ユーザー bal4ubal4u
提出日時 2019-05-07 19:32:15
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 1,465 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 29,792 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 16:27:29
合計ジャッジ時間 980 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 15 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:7:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    7 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:14:24: 備考: in expansion of macro ‘gc’
   14 |         int n = 0, c = gc();
      |                        ^~
main.c: 関数 ‘out’ 内:
main.c:8:15: 警告: 関数 ‘putchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    8 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:23:17: 備考: in expansion of macro ‘pc’
   23 |         if (!n) pc('0');
      |                 ^~

ソースコード

diff #

// yukicoder: No.90 品物の並び替え
// 2019.5.7 bal4u

#include <stdio.h>

#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif
int in() {   // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void out(int n) {  // 非負整数の表示(出力)
	int i;
	char b[20];

	if (!n) pc('0');
	else {
		i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

int tbl[10][10];

int K;		// 順列の大きさ、あるいは、文字数
int ans;

// 順列が渡される
/*
 1 2 3 4 5 6 7 8 9
 9 2 3 4 5 6 7 8 1
*/
void showperm(char *p)
{
	int i, j, s = 0;
	for (i = 0; i < K; i++) {
		for (j = i+1; j < K; j++) s += tbl[p[i]][p[j]];
	}
	if (s > ans) ans = s;
}

// Kの数{ 1,2,3,4,5,6,7,8,9 } の順列を生成する K!個
void genperm()
{
	int k, t;
	char c[12], *pc, *q;
	static char p[12] = { 1,1,1,1,1,1,1,1,1,1 };

	q = p, pc = c;
	for (k = 1; k <= K; ) *q++ = *pc++ = k++;  // 1 2 3 のように1から
	k = 1, pc = c;
	do {
		t = *(p + k);
		*(p + k) = *(q = p + ((k & 1) ? *pc : 0));
		*q = t;
		showperm(p);
		k = 1, pc = c;
		while (*pc == 0) *pc++ = k++;
		(*pc)--;
	} while (k < K);
}

int main()
{
	int i, M, a, b, s;
	
	K = in(), M = in();
	while (M--) {
		a = in()+1, b = in()+1, s = in();
		if (tbl[a][b] < s) tbl[a][b] = s;
	}
	genperm();
	out(ans);
	return 0;
}
0