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.

题意:有一个由非负整数组成的长度n的数组。数组按非递减顺序排序。数组中的每个数字恰好出现k次,除了一个元素,至少出现一次,但小于k次。你的任务是识别那个元素。

第一次做交互的题。刚拿到题目有点懵比。。因为它并不是我们习惯的输入输出格式,需要先输出再输入。

输出的是向计算机询问的数值下标,输入的是计算机回答的数值元素(注意加入fflush(stdout);)。很锻炼逆向思维。

因为题目限制了在60次内找出value,因此容易想到二分,并用map加以记录。先找到中间值再找出头部位置,看是否位于m%k==1的位置,开始用了二分嵌套WA(超出次数)。

后来想到只用一层二分,找出理论上的头尾部位置(所在位置相距k-1),并比较其值是否相同即可判断打破规律的元素群在左边还是在右边。

#include<bits/stdc++.h>
#define MAX 100005
#define INF 0x3f3f3f3f
using namespace std; map<int,int> mp; int main()
{
int t,n,i,j;
scanf("%d",&t);
while(t--){
mp.clear();
scanf("%d",&n);
printf("1 1\n");
fflush(stdout);
scanf("%d",&mp[]);
printf("1 %d\n",n);
fflush(stdout);
scanf("%d",&mp[n]);
int l=,r=n,m;
int k=;
while(l<=r){
m=(l+r)/;
if(!mp[m]){
printf("1 %d\n",m);
fflush(stdout);
scanf("%d",&mp[m]);
}
if(mp[]<mp[m]) r=m-;
else{
k=max(k,m);
l=m+;
}
}
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[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[]);
fflush(stdout);
continue;
}
int ans=INF;
l=+k,r=n-k;
while(l<=r){
m=(l+r)/;
if(!mp[m-(m-)%k]){
printf("1 %d\n",m-(m-)%k);
fflush(stdout);
scanf("%d",&mp[m-(m-)%k]);
}
if(!mp[m-(m-)%k+k-]){
printf("1 %d\n",m-(m-)%k+k-);
fflush(stdout);
scanf("%d",&mp[m-(m-)%k+k-]);
}
if(mp[m-(m-)%k]==mp[m-(m-)%k+k-]){
l=m+;
}
else{
ans=min(ans,mp[m-(m-)%k]);
r=m-;
}
}
printf("2 %d\n",ans);
fflush(stdout);
}
return ;
}

CodeChef - ELHIDARR Find an element in hidden array(二分交互)的更多相关文章

  1. CodeChef - ELHIDARR Find an element in hidden array(互动题)题解

    题意:有一串不递减的串,串中的任意元素都有k个,除了一个元素,他只有1 <= n < k-1个,你现在能向oj做出以下操作: 输出:1 pos,oj会返回pos位置的元素值 输出:2 va ...

  2. 34. Find First and Last Position of Element in Sorted Array + 二分

    题意懒得抄了,大概是:在升序数组中给定整数target,找到第一个和最后一个target的索引,找到返回{index1, index2},否则返回{-1, -1}: 时间复杂度要求:O(logn) 分 ...

  3. check the element in the array occurs more than half of the array length

    Learn this from stackflow. public class test { public static void main(String[] args) throws IOExcep ...

  4. Kth Largest Element in an Array

    Find K-th largest element in an array. Notice You can swap elements in the array Example In array [9 ...

  5. leetcode@ [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)

    https://leetcode.com/problems/count-of-smaller-numbers-after-self/ You are given an integer array nu ...

  6. leetcode面试准备:Kth Largest Element in an Array

    leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...

  7. Majority Element in an Array

    Problem Statement Given a large array of non-negative integer numbers, write a function which determ ...

  8. Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...

  9. leetcode-algorithms-34 Find First and Last Position of Element in Sorted Array

    leetcode-algorithms-34 Find First and Last Position of Element in Sorted Array Given an array of int ...

随机推荐

  1. HIbernate 级联删除

    在一对多的情形下如 Cinema - > Screen; 1.正常在不设置级联(casCade)的情况下 删除一的一方(Cinema)会报外键关联异常 (Screen 中包含Cinema的外键) ...

  2. php总结2——php中的变量、数据类型及转换、运算符、流程控制中的分支结构

    2.1  php中的变量: 定义变量:$变量名称=值: 变量名称:$开头    $之后的第一位必须是字母    $第二位之后可以是字母.数字或者是下划线.习惯上变量名称有实际含义,第二个单词后首字母大 ...

  3. hdoj 1116 Play on Words 【并查集】+【欧拉路】

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  4. [证书服务器 第二篇] 基于OpenSSL 在 CentOS6 系统上 搭建自签证书服务,并应用于Web容器

    第一部分:概述 .. 第二部分:环境准备 1 操作系统 CentOS 6.x 2 安装openssl yum install -y openssl 3 安装jdk 从官网下载JDK http://ww ...

  5. Android Studio第一次启动失败的解决办法

    Android Studio Android 开发环境 由于GFW的问题,安装后第一次启动会在显示Fetching android sdk component information对话框后,提示错误 ...

  6. qemu仿真执行uboot和barebox

    先安装qemu: apt-get install qemu-system 交叉编译器可以选择友善之臂:http://arm9download.cncncn.com/mini2440/linux/arm ...

  7. Spring 4.3 的新功能和增强

    转载自https://my.oschina.net/waylau/blog/698186 核心容器改进 核心容器额外提供了更丰富的元数据来改进编程. 默认 Java 8 的方法检测为 bean 属性的 ...

  8. VLAN虚拟局域网技术(二)-计算机网络

    本文主要知识来源于学校课程,部分知识来自于H3C及思科中国公司网页技术手册,未经许可,禁止转载.如需转载,请联系作者并注明出处. 本节主要是总结一些思科的VLAN组中的私有协议:DTP和VTP. 1. ...

  9. BZOJ 3410 [Usaco2009 Dec]Selfish Grazing 自私的食草者:贪心【最多线段覆盖】

    题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1324 题意: 给你n个区间,问你最多能选择多少个区间使得它们不相互覆盖. 题解: RQ ...

  10. kvm初体验之八:调整vm的vcpu, memory, disk大小

    假设host上创建的vm的名字为vm1. 1. 查看vm1的domain information [root@tanghuimin thm]# virsh dominfo vm1 Id: 10 Nam ...