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 ...
随机推荐
- hdu5293 lca+dp+树状数组+时间戳
题意是给了 n 个点的树,会有m条链条 链接两个点,计算出他们没有公共点的最大价值, 公共点时这样计算的只要在他们 lca 这条链上有公共点的就说明他们相交 dp[i]为这个点包含的子树所能得到的最 ...
- 使用Navicat导入SQL文件
2018-11-04 19:35:12 开始写 刷新后就可以看见导入的数据库了(按F5刷新) 谢谢.Thank you.Salamat Do(撒拉玛特朵).あリがCám o*n(嘉蒙)とゥ(阿里嘎都) ...
- arc 092C 2D Plane 2N Points
题意: 有n个红色的点和n个蓝色的点,如果红色的点的横坐标和纵坐标分别比蓝色的点的横坐标和纵坐标小,那么这两个点就可以成为一对友好的点. 问最多可以形成多少对友好的点. 思路: 裸的二分图匹配,对于满 ...
- 即时通讯(I)
网络通讯三要素: 网络七层协议划分: 网络五层协议的划分: 要记网络层的5层协议,可以把它想像为一枚洋葱.学过计算机网络的,看到这个网络协议的套接字,大概就会明白了!它是一层一层的进行包裹的,然后交由 ...
- [openjudge-搜索]Lake Counting(翻译及实现)
题目原文 描述 Due to recent rains, water has pooled in various places in Farmer John's field, which is rep ...
- unity3D客户端框架
unity3D客户端框架 博客
- OpenCV LK光流法测试
OpenCV版本: 3.2.0 例程文件目录/samples/cpp/lkdemo.cpp 原始程序是采集相机数据,台式机没有摄像头,用Euroc测试集,偷ORB_SLAM2 /Examples/Mo ...
- linux安装部署Nginx
两个参考地址: NGINX的百度百科:https://baike.baidu.com/item/nginx/3817705?fr=aladdin NGINX的中文网站:http://www.nginx ...
- c# 继承与多种状态
可访问性: public 无限制 internal 只允许在同一个程序集访问 protected ...
- Java基础整理
一.Java中的遍历 1.在java开发中会碰到遍历List删除其中多个元素的情况,如果使用一般的for循环以及增强的for循环,代码会抛出异常ConcurrentModificationExcept ...