题目
题目出处:https://codeforces.com/contest/1480/problem/C
C. Searching Local Minimum
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
This is an interactive problem.
Homer likes arrays a lot and he wants to play a game with you.
Homer has hidden from you a permutation a1,a2,…,an of integers 1 to n. You are asked to find any index k (1≤k≤n) which is a local minimum.
For an array a1,a2,…,an, an index i (1≤i≤n) is said to be a local minimum if ai<min{ai−1,ai+1}, where a0=an+1=+∞. An array is said to be a permutation of integers 1 to n, if it contains all integers from 1 to n exactly once.
Initially, you are only given the value of n without any other information about this permutation.
At each interactive step, you are allowed to choose any i (1≤i≤n) and make a query with it. As a response, you will be given the value of ai.
You are asked to find any index k which is a local minimum after at most 100 queries.
Interaction
You begin the interaction by reading an integer n (1≤n≤105) on a separate line.
To make a query on index i (1≤i≤n), you should output “? i” in a separate line. Then read the value of ai in a separate line. The number of the “?” queries is limited within 100.
When you find an index k (1≤k≤n) which is a local minimum, output “! k” in a separate line and terminate your program.
In case your query format is invalid, or you have made more than 100 “?” queries, you will receive Wrong Answer verdict.
After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
fflush(stdout) or cout.flush() in C++;
System.out.flush() in Java;
flush(output) in Pascal;
stdout.flush() in Python;
see documentation for other languages.
Hack Format
The first line of the hack should contain a single integer n (1≤n≤105).
The second line should contain n distinct integers a1,a2,…,an (1≤ai≤n).
Example
inputCopy
5
3
2
1
4
5
outputCopy
? 1
? 2
? 3
? 4
? 5
! 3
题意
给出一个n为数组大小,但你不能知道整体数组情况,你可以通过互动对数组进行查询,你可以选择任意i进行查询,并且回得到i位置的元素,问在不超过100次的查询中能够找出局部最小值的位置并且输出
思路
可以利用二分法进行解决,因为本题实际求的是 a [ m i d − 1 ] > a [ m i d ] 并 且 a [ m i d ] < a [ m i d + 1 ] a[mid-1]>a[mid] 并且a[mid]<a[mid+1] a[mid−1]>a[mid]并且a[mid]<a[mid+1] 的 a [ m i d ] a[mid] a[mid]的 m i d mid mid,所以只要用二分法一直往小的方向查找,就会发现当 r = = l r==l r==l的时候就是局部最小值。故我们使用二分法的时候每一轮只要查询 a [ m i d ] , a [ m i d + 1 ] a[mid],a[mid+1] a[mid],a[mid+1]的时候即可,通过这两个数值的比较来确定查找方向。
代码实现
#include<iostream>
#include<cstdio>
using namespace std;
int a[100000];
int l,r;
void f(int x)
{
if(x>=1&&x<=r) //x没有超过查找范围
{
cout<<"? "<<x<<endl;
cin>>a[x];
}
}
int main()
{
int n;
cin>>n;
l=1;
r=n;
a[0]=n+1; //设置边界 边界值设置为数组里面最大值,因为在边界只需要比较一个值即可知道局部情况
a[r+1]=n+1;
while(l<r) //二分查找
{
int mid=(r+l)/2;
f(mid); //查询 a[mid]的值
f(mid+1); //查询 a[mid+1]的值
if(a[mid]>a[mid+1]) l=mid+1; //a[mid]>a[mid+1]时,查找方向往右边走,即往小的方向走
else r=mid; //反之,则想左边走
}
cout<<"! "<<r; //输出结果
}
这篇博客介绍了如何在给定一个未知的长度为n的数组时,通过互动查询找到局部最小值的索引。交互过程允许查询数组中任意位置的元素,不超过100次。博主提出利用二分法来解决问题,通过不断查询中间元素及其相邻元素,来确定局部最小值的位置。最终示例展示了二分法的实现过程。
761

被折叠的 条评论
为什么被折叠?



