提交记录 22624


用户 题目 状态 得分 用时 内存 语言 代码长度
TSKY 1003. 测测你的二分查找 Accepted 100 68.88 us 12 KB C++14 1.17 KB
提交时间 评测时间
2024-10-19 19:36:10 2024-10-19 19:36:13
constexpr int BOLCKS = 8;
unsigned table[BOLCKS];

void init(const unsigned *a, int n)
{
	static bool is_init = false;
	if (!is_init)
	{
		is_init = true;
		const int block_len = n / BOLCKS;
		for (int i = 0; i < BOLCKS; i += 4)
		{
			table[i] = a[block_len * i];
			table[i + 1] = a[block_len * (i + 1)];
			table[i + 2] = a[block_len * (i + 2)];
			table[i + 3] = a[block_len * (i + 3)];
		}
	}
}

int search_block(const unsigned x)
{
	int l = -1, r = BOLCKS, m = (l + r) / 2;
	while (m > l)
	{
		if (table[m] > x)
		{
			r = m;
		}
		else
		{
			l = m;
		}
		m = (l + r) / 2;
	}
	return l;
}

int binary_search(const unsigned *a, int n, const unsigned x)
{
	init(a, n);
	const int block_len = n / BOLCKS;
	unsigned long long l = search_block(x), r = l + 1;
	l *= block_len;
	if (r == BOLCKS)
	{
		r = n - 1;
	}
	else
	{
		r = r * block_len - 1;
	}
	while (l < r)
	{
		unsigned long long l_num = a[l];
		unsigned long long r_num = a[r];
		unsigned long long mid = (r * (x - l_num) + l * (r_num - x)) / (r_num - l_num);
		unsigned long long mid_num = a[mid];
		if (mid_num == x)
		{
			return mid;
		}
		else if (mid_num < x)
		{
			l = mid + 1;
		}
		else
		{
			r = mid - 1;
		}
	}
	return l;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #168.88 us12 KBAcceptedScore: 100


Judge Duck Online | 评测鸭在线
Server Time: 2024-11-24 02:57:46 | Loaded in 0 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