题意:有一串不递减的串,串中的任意元素都有k个,除了一个元素,他只有1 <= n < k-1个,你现在能向oj做出以下操作:

输出:1 pos,oj会返回pos位置的元素值

输出:2 val,回答那个特殊的元素是什么值

要求不能询问超过60次,给出特殊元素的值。

思路:先第一次二分找出k。可以想出,k * m和k * m + 1如果不同,那么k * m之前的数肯定没有特殊元素,反之则有,那么我们就找出第一个k * m == k * m + 1的地方,这之前的元素就是特殊元素。

每次printf之后都要加一句fflsh(stdout),否则TLE伺候。

代码:

#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = 1e5 + ;
const int MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
int val[maxn];
bool vis[maxn];
int T, n, k;
int get(int pos){
if(pos > n || pos < ) return -;
if(!vis[pos]){
int b;
printf("1 %d\n", pos);
fflush(stdout);
scanf("%d", &b);
val[pos] = b;
return b;
}
else{
return val[pos];
}
}
int main(){
scanf("%d", &T);
while(T--){
memset(vis, false, sizeof(vis));
scanf("%d", &n);
int a, b;
a = get();
int l = , r = n, ans;
while(l <= r){
int m = (l + r) >> ;
int c = get(m);
if(c == a) ans = m;
if(c != a) r = m - ;
else l = m + ;
}
k = ans;
if(get(k + ) == get( * k + )){
printf("2 %d\n", get());
fflush(stdout);
}
else{
l = , r = n / k + ;
while(l <= r){
int m = (l + r) >> ;
if(get(k * m) != get(k * m + )){
l = m + ;
ans = m + ;
}
else{
r = m - ;
}
}
printf("2 %d\n", get((ans - ) * k + ));
fflush(stdout);
}
}
return ;
}

CodeChef - ELHIDARR Find an element in hidden array(互动题)题解的更多相关文章

  1. CodeChef - ELHIDARR Find an element in hidden array(二分交互)

    Find an element in hidden array There is an array of length N consisting of non-negative integers. T ...

  2. LeetCode Kth Largest Element in an Array

    原题链接在这里:https://leetcode.com/problems/kth-largest-element-in-an-array/ 题目: Find the kth largest elem ...

  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. 去掉idea中竖线

    1.现象如下: 2.解决办法. 3.解决后如下:

  2. 软工网络15团队作业4——Alpha阶段敏捷冲刺1.0

    软工网络15团队作业4--Alpha阶段敏捷冲刺1.0 1. 各个成员在 Alpha 阶段认领的任务,以及整个项目预期的任务量(使用整数表示,与项目预估的总工作小时数一致.比如项目A预估需120小时才 ...

  3. JavaScript三种判断语句和三元运算符

    三种判断语句 1.if结构 语法:if(条件){条件满足时执行的代码块} 2.if else结构 语法:if(条件){条件满足时执行的代码块}   else{条件不满足时执行的代码块} 3.if el ...

  4. mysql的指令

    mysql有一下几方面的特性: 1:多语言支持 2:可以移植性好 3:免费开源 4:高效(支持多线程,充分利用cpu资源,运行速度非常) 5:支持大量数据查询和储存 6:操作简单易于学习 其实数据库就 ...

  5. wrapper class (Integer 为例)

    1,导入 Integer a = 100; Integer b = 100; Integer c = 150; Integer d = 150; a == b; true c == d; false ...

  6. linux常用命令:rmdir 命令

    今天学习一下linux中命令: rmdir命令.rmdir是常用的命令,该命令的功能是删除空目录,一个目录被删除之前必须是空的.(注意,rm - r dir命令可代替rmdir,但是有很大危险性.)删 ...

  7. ClassThird

    动手动脑:  1,在子类中,若要调用父类中被覆盖的方法,可以使用super关键字. 代码: public class Move_hands_Move_head {  public void show( ...

  8. python使用SAX解析xml

    python 标准库包含SAX解析器,SAX用事件驱动模型,通过在解析XML的过程中触发一个个的事件并调用用户定义的回调函数来处理XML文件 在python中使用sax方式处理xml要先引入xml.s ...

  9. 委托、匿名函数到lambda表达式

    在C#2.0之前就有委托了,在2.0之后又引入了匿名方法,C#3.0之后,又引入了Lambda表达式,他们三者之间的顺序是:委托->匿名表达式->Lambda表达式,微软的一步步升级,带给 ...

  10. 如何使用Linux 命令more 查看文本文件

    Linux 下有很多实用工具可以让你在终端界面查看文本文件.其中一个就是 more. more 跟我之前另一篇文章里写到的工具 —— less 很相似.它们之间的主要不同点在于 more 只允许你向前 ...