[UOJ #52]【UR #4】元旦激光炮

题目大意:交互题,给你三个有序数组,长度分别为$n\_a,n\_b,n\_c$,都不超过$10^5$。三个函数$get\_a(i),get\_b(i),get\_c(i)$,分别返回$a_i,b_i,c_i$。

现在要你编写一个函数$query\_kth()$,求出三个数组中第$k$大的元素。

题解:每次求$Biglfloordfrac k 3Big floor$的$a,b,c$,把最小的舍去

卡点:下标移动时写错

C++ Code:

#include "kth.h"
#include <cstdio>
#include <algorithm>
int query_kth(int n_a, int n_b, int n_c, int k) {
	int a = 0, b = 0, c = 0, num[10], tot = 0;
	while (k >= 3) {
		int t = k / 3;
		int A = get_a(a + t - 1), B = get_b(b + t - 1), C = get_c(c + t - 1);
		if (A < B) {
			if (A < C) a += t;
			else c += t;
		} else {
			if (B < C) b += t;
			else c += t;
		}
		k -= t;
	}
	for (int i = 1; i <= k; i++) {
		if (a < n_a) num[++tot] = get_a(a++);
		if (b < n_b) num[++tot] = get_b(b++);
		if (c < n_c) num[++tot] = get_c(c++);
	} 
	std::sort(num + 1, num + tot + 1);
	return num[k];
	return 0;
}

  

原文地址:https://www.cnblogs.com/Memory-of-winter/p/9804505.html