Skip to content

Commit 980b495

Browse files
authored
Merge pull request #4553 from freakin23/internal-solution/cf-1520F1
internal solution for cf-1520F1
2 parents f779125 + 4c9fd5d commit 980b495

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

content/3_Silver/Binary_Search.problems.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,7 @@
174174
"isStarred": false,
175175
"tags": ["Binary Search"],
176176
"solutionMetadata": {
177-
"kind": "autogen-label-from-site",
178-
"site": "CF"
177+
"kind": "internal"
179178
}
180179
},
181180
{

solutions/silver/cf-1520F1.mdx

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
id: cf-1520F1
3+
source: CF
4+
title: Guess the K-th Zero (Easy Version)
5+
author: Rameez Parwez
6+
---
7+
8+
[Official Editorial (C++)](https://codeforces.com/blog/entry/90342)
9+
10+
## Explanation
11+
12+
We use binary search to find the location of the $k$th zero.
13+
14+
To do this, we set a midpoint and then check if the number of zeros in the left half is greater than or equal to or strictly less than $k$.
15+
This allows us to narrow the location of the $k$th zero to either the left or right half.
16+
17+
## Implementation
18+
19+
**Time Complexity:** $\mathcal{O}(\log N)$
20+
21+
<LanguageSection>
22+
<CPPSection>
23+
24+
```cpp
25+
#include <bits/stdc++.h>
26+
using namespace std;
27+
28+
int query(int r, int k) {
29+
int res;
30+
cout << "? " << 1 << " " << r << '\n';
31+
cin >> res;
32+
return r - res <= k;
33+
}
34+
35+
int main() {
36+
int n, t, k;
37+
cin >> n >> t >> k;
38+
k--;
39+
40+
int lo = 0;
41+
int hi = n;
42+
while (hi - lo > 1) {
43+
int mid = (lo + hi) >> 1;
44+
if (query(mid, k)) {
45+
lo = mid;
46+
} else {
47+
hi = mid;
48+
}
49+
}
50+
51+
cout << "! " << hi << '\n';
52+
}
53+
```
54+
55+
</CPPSection>
56+
<PySection>
57+
58+
```py
59+
def query(r: int, k: int) -> bool:
60+
print(f"? 1 {r}")
61+
res = int(input())
62+
return r - res <= k
63+
64+
65+
n, t = map(int, input().split())
66+
k = int(input()) - 1
67+
68+
lo = 0
69+
hi = n
70+
while hi - lo > 1:
71+
mid = (lo + hi) // 2
72+
if query(mid, k):
73+
lo = mid
74+
else:
75+
hi = mid
76+
77+
print(f"! {hi}")
78+
```
79+
80+
</PySection>
81+
</LanguageSection>

0 commit comments

Comments
 (0)