#P8487. 「HGOI-1」Binary search Ex
「HGOI-1」Binary search Ex
Background
This problem is an extra subtask of div.2 B. It is not a full problem. The total score is points (after entering the main problem set, the full score is points).
is learning binary search.
Problem Description
As everyone knows, when computing in binary search, you can take either or . So the indecisive student added randomness into his binary search code: each time, he randomly chooses one of them as .
Now tells you the index (starting from ) of the number he wants to find in the sequence (you can think of it as querying a number in an increasing sequence of indices ). He wants to know, in the best possible luck, how many times the loop needs to run (that is, the minimum possible final value of in the code).
Loop:
int find(int *num,int x,int len)
{
int l=0,r=len-1,mid,cnt=0,w;
while(l<r)
{
cnt++;
w=rand()%2;
mid=(l+r+w)/2;
if(num[mid]-w<x) l=mid+!w;
else r=mid-w;
}
return mid;
}
Recursion:
int cnt;
int get(int *num,int x,int l,int r)
{
if(l==r) return l;
cnt++;
int w=rand()%2;
int mid=(l+r+w)/2;
if(num[mid]-w<x) return get(num,x,mid+!w,r);
else return get(num,x,l,mid-w);
}
int find(int *num,int x,int len)
{
cnt=0;
return get(num,x,0,len-1);
}
Note: the two codes above are completely equivalent.
Input Format
The first line gives an integer indicating the length of the sequence.
The second line gives two integers , , indicating the number of queries, where is the number of queries given in the input, and is the number of queries generated by the data generator.
The next line contains integers, indicating the numbers to query.
Then the data generator provides queries (no need to read them).
Output Format
Among the total of queries, let the answer to the -th query be .
Output an integer as the result.
10
3 0
2 6 8
18
13
5 0
0 1 4 6 11
52
1928374
10 1000000
193 3489 238 438 8 912 83 19 12489 10
10000215403302
Hint
Explanation of Sample 1
The restored output is: .
Find :
Take .
Take .
Take (exit the loop).
Explanation of Sample 2
The restored output is: .
Data Generator
#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
ull sd = 111111111111111111ull, sd2, k = 1;
ull qu, n, ans;//qu表示每次询问的位置。
inline ull get_q(int i)
{
sd = (sd2 ^ (sd2 >> 3)) + 998244353;
return ((sd2 = sd ^ (sd << 37)) & k) + ((i & 1) ? 0 : (n - k - 1));
}
int q, q2;
void init()
{
//Put your code here or not.
}
inline ull get_ans(ull x)
{
//Put your code here.
}
int main()
{
cin >> n;
sd2 = n;
while((k << 1) <= n + 1) k <<= 1;
k -= 1;
cin >> q >> q2;
init();
for(int i = 1; i <= q; i++)
{
cin >> qu;
ans += get_ans(qu) * i;
}
for(int i = 1; i <= q2; i++)
{
qu = get_q(i);
ans += get_ans(qu) * (i + q);
}
cout << ans << endl;
return 0;
}
Constraints and Notes
This problem does not use bundled test. The score is the sum of the scores of all subtasks. The testdata has multiple levels, as shown in the table below.
$$\def\arraystretch{1.5} \begin{array}{|c|c|c|}\hline \textbf{ExTest} & \textbf{Score} & \textbf{Special Limits} \cr\hline 1 & 5 & n,q_2 \le 2^{20}\cr\hline 2 & 5 & n \le 2^{30},q_2 \le 2\times 10^6 \cr\hline 3 & 5 & n \le 2^{40},q_2 \le 5 \times 10^6 \cr\hline 4 & 5 & n \le 2^{50},q_2 \le 2\times 10^7 \cr\hline 5 & 5 & n \le 2^{60},q_2 \le 2\times 10^8 \cr\hline \end{array}$$For of the data, , , , and .
This problem guarantees that the time limit is more than twice the standard one, and using the provided template can pass this problem.
Translated by ChatGPT 5