题意:有一串不递减的串,串中的任意元素都有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 + 10;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int val[maxn];
bool vis[maxn];
int T, n, k;
int get(int pos){if(pos > n || pos < 1) return -1;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(1);int l = 1, r = n, ans;while(l <= r){int m = (l + r) >> 1;int c = get(m);if(c == a) ans = m;if(c != a) r = m - 1;else l = m + 1;}k = ans;if(get(k + 1) == get(2 * k + 1)){printf("2 %d\n", get(1));fflush(stdout);}else{l = 1, r = n / k + 1;while(l <= r){int m = (l + r) >> 1;if(get(k * m) != get(k * m + 1)){l = m + 1;ans = m + 1;}else{r = m - 1;}}printf("2 %d\n", get((ans - 1) * k + 1));fflush(stdout);}}return 0;
}

转载于:https://www.cnblogs.com/KirinSB/p/10290290.html

CodeChef - ELHIDARR Find an element in hidden array(互动题)题解相关推荐

  1. LeetCode 1538. Guess the Majority in a Hidden Array

    文章目录 1. 题目 2. 解题 1. 题目 We have an integer array nums, where all the integers in nums are 0 or 1. You ...

  2. LeetCode——Kth Largest Element in an Array

    LeetCode--Kth Largest Element in an Array Question Find the kth largest element in an unsorted array ...

  3. 剑指offer 最小的k个数 leetcode 215. Kth Largest Element in an Array

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  4. 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array...

    传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...

  5. 力扣215:数组中的第K个最大元素 (leetcode 215:Kth Largest Element In An Array)

    题目链接:https://leetcode.cn/problems/kth-largest-element-in-an-array 目录: 一.题目描述 1.中文 2.英文 二.解决方法 1.直接排序 ...

  6. Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  7. Leetcode: Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  8. [leedcode 215] Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  9. 49.Kth Largest Element in an Array

    Level:   Medium 题目描述: Find the kth largest element in an unsorted array. Note that it is the kth lar ...

最新文章

  1. simple c语言,C语言——指针(simple).ppt
  2. webApi前端ajax调用后端返回{“readyState“:0,“status“:0,“statusText“:“error“}解决方案
  3. BZOJ 1370: [Baltic2003]Gang团伙 [并查集 拆点 | 种类并查集WA]
  4. leetcode 之Median of Two Sorted Arrays(五)
  5. 烟台农业走进物联网大数据时代
  6. UI设计还在为聊天界面苦恼?好的案例,打开任通二脉
  7. 浓浓中国风的雅致新年元旦PSD分层海报模板
  8. oCPC中转化率模型与校准
  9. vijos P1009清帝之惑之康熙
  10. A - Divisors POJ - 2992 (组合数C的因子数)数学—大数
  11. 35. 源码与 Tarball
  12. javasocket编程面试题,小白也能看明白
  13. 物联那点事儿之小爱同学网络控制电脑开机(arduino+点灯科技篇)
  14. 某个程序员的工作记录
  15. 2022华为机试真题 C++ 实现【事件推送】
  16. 现有的数字版权保护大全
  17. servlet常见错误
  18. 小猫爪:PMSM之FOC控制07-有传感器的速度环
  19. MySQL 8 忽略表名大小写
  20. 阿里云esc服务器上装hadoop

热门文章

  1. 关于阿里云快照收费机机制和说明
  2. sap 中migo收货自动打印smartform_EWM MES/ERP集成 生产收货的几种方式
  3. mysql 截取最后三位查询
  4. mysql命令导出表结构和数据_mysql-用命令导出、导入表结构或数据
  5. 调用某个按钮事件_高级UI晋升之触摸事件分发机制(一)
  6. java 发送信号_java – 在Windows中发送任意信号?
  7. HBuilder Android APP打包发布
  8. spring boot 教程(六)多数据源配置与使用
  9. 一级计算机考试中的DBF,2017年计算机等考一级WPS2000辅导:使用DBF格式内容的方法...
  10. 关于机器学习的十个实例