#BS0000108. Phiên bản lỗi đầu tiên (First Bad Version)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Phiên bản lỗi đầu tiên (First Bad Version)
First Bad Version
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
A project has versions numbered through . There is a hidden first bad version : versions before are good, and version and all later versions are bad. Determine by asking the judge about versions. The Phuoc Hung OJ edition uses an interactive text protocol in place of the original LeetCode isBadVersion(version) API.
Input
Initially, the judge sends one integer . The value is private and is not provided to the contestant. After each query, the judge returns 0 or 1 on its own line.
Output
To query version , print ? i with . The judge replies 0 for good and 1 for bad. To finish, print ! b and terminate. Flush after every message. At most 31 queries are allowed; the final answer does not count as a query.
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): ; is the hidden first bad version.
- Subtask 2 (30 points): ; is the hidden first bad version.
- Subtask 3 (50 points): ; is the hidden first bad version.
Examples
Input
5
Output
? 3
? 4
! 4
This is an illustrative transcript, not a fixed output file. Hidden values are revealed only in the explanation to make the example understandable.
Explanation
For , suppose the hidden value is . Querying returns 0 (good), while querying returns 1 (bad). Since version works and version fails, answer with ! 4. This is one possible interaction.