其他分享
首页 > 其他分享> > Too Many Impostors (hard version)

Too Many Impostors (hard version)

作者:互联网

This is an interactive problem. The only difference between the easy and hard version is the limit on number of questions.

There are nn players labelled from 11 to nn. It is guaranteed that nn is a multiple of 33.

Among them, there are kk impostors and n−kn−k crewmates. The number of impostors, kk, is not given to you. It is guaranteed that n3<k<2n3n3<k<2n3.

In each question, you can choose three distinct integers aa, bb, cc (1≤a,b,c≤n1≤a,b,c≤n) and ask: "Among the players labelled aa, bb and cc, are there more impostors or more crewmates?" You will be given the integer 00 if there are more impostors than crewmates, and 11 otherwise.

Find the number of impostors kk and the indices of players that are impostors after asking at most n+6n+6 questions.

The jury is adaptive, which means the indices of impostors may not be fixed beforehand and can depend on your questions. It is guaranteed that there is at least one set of impostors which fulfills the constraints and the answers to your questions at any time.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1001≤t≤100). Description of the test cases follows.

The first and only line of each test case contains a single integer nn (6≤n<1046≤n<104, nn is a multiple of 33) — the number of players.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1042⋅104.

Interaction

For each test case, the interaction starts with reading nn.

Then you are allowed to make at most n+6n+6 questions in the following way:

"? a b c" (1≤a,b,c≤n1≤a,b,c≤n, aa, bb and cc are pairwise distinct).

After each one, you should read an integer rr, which is equal to 00 if there are more impostors than crewmates among players labelled aa, bb and cc, and equal to 11 otherwise.

Answer −1−1 instead of 00 or 11 means that you made an invalid query. Exit immediately after receiving −1−1 and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.

When you have found the indices of all impostors, print a single line "! " (without quotes), followed by the number of impostors kk, followed by kk integers representing the indices of the impostors. Please note that you must print all this information on the same line.

After printing the answer, your program must then continue to solve the remaining test cases, or exit if all test cases have been solved.

After printing the queries and answers do not forget to output end of line and flush the output buffer. Otherwise, you will get the Idleness limit exceeded verdict. To do flush use:

Hacks

You cannot make hacks in this problem.

Example

input

Copy

2
6

0

1

9

1

output

Copy

? 1 2 3

? 3 4 5

! 3 4 1 2

? 7 1 9

! 4 2 3 6 8

Note

Explanation for example interaction (note that this example only exists to demonstrate the interaction procedure and does not provide any hint for the solution):

For the first test case:

Question "? 1 2 3" returns 00, so there are more impostors than crewmates among players 11, 22 and 33.

Question "? 3 4 5" returns 11, so there are more crewmates than impostors among players 33, 44 and 55.

Outputting "! 3 4 1 2" means that one has found all the impostors, by some miracle. There are k=3k=3 impostors. The players who are impostors are players 44, 11 and 22.

For the second test case:

Question "? 7 1 9" returns 11, so there are more crewmates than impostors among players 77, 11 and 99.

Outputting "! 4 2 3 6 8" means that one has found all the impostors, by some miracle. There are k=4k=4 impostors. The players who are impostors are players 22, 33, 66 and 88.

思路:我们可知即使是3个数问一次,不会是全0与1,一定有1与0,那么,我们对这两组进行拆分,像简单版一样拆分成四组,那么拆分的四组中一定有不同的(假设拆分前为1与0,那么最坏的情况是1,1,1,0因为拆分前第二组就为0)同样就一定能得出一个0与一个1,(0为impostor,1为crewmate)我们发现三个数需要两次询问能够得到每个数的值(前提是知道每组的值)如:询寻(i,i+1,i+2)(假设为1),查(i,i+1,imp),然后再查i或i+2的值,就能得到全部值。因为不能查重复元素,所以需要将与刚开始知道的imp与crew有关的组合单点查,最后将imp输出即可。(题中要求先输出个数,可以将数组存队列之中,然后再输出)

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

const int N=1e4+10;

int query(int a,int b,int c)
{
    cout<<"? "<<a<<" "<<b<<" "<<c<<endl;
    int r;
    cin>>r;
    return r;
}

void solve()
{
    int n;
    cin>>n;
    int a[N],role[N];
    for(int i=1; i<=n-2; i+=3)
        a[i]=query(i,i+1,i+2);
    for(int i=0; i<=n; i++)
        role[i]=-1;

    int imp,crew;
    for(int i=1; i<=n-5; i+=3)
    {
        if(a[i]!=a[i+3])
        {
            a[i+1]=query(i+1,i+2,i+3);
            a[i+2]=query(i+2,i+3,i+4);
            for(int j=i; j<=i+2; j++)
            {
                if(a[j]!=a[j+1])//有加一
                {
                    if(a[j]==0)
                    {
                        role[j]=0,role[j+3]=1;
                        imp=j,crew=j+3;
                    }
                    else
                    {
                        role[j+3]=0,role[j]=1;
                        imp=j+3,crew=j;
                    }
                }
            }
            break;
        }
    }

    for(int i=1;i<=n-2;i+=3)
    {
        if(i==imp||i+1==imp||i+2==imp||i==crew||i+1==crew||i+2==crew)
        {
            for(int j=i;j<=i+2;j++)
            {
                if(role[j]==-1)
                {
                    role[j]=query(imp,crew,j);
                }
            }
        }
        else if(a[i]==0)
        {
            if(query(i,i+1,crew)==1)
            {
                if(query(i,imp,crew)==0)
                {
                    role[i]=0;
                    role[i+1]=1;
                    role[i+2]=0;
                }
                else
                {
                    role[i]=1;
                    role[i+1]=0;
                    role[i+2]=0;
                }
            }
            else
            {
                role[i]=0;
                role[i+1]=0;
                if(query(imp,crew,i+2)==0)role[i+2]=0;
                else role[i+2]=1;
            }
        }
        else if(a[i]==1)
        {
            if(query(i,i+1,imp)==0)
            {
                if(query(i,imp,crew)==0)
                {
                    role[i]=0;
                    role[i+1]=1;
                    role[i+2]=1;
                }
                else
                {
                    role[i]=1;
                    role[i+1]=0;
                    role[i+2]=1;
                }
            }
            else
            {
                role[i]=1;
                role[i+1]=1;
                if(query(i+2,imp,crew)==0) role[i+2]=0;
                else role[i+2]=1;
            }
        }
    }

    cout<<"! ";
    queue<int>q;
    for(int i=1;i<=n;i++)if(role[i]==0) q.push(i);
    cout<<q.size();
    while(q.size())
    {
        cout<<" "<<q.front();
        q.pop();
    }cout<<endl;
}


int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}

标签:11,there,int,Many,hard,players,impostors,Impostors,test
来源: https://blog.csdn.net/qq_51690312/article/details/122326590