Problem - D - Codeforces
D. Take a Guess
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
This is an interactive task
William has a certain sequence of integers a1,a2,…,ana1,a2,…,an in his mind, but due to security concerns, he does not want to reveal it to you completely. William is ready to respond to no more than 2⋅n2⋅n of the following questions:
You can ask William these questions and you need to find the kk-th smallest number of the sequence.
Formally the kk-th smallest number is equal to the number at the kk-th place in a 1-indexed array sorted in non-decreasing order. For example in array [5,3,3,10,1][5,3,3,10,1] 44th smallest number is equal to 55, and 22nd and 33rd are 33.
Input
It is guaranteed that for each element in a sequence the condition 0≤ai≤1090≤ai≤109 is satisfied.
Interaction
In the first line you will be given two integers nn and kk (3≤n≤104,1≤k≤n)(3≤n≤104,1≤k≤n), which are the number of items in the sequence aa and the number kk.
After that, you can ask no more than 2⋅n2⋅n questions (not including the "finish" operation).
Each line of your output may be of one of the following types:
In response to the first two types of queries, you will get an integer xx, the result of the operation for the numbers you have selected.
After outputting a line do not forget to output a new line character and flush the output buffer. Otherwise you will get the "Idleness limit exceeded". To flush the buffer use:
If you perform an incorrect query the response will be −1−1. After receiving response −1−1 you must immediately halt your program in order to receive an "Incorrect answer" verdict.
Hacking
To perform a hack you will need to use the following format:
The first line must contain two integers nn and kk (3≤n≤104,1≤k≤n)(3≤n≤104,1≤k≤n), which are the number of items in the sequence aa and the number kk.
The second line must contain nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤109)(0≤ai≤109), the sequence aa.
Example
input
Copy
7 6 2 7
output
Copy
and 2 5 or 5 6 finish 5
Note
In the example, the hidden sequence is [1,6,4,2,3,5,4][1,6,4,2,3,5,4].
Below is the interaction in the example.
Query (contestant's program) | Response (interactor) | Notes |
and 2 5 | 2 | a2=6a2=6, a5=3a5=3. Interactor returns bitwise AND of the given numbers. |
or 5 6 | 7 | a5=3a5=3, a6=5a6=5. Interactor returns bitwise OR of the given numbers. |
finish 5 | 55 is the correct answer. Note that you must find the value and not the index of the kth smallest number. |
--------------------------------------------------------------------------------------------------------------------------------
AND OR运算 合在一起考察时多半考虑其组合性质 x+y =xandy+ xor y
即x+y = 其二进制交集加上并集 ,那么完全可以用 2*(n-1)从询问获得 a1+a2 a1+a3...a1+an
外加2次询问获得 a2+a3 ,间接求出 a1+a1参与排序,得到位置k之后,将其值减去a1即可
# include
using namespace std;
typedef long long int ll;
struct node
{
int id;
ll val;
};
struct node s[10000+10];
bool cmp(struct node x, struct node y)
{
return x.val>n>>k;
for(int i=1;i<=n;i++)
{
s[i].id=i;
s[i].val=0;
}
for(int i=2;i<=n;i++)
{
ask1(1,i);
ll x;
cin>>x;
ask2(1,i);
ll y;
cin>>y;
s[i].val=x+y;
}
ll x=0;
ask1(2,3);
cin>>x;
ask2(2,3);
ll y;
cin>>y;
x+=y;
s[1].val=s[2].val+s[3].val-x;
ll temp=s[1].val;
sort(s+1,s+1+n,cmp);
cout<<"finish "<