結果

問題 No.135 とりあえず1次元の問題
ユーザー kachipankachipan
提出日時 2023-06-30 16:14:36
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 1,578 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 30,428 KB
実行使用メモリ 5,828 KB
最終ジャッジ日時 2023-09-21 09:50:06
合計ジャッジ時間 1,350 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
4,376 KB
testcase_01 AC 1 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 1 ms
4,376 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 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 4 ms
5,700 KB
testcase_13 AC 4 ms
5,748 KB
testcase_14 AC 3 ms
5,756 KB
testcase_15 AC 4 ms
5,728 KB
testcase_16 AC 4 ms
5,828 KB
testcase_17 AC 4 ms
5,764 KB
testcase_18 AC 3 ms
5,728 KB
testcase_19 AC 4 ms
5,664 KB
testcase_20 AC 3 ms
5,748 KB
testcase_21 AC 16 ms
5,632 KB
testcase_22 AC 17 ms
5,828 KB
evil01.txt AC 16 ms
5,636 KB
権限があれば一括ダウンロードができます

ソースコード

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]);
	}

	BucketSort(input, num);

	// 最初の座標だけあらかじめ格納しておく
	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;
}

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);
}
0