結果

問題 No.168 ものさし
ユーザー asugen0402asugen0402
提出日時 2019-01-31 10:56:44
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 5,544 bytes
コンパイル時間 1,334 ms
コンパイル使用メモリ 34,816 KB
実行使用メモリ 13,060 KB
最終ジャッジ日時 2024-04-25 21:33:48
合計ジャッジ時間 1,473 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 8 ms
6,944 KB
testcase_12 AC 8 ms
6,944 KB
testcase_13 AC 19 ms
11,776 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 AC 1 ms
6,940 KB
testcase_19 AC 9 ms
6,940 KB
testcase_20 AC 6 ms
6,940 KB
testcase_21 AC 21 ms
13,060 KB
testcase_22 AC 9 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 内部定数
#define D_ON			1										// 汎用フラグ - ON
#define D_OFF			0										// 汎用フラグ - OFF
#define D_POINT_MAX		1000									// 最大点数
#define D_HEAP_MAX		D_POINT_MAX * D_POINT_MAX				// 最大ヒープ数

// 内部構造体 - 点情報
typedef struct Point {
	int miX, miY;												// 座標
	int miDone;													// 処理済フラグ
} Point;

// 内部構造体 - ヒープ情報
typedef struct Heap {
	long long mlLen2;											// 距離2乗
	int miPNo;													// 点
} Heap;

// 内部変数
static FILE *szpFpI;											// 入力
static Point sz1Point[D_POINT_MAX];								// 点
static int siPCnt;												// 点数
static Heap sz1Heap[D_HEAP_MAX];								// ヒープ
static int siHCnt;												// ヒープ数
static long long slMax;											// 最大距離2乗

// 内部変数 - テスト用
#ifdef D_TEST
	static int siRes;
	static FILE *szpFpA;
#endif

// ヒープ - 比較 - 距離昇順
int
fHeapCmp(
	int piNo1					// <I> 配列番号1 0~
	, int piNo2					// <I> 配列番号2 0~
)
{
	// 距離昇順
	if (sz1Heap[piNo1].mlLen2 < sz1Heap[piNo2].mlLen2) {
		return -1;
	}
	else if (sz1Heap[piNo1].mlLen2 > sz1Heap[piNo2].mlLen2) {
		return 1;
	}

	return 0;
}

// ヒープ - 親子関係チェック
// 戻り値:[>=0]:変更した子の配列番号 [-1]:変更なし
int
fHeapChk(
	int piPNo					// <I> 親の配列番号 0~
)
{
	int liRet;

	// 最小値
	int liMNo = piPNo;

	// 左の子と比較
	int liCNo = piPNo * 2 + 1;
	if (liCNo < siHCnt) {
		liRet = fHeapCmp(liMNo, liCNo);
		if (liRet == 1) {
			liMNo = liCNo;
		}
	}

	// 右の子と比較
	liCNo = piPNo * 2 + 2;
	if (liCNo < siHCnt) {
		liRet = fHeapCmp(liMNo, liCNo);
		if (liRet == 1) {
			liMNo = liCNo;
		}
	}

	// 自分が最小値であるかチェック
	if (piPNo == liMNo) {
		return -1;
	}

	// 値の交換
	Heap lzWork;
	memcpy(&lzWork, &sz1Heap[liMNo], sizeof(Heap));
	memcpy(&sz1Heap[liMNo], &sz1Heap[piPNo], sizeof(Heap));
	memcpy(&sz1Heap[piPNo], &lzWork, sizeof(Heap));

	return liMNo;
}

// ヒープ - キュー追加
int
fHeapEnqueue(
	long long plLen2			// <I> 距離2乗
	, int piPNo					// <I> 点
)
{
	int liRet;

	// 末尾に追加
	sz1Heap[siHCnt].mlLen2 = plLen2;
	sz1Heap[siHCnt].miPNo = piPNo;
	siHCnt++;

	// 親子関係チェック
	int liNo = siHCnt - 1;
	while (1) {

		// 親の配列番号
		liNo = (liNo - 1) / 2;

		// 親子関係チェック
		liRet = fHeapChk(liNo);
		if (liRet < 0) {
			break;
		}
	}

	return 0;
}

