D. Take a Guess-Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2) - 交互 位运算 排序

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:

  • What is the result of a bitwise AND of two items with indices ii and jj (i≠ji≠j)
  • What is the result of a bitwise OR of two items with indices ii and jj (i≠ji≠j)

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:

  • "or i j" (1≤i,j≤n,i≠j)(1≤i,j≤n,i≠j), where ii and jj are indices of items for which you want to calculate the bitwise OR.
  • "and i j" (1≤i,j≤n,i≠j)(1≤i,j≤n,i≠j), where ii and jj are indices of items for which you want to calculate the bitwise AND.
  • "finish res", where resres is the kkth smallest number in the sequence. After outputting this line the program execution must conclude.

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:

  • fflush(stdout) in C++
  • System.out.flush() in Java
  • stdout.flush() in Python
  • flush(output) in Pascal
  • for other languages refer to documentation

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 52a2=6a2=6, a5=3a5=3. Interactor returns bitwise AND of the given numbers.
or 5 67a5=3a5=3, a6=5a6=5. Interactor returns bitwise OR of the given numbers.
finish 555 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<bits/stdc++.h>

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<y.val;
}

void ask1(int x,int y)
{
    cout<<"and "<<x<<" "<<y<<endl;
}

void ask2(int x,int y)
{
    cout<<"or "<<x<<" "<<y<<endl;
}
int main ()
{


    int n,k;
    cin>>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 "<<s[k].val-(temp/2)<<endl;


    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦三码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值