#BS0000110. Tìm phần tử trong mảng hình núi (Find in Mountain Array)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tìm phần tử trong mảng hình núi (Find in Mountain Array)
Find in Mountain Array
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
A mountain array has at least entries and exactly one peak index (), with and . You know its length and target but cannot inspect the whole array. Find the smallest index with , or if absent. Indices start from . The original API calls are adapted to interactive queries while retaining the access limit.
Input
Initially, the judge sends and on one line. Array entries remain hidden. After each query the judge sends the requested array value as an integer.
Output
To read index , print ? i with . The judge returns . At most 100 queries are allowed; repeated queries to the same index still count. Finish by printing ! ans where is the smallest matching index or -1. Flush each message.
Interactive Protocol
Lines beginning with ? are queries to the judge; lines beginning with ! are final answers. Do not print any other text to standard output.
Subtasks
- Subtask 1 (20 points): ; ; ; .
- Subtask 2 (30 points): ; ; ; .
- Subtask 3 (50 points): ; ; ; .
Examples
Input
7 3
Output
? 3
? 4
? 5
? 2
! 2
This is an illustrative transcript, not a fixed output file. Hidden values are revealed only in the explanation to make the example understandable.
Explanation
Suppose the hidden array is and . Queries at indices return . The target occurs at and , so the smallest index is ; output ! 2. Other query orders are allowed.