結果

問題 No.135 とりあえず1次元の問題
ユーザー kachipankachipan
提出日時 2023-07-03 09:51:49
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 2,867 bytes
コンパイル時間 880 ms
コンパイル使用メモリ 30,528 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-24 06:47:24
合計ジャッジ時間 2,717 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 WA -
testcase_05 AC 1 ms
4,380 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 12 ms
4,380 KB
evil01.txt AC 12 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘main’ 内:
main.c:23:9: 警告: 関数 ‘QuickSort’ の暗黙的な宣言です [-Wimplicit-function-declaration]
   23 |         QuickSort(input, 0, num - 1);
      |         ^~~~~~~~~
main.c: トップレベル:
main.c:105:6: 警告: conflicting types for ‘QuickSort’; have ‘void(int *, int,  int)’
  105 | void QuickSort(int arr[], int start, int last)
      |      ^~~~~~~~~
main.c:23:9: 備考: previous implicit declaration of ‘QuickSort’ with type ‘void(int *, int,  int)’
   23 |         QuickSort(input, 0, num - 1);
      |         ^~~~~~~~~

ソースコード

diff #

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

void BucketSort(int arr[], int num);

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 (k >= i)
		{
			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