// ヒープ - キュー取得
int
fHeapDequeue(
	Heap *pzpRet				// <O> 取得先
)
{
	// データ数
	if (siHCnt < 1) {
		return -1;
	}

	// 取得
	memcpy(pzpRet, &sz1Heap[0], sizeof(Heap));
	siHCnt--;

	// データ数
	if (siHCnt < 1) {
		return 0;
	}

	// 末尾を先頭へ
	memcpy(&sz1Heap[0], &sz1Heap[siHCnt], sizeof(Heap));

	// 親子関係チェック
	int liNo = 0;
	while (liNo >= 0) {
		liNo = fHeapChk(liNo);
	}

	return 0;
}

// 実行メイン
int
fMain(
	int piTNo					// <I> テスト番号 1~
)
{
	int i;
	char lc1Buf[1024], lc1Out[1024];

	// データ - 初期化
	memset(sz1Point, 0, sizeof(sz1Point));						// 点
	siHCnt = 0;													// ヒープ数
	slMax = 0;													// 最大距離2乗

	// 入力 - セット
#ifdef D_TEST
	sprintf(lc1Buf, ".\\Test\\T%d.txt", piTNo);
	szpFpI = fopen(lc1Buf, "r");
	sprintf(lc1Buf, ".\\Test\\A%d.txt", piTNo);
	szpFpA = fopen(lc1Buf, "r");
	siRes = 0;
#else
	szpFpI = stdin;
#endif

	// 点数 - 取得
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%d", &siPCnt);

	// 点 - 取得
	for (i = 0; i < siPCnt; i++) {
		fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
		sscanf(lc1Buf, "%d%d", &sz1Point[i].miX, &sz1Point[i].miY);
	}

	// 開始位置 - セット
	fHeapEnqueue(0, 0);

	// 移動
	while (1) {

		// 移動先 - 取得
		Heap lzHeap;
		fHeapDequeue(&lzHeap);

		// 処理済フラグ
		if (sz1Point[lzHeap.miPNo].miDone != D_OFF) {
			continue;
		}
		sz1Point[lzHeap.miPNo].miDone = D_ON;

		// 最大距離2乗 - 更新
		if (slMax < lzHeap.mlLen2) {
			slMax = lzHeap.mlLen2;
		}

		// 到着チェック
		if (lzHeap.miPNo == siPCnt - 1) {
			break;
		}

		// 次の移動先 - セット
		for (i = 0; i < siPCnt; i++) {
			int liX = sz1Point[lzHeap.miPNo].miX - sz1Point[i].miX;
			int liY = sz1Point[lzHeap.miPNo].miY - sz1Point[i].miY;
			long long llLen2 = (long long)liX * (long long)liX + (long long)liY * (long long)liY;
			fHeapEnqueue(llLen2, i);
		}
	}

	// 長さ
	int liLen = (int)sqrt((double)slMax) / 10 * 10;
	while (1) {
		long long llLen2 = (long long)liLen * (long long)liLen;
		if (llLen2 >= slMax) {
			break;
		}
		liLen += 10;
	}

	// 結果 - セット
	sprintf(lc1Out, "%d\n", liLen);

	// 結果 - 表示
#ifdef D_TEST
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, lc1Out)) {
		siRes = -1;
	}
#else
	printf("%s", lc1Out);
#endif

	// 残データ有無
#ifdef D_TEST
	lc1Buf[0] = '\0';
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, "")) {
		siRes = -1;
	}
#endif

	// テストファイルクローズ
#ifdef D_TEST
	fclose(szpFpI);
	fclose(szpFpA);
#endif

	// テスト結果
#ifdef D_TEST
	if (siRes == 0) {
		printf("OK %d\n", piTNo);
	}
	else {
		printf("NG %d\n", piTNo);
	}
#endif

	return 0;
}

int
main()
{

#ifdef D_TEST
	int i;
	for (i = D_TEST_SNO; i <= D_TEST_ENO; i++) {
		fMain(i);
	}
#else
	fMain(0);
#endif

	return 0;
}

0