Skip to content

Commit 893d199

Browse files
authoredJun 26, 2024
Merge branch 'master' into maximum-building-2
2 parents 0aa660c + 1828518 commit 893d199

File tree

4 files changed

+143
-10
lines changed

4 files changed

+143
-10
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
{

‎content/3_Silver/Prefix_Sums.mdx

+26-7
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ These are also known as
185185
## Solution - Static Range Sum
186186

187187
<LanguageSection>
188-
189188
<CPPSection>
190189

191190
In C++ we can use
@@ -223,7 +222,6 @@ int main() {
223222
```
224223
225224
</CPPSection>
226-
227225
<JavaSection>
228226
229227
```java
@@ -269,14 +267,13 @@ public class Main {
269267
```
270268
271269
</JavaSection>
272-
273270
<PySection>
274271
275272
```py
276273
def psum(a):
277274
psum = [0]
278275
for i in a:
279-
psum.append(psum[-1] + i) # psum[-1] is the last element in the list
276+
psum.append(psum[-1] + i)
280277
return psum
281278

282279

@@ -289,8 +286,30 @@ for i in range(Q):
289286
print(p[r] - p[l])
290287
```
291288
292-
</PySection>
289+
An alternative approach is to use [`itertools.accumulate`](https://docs.python.org/3/library/itertools.html#itertools.accumulate).
290+
Notice that we need to add a $0$ to the front of the array.
291+
If using a newer version, you can use the optional parameter `initial=0` as well.
292+
293+
```py
294+
import itertools
295+
296+
297+
def psum(a):
298+
return [0] + list(itertools.accumulate(a))
299+
293300

301+
# BeginCodeSnip{Same code as above}
302+
N, Q = map(int, input().split())
303+
a = list(map(int, input().split()))
304+
p = psum(a)
305+
306+
for i in range(Q):
307+
l, r = map(int, input().split())
308+
print(p[r] - p[l])
309+
# EndCodeSnip
310+
```
311+
312+
</PySection>
294313
</LanguageSection>
295314
296315
## Problems
@@ -299,8 +318,8 @@ for i in range(Q):
299318
300319
<Warning>
301320
302-
"Haybale Stacking" isn't submittable on the USACO website. Use
303-
[this link](https://www.spoj.com/problems/HAYBALE/) to submit.
321+
"Haybale Stacking" isn't submittable on the USACO website;
322+
Use [this link](https://www.spoj.com/problems/HAYBALE/) to submit instead.
304323
305324
</Warning>
306325

‎solutions/general/cf-4A.mdx

+35-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
id: cf-4A
33
source: CF
44
title: Watermelon
5-
author: Junwon Kim
5+
author: Junwon Kim, Ayush Shukla
66
---
77

88
[Official Analysis (C++)](https://codeforces.com/blog/entry/95249)
@@ -34,4 +34,38 @@ int main() {
3434
```
3535

3636
</CPPSection>
37+
<JavaSection>
38+
39+
```java
40+
import java.io.*;
41+
import java.util.*;
42+
43+
public class Watermelon {
44+
public static void main(String[] args) {
45+
Kattio io = new Kattio();
46+
int w = io.nextInt();
47+
if (w % 2 == 0 && w > 2) {
48+
io.println("YES");
49+
} else {
50+
io.println("NO");
51+
}
52+
io.close();
53+
}
54+
55+
// CodeSnip{Kattio}
56+
}
57+
```
58+
59+
</JavaSection>
60+
<PySection>
61+
62+
```py
63+
w = int(input())
64+
if w % 2 == 0 and w > 2:
65+
print("YES")
66+
else:
67+
print("NO")
68+
```
69+
70+
</PySection>
3771
</LanguageSection>

‎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)