File tree 2 files changed +82
-2
lines changed
2 files changed +82
-2
lines changed Original file line number Diff line number Diff line change 174
174
"isStarred" : false ,
175
175
"tags" : [" Binary Search" ],
176
176
"solutionMetadata" : {
177
- "kind" : " autogen-label-from-site" ,
178
- "site" : " CF"
177
+ "kind" : " internal"
179
178
}
180
179
},
181
180
{
Original file line number Diff line number Diff line change
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 >
You can’t perform that action at this time.
0 commit comments