Vjudge题解:BZOJ - 4320 : ShangHai2006 Homework
Summary of the question meaning
1: Add a new programmer with the code \(X\) to the character set \(S\), ensuring that \(X\) does not exist in the current set.
2: Inquire with the programmer about the minimum value of \(\bmod Y\) in the current character set.
Thought analysis
Firstly, let \(M\) be the maximum value of \(X, Y\).
-
When \(Y \leq \sqrt{M}\), the remainder of \(\bmod Y\) is calculated for each input number, and the minimum value is taken. When we are asked to get the answer, the result is obtained directly. \(N \times \sqrt{M} \leq 10^{5} \times \sqrt{3 \times 10^{5}} = 5.48 \times 10^{7}\), which can be processed in
1000ms. -
When \(Y \geq \sqrt{M}\), store the input number, and when we are asked to get the answer, skip \(Y\) from \(0\) to \(M\) in the \(X\) sequence for at most \(\sqrt {M}\) times. $N \times \sqrt{M} \leq 10^{5} \times \sqrt{3 \times 10 ^ {5}} = 5.48 \times 10 ^ {7} $, which can also be processed in
1000ms.
So consider \(\Large \textcolor{red}{square \ root \ divide \ and \ conquer}\).
-
Every time \(X\) is inputted, \(1 \leq Y \leq \sqrt{M}\) is preprocessed and \(X\) is stored in a
set. -
Each time a query is made, the result of \(1 \leq Y \leq \sqrt{M}\) have been preprocessed and we can output it directly. In the case of \(Y \geq \sqrt{M}\), make a jump on the
setand calculate the answer.
题意简述
1:在人物集合 \(S\) 中加入一个新的程序员,其代号为 \(X\),保证 \(X\) 在当前集合中不存在。
2:在当前的人物集合中询问程序员的 \(\bmod Y\) 最小的值。
思路分析
首先设 \(M\) 为 \(X, Y\) 的最大值。
-
当 \(Y \leq \sqrt{M}\) 时,对每个输入的数算 \(\bmod Y\) 的余数,并取最小值,求答案时直接得到结果,\(N \times \sqrt{M} \leq 10^{5} \times \sqrt{3 \times 10^{5}} = 5.48 \times 10^{7}\),可以处理。
-
当 \(Y \geq \sqrt{M}\) 时,存下输入的数,求答案时在 \(X\) 序列中从 \(0\) 开始到 \(M\) 每次跳跃 \(Y\) 的长度,最多跳 \(\sqrt{M}\) 次,\(N \times \sqrt{M} \leq 10^{5} \times \sqrt{3 \times 10^{5}} = 5.48 \times 10^{7}\),也可以处理。
所以考虑 \(\Large \textcolor{red}{根号分治}\)。
-
每次输入一个 \(X\) 时,对 \(1 \leq Y \leq \sqrt{M}\) 进行预处理,同时将 \(X\) 存入一个
set。 -
每次查询时,\(1 \leq Y \leq \sqrt{M}\) 的结果已经预处理,直接输出;\(Y \geq \sqrt{M}\) 的情况就在
set上做跳跃,算出答案。
\(Code\)
| ID | Time | Memory | Length | Language | When |
|---|---|---|---|---|---|
| [submission:55639306] | 511ms | 1536kB | 791 | C++ 11 O2 | 2024-11-02 20:08:48 |
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <set>
using namespace std;
const int NR = 3e5 + 10;
const int SR = sqrt(NR);
int pans[SR + 10];
set<int> s;
int main()
{
memset(pans, 0x3f, sizeof(pans));
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i ++)
{
char op;
int x;
scanf(" %c%d", &op, &x);
if (op == 'A')
{
s.insert(x);
for (int j = 1; j <= SR; j ++)
{
pans[j] = min(pans[j], x % j);
}
}
else
{
if (x <= SR)
{
printf("%d\n", pans[x]);
}
else
{
int sans = 1e9;
for (int j = 0; j <= NR; j += x)
{
auto it = s.lower_bound(j);
if (it != s.end())
{
sans = min(sans, *it - j);
}
}
printf("%d\n", sans);
}
}
}
return 0;
}

浙公网安备 33010602011771号