結果

問題 No.837 Noelちゃんと星々2
ユーザー asugen0402asugen0402
提出日時 2019-06-18 11:31:38
言語 C
(gcc 12.3.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 4,261 bytes
コンパイル時間 342 ms
コンパイル使用メモリ 31,060 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-29 23:42:34
合計ジャッジ時間 9,404 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 769 ms
4,376 KB
testcase_01 AC 768 ms
4,380 KB
testcase_02 WA -
testcase_03 AC 768 ms
4,380 KB
testcase_04 AC 770 ms
4,376 KB
testcase_05 AC 770 ms
4,376 KB
testcase_06 AC 769 ms
4,380 KB
testcase_07 AC 769 ms
4,376 KB
testcase_08 AC 769 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 0 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 0 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 0 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 0 ms
4,376 KB
testcase_24 AC 0 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 0 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 0 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 0 ms
4,380 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_POINT_MAX		100000									// 最大点数

// 内部変数
static FILE *szpFpI;											// 入力
static int si1Point[D_POINT_MAX];								// 点
static int siPCnt;												// 点数

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

// 出力
int
fOut(
	char *pcpLine				// <I> 1行
)
{
	char lc1Buf[1024];

#ifdef D_TEST
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, pcpLine)) {
		siRes = -1;
	}
#else
	printf("%s", pcpLine);
#endif

	return 0;
}

// ソート関数 - int昇順
int
fSortFnc(
	const void *pzpVal1			// <I> 値1
	, const void *pzpVal2		// <I> 値2
)
{
	int *lipVal1 = (int *)pzpVal1;
	int *lipVal2 = (int *)pzpVal2;

	// int昇順
	if (*lipVal1 > *lipVal2) {
		return 1;
	}
	else if (*lipVal1 < *lipVal2) {
		return -1;
	}

	return 0;
}

// 合計値 - 取得
long long
fGetSum(
	int piSNo					// <I> 開始位置 0~
	, int piENo					// <I> 終了位置 0~
	, int piH					// <I> 高さ
)
{
	int i;

	long long llSum = 0;
	for (i = piSNo; i <= piENo; i++) {
		llSum += llabs(piH - si1Point[i]);
	}

	return llSum;
}

// 最小値 - 取得
long long
fGetMin(
	int piSNo					// <I> 開始位置 0~
	, int piENo					// <I> 終了位置 0~
)
{
	int i;

	// 最適位置の検索
	int liMin = si1Point[piSNo];
	int liMax = si1Point[piENo];
	while (1) {
		int liRng = (liMax - liMin) / 3;
		if (liRng < 1) {
			break;
		}

		// チェック位置
		int liNo1 = liMin + liRng;
		int liNo2 = liMax - liRng;

		// 合計値 - 取得
		long long llVal1 = fGetSum(piSNo, piENo, liNo1);
		long long llVal2 = fGetSum(piSNo, piENo, liNo2);

		// 最適位置の絞り込み
		if (llVal1 < llVal2) {
			liMax = liNo2;
		}
		else {
			liMin = liNo1;
		}
	}
	long long llMin = fGetSum(piSNo, piENo, liMin);
	for (i = liMin + 1; i <= liMax; i++) {
		long long llVal = fGetSum(piSNo, piENo, i);
		if (llMin > llVal) {
			llMin = llVal;
		}
	}

	return llMin;
}

// 実行メイン
long long
fMain(
)
{
	int i;
	char lc1Buf[1024];

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

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

	// 点 - ソート
	qsort(si1Point, siPCnt, sizeof(int), fSortFnc);
	if (si1Point[0] == si1Point[siPCnt - 1]) {
		return 1;
	}

	// 分割位置の絞り込み
	int liMin = 1;
	int liMax = siPCnt - 1;
	while (1) {
		int liRng = (liMax - liMin) / 3;
		if (liRng < 1) {
			break;
		}

		// チェック位置
		int liNo1 = liMin + liRng;
		int liNo2 = liMax - liRng;

		// 最小値 - 取得
		long long llVal1 = fGetMin(0, liNo1 - 1) + fGetMin(liNo1, siPCnt - 1);
		long long llVal2 = fGetMin(0, liNo2 - 1) + fGetMin(liNo2, siPCnt - 1);

		// 分割位置の絞り込み
		if (llVal1 < llVal2) {
			liMax = liNo2;
		}
		else {
			liMin = liNo1;
		}
	}
	long long llMin = fGetMin(0, liMin - 1) + fGetMin(liMin, siPCnt - 1);
	for (i = liMin + 1; i <= liMax; i++) {
		long long llVal = fGetMin(0, i - 1) + fGetMin(i, siPCnt - 1);
		if (llMin > llVal) {
			llMin = llVal;
		}
	}

	return llMin;
}

// 1回実行
int
fOne(
)
{
	long long llRet;
	char lc1Buf[1024];

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

	// 実行メイン
	llRet = fMain();

	// 出力
	sprintf(lc1Buf, "%lld\n", llRet);
	fOut(lc1Buf);

	// 残データ有無
#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", siTNo);
	}
	else {
		printf("NG %d\n", siTNo);
	}
#endif

	return 0;
}

// プログラム開始
int
main()
{

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

	return 0;
}

0