Skip to content

Commit 67989dd

Browse files
committed
Added W02 materials
1 parent 016dddc commit 67989dd

6 files changed

+155
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
| Week | Content | Slides | WarmUp test | Contest | Soft Deadline |
4141
|:------:|:-----------------------|:--------------------:|:-----------------------:|:------------------------:|:----------------------:|
4242
| 01 | Sorting algorithms | [Slides][Slides_W01] | [Test][WarmUp_test_W01] | [Contest][ContestID_W01] | 07.05.2024 09:00 UTC+7 |
43-
<!---
4443
| 02 | Binary search | [Slides][Slides_W02] | [Test][WarmUp_test_W02] | [Contest][ContestID_W02] | 08.05.2024 09:00 UTC+7 |
44+
<!---
4545
| 03 | Basic Data sturctures | [Slides][Slides_W03] | [Test][WarmUp_test_W03] | [Contest][ContestID_W03] | 09.05.2024 09:00 UTC+7 |
4646
| 04 | Dynamic programming | [Slides][Slides_W04] | [Test][WarmUp_test_W04] | [Contest][ContestID_W04] | 10.05.2024 09:00 UTC+7 |
4747
| 05 | Knapsack problem | [Slides][Slides_W05] | [Test][WarmUp_test_W05] | [Contest][ContestID_W05] | 13.05.2024 09:00 UTC+7 |
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# This is a template for problem Sigma-Trimpazation.
2+
3+
from time import time
4+
5+
# set the following flag to True to estimate execution time
6+
#
7+
# Exact execution time on you device may be different
8+
# from one on Yandex contest platform!
9+
#
10+
# The flag MUST be False when you are submitting your solution!
11+
estimate_execution_time = False
12+
13+
# Fixed parameters of quantum process:
14+
quantum_a = 7**5
15+
quantum_m = 2**31 - 1
16+
17+
18+
def analyze_trimpazation(n, m, q0):
19+
'''
20+
This function generates data with given parameters
21+
and calculates desired Y value.
22+
23+
You need to modify it to make it execute faster.
24+
You can check your progress using estimate_execution_time flag
25+
at the top of the file.
26+
'''
27+
m_div2 = m // 2
28+
q = q0
29+
30+
# generating x data:
31+
x = []
32+
for i in range(n):
33+
x_i = q % m - m_div2
34+
x.append(x_i)
35+
q = ((q * quantum_a) % quantum_m)
36+
37+
# sorting x data:
38+
x.sort()
39+
40+
# calculating sum:
41+
res = 0
42+
for i, v in enumerate(x):
43+
res += (i + 1) * v
44+
45+
return res
46+
47+
48+
if __name__ == '__main__':
49+
N, M, q0 = map(int, input().split())
50+
t = time()
51+
print(analyze_trimpazation(N, M, q0))
52+
if estimate_execution_time:
53+
print(f'Execution time = {time() - t:.8f} seconds')
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
def binsearch_exists(x, key):
2+
l = 0
3+
r = len(x)
4+
while r - l > 1:
5+
m = (l + r) // 2
6+
if x[m] <= key:
7+
l = m
8+
else:
9+
r = m
10+
return x[l] == key
11+
12+
13+
def binsearch_left(x, key):
14+
l = -1
15+
r = len(x)
16+
while r - l > 1:
17+
m = (l + r) // 2
18+
if x[m] < key:
19+
l = m
20+
else:
21+
r = m
22+
return r
23+
24+
25+
def binsearch_right(x, key):
26+
l = -1
27+
r = len(x)
28+
while r - l > 1:
29+
m = (l + r) // 2
30+
if x[m] <= key:
31+
l = m
32+
else:
33+
r = m
34+
return r
35+
36+
37+
N = int(input())
38+
x = list(map(int, input().split()))
39+
x = sorted(x)
40+
key = int(input())
41+
print("YES" if binsearch_exists(x, key) else "NO")

week02_binary_search/counting.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
def counting_sort(x):
2+
N = len(x)
3+
M = max(x) + 1
4+
a = [0] * M
5+
for v in x:
6+
a[v] += 1
7+
res = []
8+
for i in range(M):
9+
for j in range(a[i]):
10+
res.append(i)
11+
return res
12+
13+
14+
def counting_sort2(x):
15+
N = len(x)
16+
M = max(x) + 1
17+
c = [0] * M
18+
for v in x:
19+
c[v] += 1
20+
for i in range(1, M):
21+
c[i] += c[i - 1]
22+
# print(c)
23+
res = [None] * N
24+
for i in range(N):
25+
position = c[x[i]] - 1
26+
res[position] = x[i]
27+
c[x[i]] -= 1
28+
return res
29+
30+
31+
if __name__ == '__main__':
32+
N = int(input())
33+
x = list(map(int, input().split()))
34+
x = counting_sort2(x)
35+
print(' '.join(map(str, x)))

week02_binary_search/sqrt.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from math import sqrt as msqrt
2+
from random import random
3+
4+
def sqrt(x, eps):
5+
l = 0.
6+
r = max(1, float(x))
7+
while r - l > eps:
8+
m = (l + r) / 2
9+
if m ** 2 < x:
10+
l = m
11+
else:
12+
r = m
13+
return (l + r) / 2
14+
15+
16+
eps = 1e-8
17+
while True:
18+
x = random() * 100
19+
y1 = msqrt(x)
20+
y2 = sqrt(x, eps)
21+
if abs(y1 - y2) > eps:
22+
print(f'fail: {x:.9f}')
23+
exit(0)
24+
else:
25+
print('OK')

0 commit comments

Comments
 (0)