CodeChef - ELHIDARR Find an element in hidden array(互动题)题解
题意:有一串不递减的串,串中的任意元素都有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(互动题)题解的更多相关文章
- 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 ...
- LeetCode Kth Largest Element in an Array
原题链接在这里:https://leetcode.com/problems/kth-largest-element-in-an-array/ 题目: Find the kth largest elem ...
- 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 ...
- 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 ...
- 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 ...
- leetcode面试准备:Kth Largest Element in an Array
leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...
- Majority Element in an Array
Problem Statement Given a large array of non-negative integer numbers, write a function which determ ...
- Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...
- 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 ...
随机推荐
- 将网站项目转为 Web form应用程序(转)
转自 http://blog.sina.com.cn/s/blog_53729e4601014ze9.html 本文介绍如何将现有的 Microsoft Visual Studio 2005 网站项目 ...
- 001- CreateProcess failed with error 216 (no message available)错误详解
问题详解 runnerw.exe: CreateProcess failed with error 216 (no message available) 看描述,创建进程失败,应该是main这个入口文 ...
- Big Event in HDU (母函数, 玄学AC)
Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't k ...
- KindEditor echarts
var editor; KindEditor.ready(function (K) { editor = K.create('textarea[name="content"]', ...
- pcb走线注意事项笔记
一.高压隔离. PCB的安全距离: 1.电气间隙或者叫做控件距离. (两相邻的后者一个到相邻电机壳表面的沿空气测量的最短距离,电气间隙的决定,根据测量的工作电压以及绝缘等级就可以决定距离.) a.一次 ...
- 以太坊智能合约开发,Web3.js API 中文文档 ethereum web3.js入门说明
以太坊智能合约开发,Web3.js API 中文文档 ethereum web3.js入门说明 为了让你的Ðapp运行上以太坊,一种选择是使用web3.js library提供的web3.对象.底层实 ...
- [转载]PowerDesigner生成的ORACLE 建表脚本中去掉对象的双引号,设置大、小写
若要将 CDM 中将 Entity的标识符都设为指定的大小写,则可以这么设定: 打开cdm的情况下,进入Tools-Model Options-Naming Convention,把Name和Code ...
- The Little Prince-12/13
The Little Prince-12/13 突然发现:这应该是一封情书~ 我那时什么也不懂!我应该根据她的行为,而不是根据她的话来判断她. 她使我的生活芬芳多彩,我真不该离开她跑出来.我本应该猜出 ...
- 怎样从外网访问内网php-fpm?
本地安装了一个php-fpm,只能在局域网内访问,怎样从外网也能访问到本地的php-fpm呢?本文将介绍具体的实现步骤. 准备工作 安装并启动php-fpm 默认安装的php-fpm端口是9000. ...
- django中模型详解-字段类型与约束条件
这片博文来详细说明django模型的使用,涉及到django模型的创建,字段介绍,以及django模型的crud操作,以及一对一等操作. 在使用模型之前,我们首先设置数据库选项,django的默认数据 ...