Skip to content

Commit 10e3940

Browse files
committed
Merge branch 'internal-solution/cf-1520F1' of github.com:freakin23/usaco-guide into internal-solution/cf-1520F1
2 parents 457a408 + 5695f2a commit 10e3940

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

solutions/silver/cf-1520F1.mdx

+17-13
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ title: Guess the K-th Zero (Easy Version)
55
author: Rameez Parwez
66
---
77

8-
[Official Editorial](https://codeforces.com/blog/entry/90342?locale=en)
8+
[Official Editorial (C++)](https://codeforces.com/blog/entry/90342)
9+
10+
## Explanation
911

10-
## Implementation
1112
To efficiently locate the $k$-th zero within a segment $[l, r]$, we utilize a binary search strategy. Initially, our segment encompasses the entire range $[l, r]$. The objective is to gradually narrow down this segment until we pinpoint the exact position of the $k$-th zero.
1213

1314
We start by defining the midpoint of the current segment as $m = \frac{l+r}{2}$. This allows us to divide the segment into two halves: $[l, m]$ and $[m+1, r]$.
@@ -18,10 +19,11 @@ Next, we perform a query to determine the number of zeros within the left half $
1819

1920
By following this iterative strategy, we continuously refine our search segment based on whether the $k$-th zero is expected to be in the left or right half. This systematic approach allows us to gradually narrow down the range $[l, r]$ until we pinpoint the exact position of the $k$-th zero.
2021

21-
**Time Complexity:** $\mathcal{O}(\log n)$
22+
## Implementation
23+
24+
**Time Complexity:** $\mathcal{O}(\log N)$
2225

2326
<LanguageSection>
24-
2527
<CPPSection>
2628

2729
```cpp
@@ -32,14 +34,16 @@ int query(int r, int k) {
3234
int res;
3335
cout << "? " << 1 << " " << r << '\n';
3436
cin >> res;
35-
return (r - res <= k);
37+
return r - res <= k;
3638
}
3739

3840
int main() {
3941
int n, t, k;
4042
cin >> n >> t >> k;
4143
k--;
42-
int lo = 0, hi = n;
44+
45+
int lo = 0;
46+
int hi = n;
4347
while (hi - lo > 1) {
4448
int mid = (lo + hi) >> 1;
4549
if (query(mid, k)) {
@@ -48,12 +52,12 @@ int main() {
4852
hi = mid;
4953
}
5054
}
55+
5156
cout << "! " << hi << '\n';
52-
return 0;
5357
}
5458
```
55-
</CPPSection>
5659
60+
</CPPSection>
5761
<PySection>
5862
5963
```py
@@ -64,9 +68,10 @@ def query(r: int, k: int) -> bool:
6468
6569
6670
n, t = map(int, input().split())
67-
k = int(input())
68-
k = k - 1
69-
lo, hi = 0, n
71+
k = int(input()) - 1
72+
73+
lo = 0
74+
hi = n
7075
while hi - lo > 1:
7176
mid = (lo + hi) // 2
7277
if query(mid, k):
@@ -78,5 +83,4 @@ print(f"! {hi}")
7883
```
7984

8085
</PySection>
81-
82-
</LanguageSection>
86+
</LanguageSection>

0 commit comments

Comments
 (0)