题意:有一串不递减的串,串中的任意元素都有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. linux文本文件编辑命令

    1.cat命令 cat命令用于查看纯文本文件(内容较少的),格式为“cat [选项] [文件]”. Linux系统中有多个用于查看文本内容的命令,每个命令都有自己的特点,比如这个cat命令就是用于查看 ...

  2. hdu4870 高斯消元

    题意 一个人打比赛 ,rating 有p的概率 为加50分 有1-p的概率为 x-100分 最大值为 1000 最小值为0 有两个号 每次拿较小的号来提交 , 计算最后到达 1000分得期望场数是多少 ...

  3. ios 回调函数作用

    //应用程序启动后调用的第一个方法 不懂的程序可以做不同的启动 //launchOption参数的作业:应用在特定条件下的不同启动参数 比如:挑战的支付宝支付 - (BOOL)application: ...

  4. SiteCore Experience Analytics-体验分析

    体验分析   Sitecore Experience Analytics为营销人员和营销分析师提供仪表板和报告,以识别从其网站和可能的其他外部数据源收集的体验数据的模式和趋势. 体验分析报告示例:   ...

  5. uvalive 4452 The Ministers’ Major Mess

    题意: 有一些部长需要对某些账单进行投票. 一个部长最多对4个账单进行投票,且每票对一个账单通过,要么否决. 问是否存在一个方案使得所有部长有超过半数的投票被通过,如果有,那么说明哪些账单的决定是明确 ...

  6. 微信小程序项目

    大体思想 微信小程序,没有DOM和BOM概念,所以,不会涉及到操作节点.它的主要思想是操作数据,然后改变视图层,即MVVM,如果知道angularJS,能很快的理解上手小程序.   一些开发小程序时, ...

  7. Token:共识的下一个100年,区块链技术是无可阻挡的文明进程

    Token:共识的下一个100年,区块链技术是无可阻挡的文明进程.很少有人意识到,区块链的应用——Token可能是近100年来最伟大的制度创新,超越股份制,并将是『债券』之后最重要最主流的交易品种,没 ...

  8. 【JavaScript 6连载】一、关于对象(访问)

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. Symfony2 学习笔记之服务容器

    现在的PHP应用程序都是面向对象开发,所以主要是由对象构成.有的对象可以方便的分发邮件信息而有的可能帮你把信息写入到数据库中.在你的应用程序中,你可能创建一个对象用于管理你的产品库存,或者另外一个对象 ...

  10. [转载]WeeksInAYear、WeeksInYear、DaysInAYear、DaysInAMonth、DaysInYear、DaysInMonth - 获取指定年月的周、日数

    DateUtils.DaysInYear(); DateUtils.DaysInMonth(); DateUtils.DaysInAYear(); DateUtils.DaysInAMonth(); ...