結果

問題 No.135 とりあえず1次元の問題
ユーザー kachipankachipan
提出日時 2023-07-03 09:58:10
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 21 ms / 5,000 ms
コード長 2,915 bytes
コンパイル時間 1,588 ms
コンパイル使用メモリ 30,264 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-24 06:53:34
合計ジャッジ時間 1,631 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,384 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 14 ms
4,380 KB
testcase_22 AC 21 ms
4,380 KB
evil01.txt AC 20 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>

void BucketSort(int arr[], int num);
void QuickSort(int att[], int start, int last);

int main()
{
	int num = 0;
	int input[100000] = { 0 };
	int point[2] = { 0 };
	int distance = 0;

	scanf("%d", &num);

	for (int i = 0;i < num;i++)
	{
		scanf("%d", &input[i]);
	}

	QuickSort(input, 0, num - 1);

	// 最初の座標だけあらかじめ格納しておく
	point[0] = input[0];

	// 比較
	for (int i = 1;i < num;i++)
	{
		int tmp = 0;
		point[1] = input[i];

		tmp = point[1] - point[0];

		// 距離が今まで比べて一番小さいか判断
		if (distance == 0 || (tmp > 0 && tmp < distance))
		{
			distance = tmp;
		}

		// 次の距離も確認できるように格納しておく
		point[0] = point[1];
	}

	printf("%d\n", distance);

	return 0;
}

/// <summary>
/// バケットソート。重複なし
/// </summary>
/// <param name="arr"></param>
/// <param name="num"></param>
void BucketSort(int arr[], int num)
{
	int max = 0;	// arrの中の最大の値
	int* buckets = NULL;	// バケツ
	int size = 0;
	int j = 0;

	// 配列の中の最大の大きさの物を求める
	for (int i = 0;i < num;i++)
	{
		if (arr[i] > max)
		{
			max = arr[i];
		}
	}

	// maxのままではarr配列の中の最大の大きさの数値を格納できないため+1する
	size = max + 1;

	// 最大の大きさ分メモリを確保。
	buckets = (int*)calloc(size, sizeof(int));

	// バケツの中身を-1で初期化
	for (int i = 0;i < size;i++)
	{
		buckets[i] = -1;
	}

	for (int i = 0;i < num;i++)
	{
		buckets[arr[i]] = arr[i];
	}

	for (int i = 0;i < size;i++)
	{
		if (buckets[i] != -1)
		{
			arr[j] = buckets[i];
			j++;
		}
	}

	free(buckets);
}

/// <summary>
/// 
/// </summary>
/// <param name="arr"></param>
void QuickSort(int arr[], int start, int last)
{
	int i = start;
	int k = last;
	int pivot = arr[start];


	if (start >= last)
	{
		return;
	}

	while (true)
	{
		// pivotより値が大きくなるまで要素番号が小さい順に配列を探索
		while (arr[i] < pivot)
		{
			i++;
		}

		// pivotより値が小さくなるまで要素番号が大きい順に配列を探索
		while (arr[k] > pivot)
		{
			k--;
		}

		// iがk以上になったら、基準の数値より小さい側と大きい側にわけられている
		if (i >= k)
		{
			break;
		}

		// ここまで来てたらarr[i]は基準値より大きく、arr[k]は基準値より小さい
		// ので値を交換する
		int tmp = arr[i];
		arr[i] = arr[k];
		arr[k] = tmp;

		// 探索を再開
		i++;
		k--;
	}

	// 基準値より小さい範囲に対して同じ処理を行う
	// arr[i]は基準値なので-1して渡す。
	QuickSort(arr, start, i - 1);

	// 基準値より大きい範囲に対して同じ処理を行う
	// arr[k]は基準値なので+1して渡す。
	QuickSort(arr, k + 1, last);
}
0