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, and less than K times. Your task is to identify that element.
This was an interactive problem. You is only given the integer N in the input. Both the array and the value of K are hidden. 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 such queries.
Input and Output
The first line of the input contains a single integer T denoting the number of the 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 is types of operations:to perform one operation, you should print to the standard output a line containing space-separated integers type and Val.
- If Type = 1, you is asking the judge a query for the value of the element of the An array at index Val. After printing this line, the judge would print to the standard input a line containing one integer corresponding to the VA Lue of the element at index Val.
- If Type = 2, you are telling the judge, the element with frequency less than K is val. For each test case, you should perform this operation exactly once at the end. This isn't counted towards the queries.
Note
Don ' t forget to flush the "standard" output after printing. It can be-done using fflush (stdout) in C + +, System.out.flush () in Java and Sys.out.flush () in Python.
If you ask more than queries, your program would 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 Feedbackyour output131 211 351 112 5
Explanation
Example Case 1: Suppose the array is [1, 1, 5]. Note the value of K is 2, but it's hidden from.
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 a Nswers 1.
Now, your tell the judge, the answer is 5. You made a total of 3 queries.
Test instructions: There is an array of length n consisting of nonnegative integers. Arrays are sorted in non-descending order. Each number in the array occurs exactly k times, except for one element, appearing at least once, but less than k times. Your task is to identify that element.
The first time to do interactive questions. Just got the topic a little confused than. Because it is not the input and output format we are accustomed to, we need to output and re-enter it first.
The output is a numeric subscript that is queried to the computer, and the input is the numeric element that the computer answers (note adding fflush (stdout);). Exercise converse thinking very well.
Because the topic limits the value to be found within 60 times, it is easy to think of two points and record them with a map. Find the median before finding the position of the head to see if it is in the m%k==1 position and start using two points to nest WA (number of times exceeded).
Later thought that only with a layer of two points, find the theoretical head and tail position (where the location of the k-1), and compare its value is the same can be judged to break the law of the element group on the left or on the right.
#include <bits/stdc++.h>#defineMAX 100005#defineINF 0x3f3f3f3fusing namespaceStd;map<int,int>MP;intMain () {intt,n,i,j; scanf ("%d",&t); while(t--) {mp.clear (); scanf ("%d",&N); printf ("1 1\n"); Fflush (stdout); scanf ("%d", &mp[1]); printf ("1%d\n", N); Fflush (stdout); scanf ("%d",&Mp[n]); intL=1, r=n,m; intk=0; while(l<=R) {m= (l+r)/2; if(!Mp[m]) {printf ("1%d\n", M); Fflush (stdout); scanf ("%d",&mp[m]); } if(mp[1]<MP[M]) r=m-1; Else{k=Max (k,m); L=m+1; } } if(!mp[n-k+1]) {printf ("1%d\n", n-k+1); Fflush (stdout); scanf ("%d", &mp[n-k+1]); } if(mp[n-k+1]!=Mp[n]) {printf ("2%d\n", Mp[n]); Fflush (stdout); Continue; } if(!mp[n-K]) {printf ("1%d\n", N-k); Fflush (stdout); scanf ("%d", &mp[n-K]); } if(mp[n-k]==Mp[n]) {printf ("2%d\n", mp[1]); Fflush (stdout); Continue; } intans=INF; L=1+k,r=n-K; while(l<=R) {m= (l+r)/2; if(!mp[m-(M-1)%K]) {printf ("1%d\n", M-(M-1)%k); Fflush (stdout); scanf ("%d", &mp[m-(M-1)%K]); } if(!mp[m-(M-1)%k+k-1]) {printf ("1%d\n", M-(M-1)%k+k-1); Fflush (stdout); scanf ("%d", &mp[m-(M-1)%k+k-1]); } if(Mp[m-(M-1)%k]==mp[m-(M-1)%k+k-1]) {L=m+1; } Else{ans=min (ans,mp[m-(M-1)%K]); R=m-1; }} printf ("2%d\n", ans); Fflush (stdout); } return 0;}
Codechef-elhidarr Find an element in hidden array (two-part interaction)