-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
85 lines (63 loc) · 2.06 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from queue import PriorityQueue
import sys
def FILE_IO():
sys.stdin = open('input.txt', 'r')
sys.stdout = open('output.txt', 'w')
# def solve():
# n = int(input())
# pq = PriorityQueue()
# pqr = PriorityQueue()
# for _ in range(n):
# line = input()
# if line[0] == '1':
# x = int(line.split()[1])
# pq.put(x)
# elif line[0] == '2':
# x = int(line.split()[1])
# pqr.put(x)
# else:
# while not pqr.empty() and pq.queue[0] == pqr.queue[0]:
# pq.get()
# pqr.get()
# print(pq.queue[0])
# # FILE_IO()
# solve()
def formula(prim_value: int, len_: int):
len_ = min(len_, prim_value)
sub_value = max(prim_value - len_ + 1, 0)
return len_*(sub_value + prim_value) // 2
def maxValue(n: int, index: int, maxSum: int) -> int:
# res = n - 1
# for x in range(n - 1, -1, -1):
# prim_value = x - 1
# sum_left = formula(prim_value, index)
# sum_right = formula(prim_value, n - 1 - index)
# print(f"with x = {x} then sum_left = {sum_left} and sum_right = {sum_right}")
# if x + sum_left + sum_right > maxSum:
# continue
# res = x
# break
# return res
left = 1
right = maxSum
mid = (left + right) // 2
res = 1
while left <= right:
prim_value = mid - 1
sum_left = formula(prim_value, index)
sum_right = formula(prim_value, n - 1 - index)
tmp = mid + sum_left + sum_right
print(f"with mid = {mid}, left is {left} and right is {right} then sum_left = {sum_left} and sum_right = {sum_right} and tmp = {tmp}")
if tmp > maxSum:
right = mid
else:
left = mid
res = max(res, mid)
tmp = (left + right) // 2
if tmp == mid:
break
else:
mid = tmp
return res
print(maxValue(6, 1, 10))
# with mid = 3, left is 3 and right is 4 then sum_left = 2 and sum_right = 3 and tmp = 8