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 ...
随机推荐
- object base基类分析
uvm_object,是所有uvm data和hierarchical class的基类,实现了copy,compare,print,record之类的函数 扩展类中必须实现create和get_ty ...
- 转:php 5.5源码安装全过程
下载 PHP 源码包 # wget http://cn2.php.net/distributions/php-5.5.15.tar.bz2 # tar xf php-5.5.15.tar.bz2 -C ...
- sitecore系列教程之改进Sitecore编辑体验的5个步骤
Sitecore完全关注客户体验,在适当的时间为合适的人提供合适的体验.虽然没有人会不同意客户体验是王道,但我们仍然需要记住每天使用Sitecore的人们为客户带来惊人体验的体验. 我看到无数客户通过 ...
- Ontology Relations
Overview The following page documents the relations used in the filtered GO ontology. For informatio ...
- Failed to start component [StandardEngine[Tomcat].StandardHost[localhost]]
1.问题场景描述:一个maven项目启动时候,偶尔会报tomcat的这个错误(如图:) 随机报错,有时频率很高,要一直重新启动很多次可能还是启动不了,有时不报错.. 2.解决过程:网上各种寻找解决办法 ...
- 线程安全的CopyOnWriteArrayList
证明CopyOnWriteArrayList是线程安全的 先写一段代码证明CopyOnWriteArrayList确实是线程安全的. ReadThread.java import java.util. ...
- table表格超出部分显示省略号
做table表格时,某一列字数比较多,希望超出宽度的部分以省略号显示 设置table的布局 默认automatic 以表格内容显示相应宽度 改成fixed 以表格列宽显示内容 table{ ta ...
- Kattis之旅——Factovisors
The factorial function, n! is defined thus for n a non-negative integer: 0! = 1 n! = n * (n-1)! (n & ...
- 论文翻译——Rapid 2D-to-3D conversion——快速2D到3D转换
https://blog.csdn.net/qq_33445835/article/details/80143598 目前想做一个关于2D转3D的项目,由于国内资料比较少而且大部分都是基于国外的研究 ...
- 【gulp-sass】本地搭建sass开发环境
首先去官网下载一下nodejs. 然后安装gulp: 选择一个目录执行命令行:npm init,一直回车生成package.json文件 再执行命令:npm install gulp --save-d ...