1. 程式人生 > >Find an element in hidden array (人生第二道互動題)

Find an element in hidden array (人生第二道互動題)

There is an array of length N consisting of non-negative integers. The array is sorted in non-decreasing order. Each number in the array appears exactly K times, except one element, which appears at least once, but less than K times. Your task is to identify that element.

This is an interactive problem. You are only given the integer N

in the input. Both the array and the value of K are hidden. You are allowed to ask the judge the following queries: What is the value of the element at index i of the array? Identify the value of the element with frequency less than K by asking at most 60 such queries.

Input and Output

The first line of the input contains a single integer T

denoting the number of test cases.

For each test case, you should start by reading a single line containing one integer N from the input.

You can interact with the judge using the standard input and output. There are two types of operations: to perform one operation, you should print to the standard output a line containing two space-separated integers type

and val.

  • If type = 1, you are asking the judge a query for the value of the element of the array at index val. After printing this line, the judge will print to the standard input a line containing one integer corresponding to the value of the element at index val.
  • If type = 2, you are telling the judge that the element with frequency less than K is val. For each test case, you should perform this operation exactly once at the end. This is not counted towards the 60 queries.

Note

Don't forget to flush the standard output after printing each line. It can be done using fflush(stdout) in C/C++, System.out.flush() in Java and sys.out.flush() in Python.

If you ask more than 60 queries, your program will get the verdict Wrong Answer.

Constraints

  • 1 ≤ T ≤ 104
  • 3 ≤ N ≤ 105
  • 2 ≤ K ≤ N - 1
  • each element of the array lies between 1 and 109 inclusive

Example

Input / judge feedback	your output
1
3
						1 2
1
						1 3
5
						1 1
1
						2 5

Explanation

Example case 1: Suppose the array is [1, 1, 5]. Note that the value of K is 2, but it is hidden from you.

In the first query, you request the value of the 2nd element and the judge answers 1. Then you request the value of the 3rd element and the judge answers 5, then the value of the first element and the judge answers 1.

Now, you tell the judge that the answer is 5. You made a total of 3 queries.

程式碼 :

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <iostream>
#include <cmath>

using namespace std;

int T,N,K;

char s[1005];

int Bin(int l,int r,int x){
	int id;
	while(l < r){
		int m = l + (r-l)/2;
		printf("1 %d\n",m);
		fflush(stdout);
		scanf("%d",&id);
		if(id > x)r = m;
		else l = m+1;
	}
	return r;
}

int MyBin(int len){
	int l = 0,r = len-1;
	int id,idd;
	while(l <= r){
		int m = l + (r-l)/2;
		printf("1 %d\n",(m)*K+1);
		fflush(stdout);
		scanf("%d",&id);
		printf("1 %d\n",(m+1)*K);
		fflush(stdout);
		scanf("%d",&idd);
		if(id == idd)l = m+1;
		else r = m-1;
	}
	return r;
}

int main(){
	
	int re;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&N);
		int num = 60;
		int id,idd,iddd;
		printf("1 1\n");
		fflush(stdout);
		scanf("%d",&id);
		K = Bin(1,N,id)-1;
		
		printf("1 %d\n",1+K);
		fflush(stdout);
		scanf("%d",&idd);
		if(K+K > N){
			printf("2 %d\n",idd);
			fflush(stdout);
			continue;
		}
		printf("1 %d\n",K+K);
		fflush(stdout);
		scanf("%d",&iddd);
		if(iddd > idd){
			printf("2 %d\n",idd);
			fflush(stdout);
			continue;
		}
		printf("1 %d\n",K+1+K);
		fflush(stdout);
		scanf("%d",&iddd);
		if(idd == iddd){
			printf("2 %d\n",id);
			fflush(stdout);
			continue;
		}
		
		int len = N/K;
		int r = MyBin(len);
		printf("1 %d\n",r*K+1);
		fflush(stdout);
		scanf("%d",&idd);
		printf("1 %d\n",(r+1)*K);
		fflush(stdout);
		scanf("%d",&iddd);
		if(idd == iddd){
			printf("1 %d\n",(r+1)*K+1);
			fflush(stdout);
			scanf("%d",&re);
			printf("2 %d\n",re);
			fflush(stdout);
		}
		else {
			printf("2 %d\n",idd);
			fflush(stdout);
		}
	}
	
	return 0;
}