Compare commits

..

13 Commits

Author SHA1 Message Date
f014389281 add 堆排序. 2024-05-15 16:16:05 +08:00
94021b3455 doc 格式化问题. 2023-07-08 15:12:02 +08:00
4d1c595a6b add 处理输入后ac. 2023-07-08 15:08:10 +08:00
f674b758aa add tle. 2023-07-08 14:01:55 +08:00
ab7ad7c09e add 数论基础 唯一分解定理(算术基本定理). 2023-07-08 13:22:56 +08:00
7d65b8c204 add 简单判断签到题. 2023-07-08 13:02:23 +08:00
fe1a61e369 add 张泽l1. 2022-11-22 15:20:08 +08:00
6b40f708b2 add 电信研发工程师考试内容. 2022-10-28 02:37:25 +08:00
2aa12fbb6e add 中国剩余定理. 2022-04-10 05:51:10 +08:00
be353533a7 add 素数筛. 2022-04-10 05:50:28 +08:00
4a67c4295a add 复盘. 2022-04-10 05:47:28 +08:00
16950b9a5c add 2022蓝桥省赛结束. 2022-04-09 12:19:47 +08:00
7b77a1216e add 练习题. 2022-04-09 01:07:54 +08:00
57 changed files with 1235 additions and 0 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

11
.idea/Algorithm.iml generated Normal file
View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" />
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.12 (Algorithm)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

15
.idea/git_toolbox_prj.xml generated Normal file
View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GitToolBoxProjectSettings">
<option name="commitMessageIssueKeyValidationOverride">
<BoolValueOverride>
<option name="enabled" value="true" />
</BoolValueOverride>
</option>
<option name="commitMessageValidationEnabledOverride">
<BoolValueOverride>
<option name="enabled" value="true" />
</BoolValueOverride>
</option>
</component>
</project>

View File

@@ -0,0 +1,7 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="PROJECT_PROFILE" value="Default" />
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.12 (Algorithm)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (Algorithm)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Algorithm.iml" filepath="$PROJECT_DIR$/.idea/Algorithm.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

0
ct/__init__.py Normal file
View File

17
ct/l1/8-平衡数.py Normal file
View File

@@ -0,0 +1,17 @@
def f(x):
y = 1
for j in x:
y *= int(j)
return y
n = input()
flag = True
for i in range(1, len(n)):
left, right = n[:i], n[i:]
if f(left) == f(right):
flag = False
print("YES")
break
if flag:
print("No")

24
ct/l1/星之卡比.py Normal file
View File

@@ -0,0 +1,24 @@
import io
import sys
sys.stdin = io.StringIO('''4
5 5 5
1 2 40
10 5 41
7 2 42''')
# 80%
n = int(input())
flag = True
for _ in range(n):
chang, kuan, gao = map(int, input().split())
if gao >= 41:
continue
tiji = chang * kuan * gao
flag = False
print(tiji)
if flag:
print(0)

19
ct/l1/求阶乘位数.py Normal file
View File

@@ -0,0 +1,19 @@
import io
import sys
sys.stdin = io.StringIO('''2
10
20''')
from math import pi, e, log10, ceil
m = int(input())
for _ in range(m):
num = int(input())
if num == 0 or num == 1:
print(1)
continue
digit = ceil(log10(float(2 * pi * num)) / 2 + num * log10(float(num / e)))
print(digit)

15
ct/l2/14-Ruby和薯条.py Normal file
View File

@@ -0,0 +1,15 @@
import io
import sys
sys.stdin = io.StringIO('''5 2 3
3 1 6 2 5''') # 4
n, l, r = map(int, input().split())
a = list(map(int, input().split()))
s = 0
for i in range(n - 1):
for j in range(i + 1, n):
if l <= abs(a[i] - a[j]) <= r:
s += 1
print(s)

19
ct/l2/15-小红无敌.py Normal file
View File

@@ -0,0 +1,19 @@
import io
import sys
sys.stdin = io.StringIO('''2 3 1 3''') # 26
a, h, b, k = map(int, input().split())
c = 0
while h > 0 and k > 0:
c += a
c += b
h -= b
k -= a
if h > 0 > k:
c += a * 10
elif h <= 0 < k:
c += b * 10
print(c)

View File

@@ -0,0 +1,22 @@
import io
import sys
sys.stdin = io.StringIO('''4
a
ab
abbc
aaabb''') # 1 2 5 8
t = int(input())
s = []
for _ in range(t):
s.append(input())
for i in s:
k = 0
for j in range(len(i)):
if j + 1 < len(i):
if i[j] == i[j + 1]:
k += 1
print(k + len(i))

View File

@@ -0,0 +1,33 @@
import io
import sys
sys.stdin = io.StringIO('''1
aabbccc''') # cabcacb
from collections import Counter
def reorganizeString(str):
cnt = Counter(str)
# 桶的数目等于字符串中最多的元素的数目
bucketNum = cnt.most_common(1)[0][1]
buckets = [[] for _ in range(bucketNum)]
idx = 0
# 优先填充数目最多的元素
for c, num in cnt.most_common():
while num:
buckets[idx].append(c)
# 循环在不同桶中进行填充
idx = (idx + 1) % bucketNum
num -= 1
return "".join(["".join(bucket) for bucket in buckets]) if list(map(len, buckets)).count(1) <= 1 else ""
n = int(input())
str = input()
result = reorganizeString(str)
if result == '':
print('no')
else:
print('yes')
print(result)

View File

@@ -0,0 +1,22 @@
import io
import sys
sys.stdin = io.StringIO('''5 5
4 8 2 9 1''') # 20
n, k = map(int, input().split())
a = list(map(int, input().split()))
dp = [[-1] * k for i in range(n + 1)]
dp[0][0] = 0
for i in range(n + 1):
for j in range(k):
if dp[i - 1][j] != -1:
dp[i][j] = dp[i - 1][j]
tmp = (j - a[i - 1] % k + k) % k
if dp[i - 1][tmp] != -1:
dp[i][j] = max(dp[i][j], dp[i - 1][tmp] + a[i - 1])
if dp[n][0] == 0:
print(-1)
else:
print(dp[n][0])

View File

@@ -0,0 +1,16 @@
import io
import sys
sys.stdin = io.StringIO('''2 60 3 96''') # 12 / 6
a = list(map(int, input().split()))
_min, _max = min(a), max(a)
k = 0
for i in range(_min, _max):
if i % a[0] + i % a[2] + a[1] % i + a[3] % i == 0:
k += 1
print(i)
break
if k == 0:
print(-1)

View File

@@ -0,0 +1,27 @@
import io
import sys
sys.stdin = io.StringIO('''18:0?
2?:1?''') # 121 319
def check(tStr, tInt):
s = '%02d' % (tInt / 60) + ':' + '%02d' % (tInt % 60)
for x in range(len(s)):
if tStr[x] != '?' and tStr[x] != s[x]:
return False
return True
tStr1 = input()
tStr2 = input()
minT, maxT = 9999, 0
for i in range(0, 24 * 60):
if check(tStr1, i):
for j in range(i + 1, 24 * 60):
if check(tStr2, j):
minT = j - i if 0 < j - i < minT else minT
maxT = j - i if maxT < j - i else maxT
print(minT, maxT)

View File

@@ -0,0 +1,13 @@
import io
import sys
sys.stdin = io.StringIO('''4
2 6 4 1''') # 7
n = int(input())
a = list(map(int, input().split()))
dp0, dp1 = 0, a[0]
for i in range(1, n):
dp0, dp1 = dp1, max(dp1, dp0 + a[i])
print(dp1)

0
ct/l2/__init__.py Normal file
View File

13
ct/l2/两数乘积k.py Normal file
View File

@@ -0,0 +1,13 @@
from bisect import *
n, k = map(int, input().split())
a = sorted(list(map(int, input().split())))
pair = [0, 0, 0]
for idx in range(n - 1):
l = bisect_left(a, k / a[idx], idx + 1, n)
r = bisect_right(a, k / a[idx], idx + 1, n)
pair[0] += n - r
pair[1] += r - l
pair[2] += l - idx - 1
print(*pair)

12
ct/l2/回文.py Normal file
View File

@@ -0,0 +1,12 @@
def solution(s):
length = len(s)
for n in range(2, length + 1):
for i in range(0, length - n + 1):
if s[i:i + n - 1] == s[i + n - 1:i:-1]:
print(n)
return
print("-1")
s = input()
solution(s)

44
ct/l2/打家劫舍3.py Normal file
View File

@@ -0,0 +1,44 @@
import sys
sys.setrecursionlimit(100010) # 修改默认递归深度
class BiTNode(object): # 二叉树节点
def __init__(self, data):
self.data = data
self.dp = [0, 0] # dp[0]表示不偷该节点,dp[1]表示偷该节点
self.lChild = None
self.rChild = None
# 二叉树的后序遍历
def dfs(T):
if T != None:
dfs(T.lChild)
dfs(T.rChild)
lChildDp = [0, 0]
rChildDp = [0, 0]
if T.lChild != None:
lChildDp = T.lChild.dp
if T.rChild != None:
rChildDp = T.rChild.dp
# 不偷该节点,左节点的最优情况(偷与不偷取大者) + 右节点的最优情况(偷与不偷取大者)
T.dp[0] = max(lChildDp[0], lChildDp[1]) + max(rChildDp[0], rChildDp[1])
# 偷该节点,该节点的现金 + 不偷左节点获得的现金 + 不偷右节点获得的现金
T.dp[1] = T.data + lChildDp[0] + rChildDp[0]
n = int(input())
nodes = list(map(int, input().split()))
fathers = list(map(int, input().split()))
nodeArray = [BiTNode(None) for i in range(n)]
for i in range(n): # 构造二叉树
nodeArray[i].data = nodes[i]
f = fathers[i] - 1 # 节点编号从0开始
if f >= 0:
if nodeArray[f].lChild == None:
nodeArray[f].lChild = nodeArray[i]
else:
nodeArray[f].rChild = nodeArray[i]
dfs(nodeArray[0]) # 二叉树的后序遍历nodeArray[0]为根节点
print(max(nodeArray[0].dp[0], nodeArray[0].dp[1]))

0
ct/main.py Normal file
View File

0
lanqiao/__init__.py Normal file
View File

22
lanqiao/main.py Normal file
View File

@@ -0,0 +1,22 @@
import io
import sys
sys.stdin = io.StringIO('WHERETHEREISAWILLTHEREISAWAY')
s_in = input()
# print(s_in)
# n = 97
# prime = [1] * (n + 1)
# prime[0] = prime[1] = 0
# for i in range(2, n + 1):
# j = i * i
# while j <= n:
# prime[j] = 0
# j += i
#
# print(prime)
# for idx, num in enumerate(prime):
# if num:
# print(idx)

View File

@@ -0,0 +1,10 @@
import io
import sys
sys.stdin = io.StringIO('WHERETHEREISAWILLTHEREISAWAY')
s_in = input()
l = list(s_in)
l.sort()
print(''.join(l))
assert 'AADDDDDGGOOOOPSTUUYYY' == 'AADDDDDGGOOOOPSTUUYYY'

View File

@@ -0,0 +1,83 @@
import math
nums = [
[2, 1],
[3, 2],
[4, 1],
[5, 4],
[6, 5],
[7, 4],
[8, 1],
[9, 2],
[10, 9],
[11, 0],
[12, 5],
[13, 10],
[14, 11],
[15, 14],
[16, 9],
[17, 0],
[18, 11],
[19, 18],
[20, 9],
[21, 11],
[22, 11],
[23, 15],
[24, 17],
[25, 9],
[26, 23],
[27, 20],
[28, 25],
[29, 16],
[30, 29],
[31, 27],
[32, 25],
[33, 11],
[34, 17],
[35, 4],
[36, 29],
[37, 22],
[38, 37],
[39, 23],
[40, 9],
[41, 1],
[42, 11],
[43, 11],
[44, 33],
[45, 29],
[46, 15],
[47, 5],
[48, 41],
[49, 46],
]
def check(number):
for num, mod in nums:
a, b = divmod(number, num)
if b != mod:
print(f"{number} % {num} == {number % num} != {mod}")
return False
print(f"{number}: pass.")
return True
def lcm(a, b):
return a * b // math.gcd(a, b)
ans = 2022040920220409
check(ans)
# 中国剩余定理
ans = product = 1
for num, mod in nums:
while ans % num != mod:
ans += product
product = lcm(product, num)
print(f"{ans} % {num} == {mod}")
print(ans)
check(ans)

View File

@@ -0,0 +1,70 @@
# for i in range(2, 50):
# print(f"[{i}, ],")
nums = [
[2, 1],
[3, 2],
[4, 1],
[5, 4],
[6, 5],
[7, 4],
[8, 1],
[9, 2],
[10, 9],
[11, 0],
[12, 5],
[13, 10],
[14, 11],
[15, 14],
[16, 9],
[17, 0],
[18, 11],
[19, 18],
[20, 9],
[21, 11],
[22, 11],
[23, 15],
[24, 17],
[25, 9],
[26, 23],
[27, 20],
[28, 25],
[29, 16],
[30, 29],
[31, 27],
[32, 25],
[33, 11],
[34, 17],
[35, 4],
[36, 29],
[37, 22],
[38, 37],
[39, 23],
[40, 9],
[41, 1],
[42, 11],
[43, 11],
[44, 33],
[45, 29],
[46, 15],
[47, 5],
[48, 41],
[49, 46],
]
ans = 11 * 17
while True:
flag = True
for num, mod in nums:
a, b = divmod(ans, num)
if b != mod:
flag = False
break
if flag:
print(ans)
break
ans += 11 * 17
if ans > pow(10, 17):
break
print(flag, ans)

View File

@@ -0,0 +1,69 @@
# for i in range(2, 50):
# print(f"[{i}, ],")
import random
nums = [
[2, 1],
[3, 2],
[4, 1],
[5, 4],
[6, 5],
[7, 4],
[8, 1],
[9, 2],
[10, 9],
[11, 0],
[12, 5],
[13, 10],
[14, 11],
[15, 14],
[16, 9],
[17, 0],
[18, 11],
[19, 18],
[20, 9],
[21, 11],
[22, 11],
[23, 15],
[24, 17],
[25, 9],
[26, 23],
[27, 20],
[28, 25],
[29, 16],
[30, 29],
[31, 27],
[32, 25],
[33, 11],
[34, 17],
[35, 4],
[36, 29],
[37, 22],
[38, 37],
[39, 23],
[40, 9],
[41, 1],
[42, 11],
[43, 11],
[44, 33],
[45, 29],
[46, 15],
[47, 5],
[48, 41],
[49, 46],
]
def check(number):
for num, mod in nums:
a, b = divmod(number, num)
if b != mod:
# print(f"{number} % {num} != {mod}")
return False
print(f"{number}: pass.")
return True
while True:
check(random.randint(90909090, pow(10, 17)))

View File

@@ -0,0 +1,70 @@
# for i in range(2, 50):
# print(f"[{i}, ],")
nums = [
[2, 1],
[3, 2],
[4, 1],
[5, 4],
[6, 5],
[7, 4],
[8, 1],
[9, 2],
[10, 9],
[11, 0],
[12, 5],
[13, 10],
[14, 11],
[15, 14],
[16, 9],
[17, 0],
[18, 11],
[19, 18],
[20, 9],
[21, 11],
[22, 11],
[23, 15],
[24, 17],
[25, 9],
[26, 23],
[27, 20],
[28, 25],
[29, 16],
[30, 29],
[31, 27],
[32, 25],
[33, 11],
[34, 17],
[35, 4],
[36, 29],
[37, 22],
[38, 37],
[39, 23],
[40, 9],
[41, 1],
[42, 11],
[43, 11],
[44, 33],
[45, 29],
[46, 15],
[47, 5],
[48, 41],
[49, 46],
]
ans = 11 * 17
while True:
flag = True
for num, mod in nums:
a, b = divmod(ans, num)
if b != mod:
flag = False
break
if flag:
print(ans)
break
ans += 11
if ans > pow(10, 17):
break
print(flag, ans)

View File

@@ -0,0 +1,70 @@
# for i in range(2, 50):
# print(f"[{i}, ],")
nums = [
[2, 1],
[3, 2],
[4, 1],
[5, 4],
[6, 5],
[7, 4],
[8, 1],
[9, 2],
[10, 9],
[11, 0],
[12, 5],
[13, 10],
[14, 11],
[15, 14],
[16, 9],
[17, 0],
[18, 11],
[19, 18],
[20, 9],
[21, 11],
[22, 11],
[23, 15],
[24, 17],
[25, 9],
[26, 23],
[27, 20],
[28, 25],
[29, 16],
[30, 29],
[31, 27],
[32, 25],
[33, 11],
[34, 17],
[35, 4],
[36, 29],
[37, 22],
[38, 37],
[39, 23],
[40, 9],
[41, 1],
[42, 11],
[43, 11],
[44, 33],
[45, 29],
[46, 15],
[47, 5],
[48, 41],
[49, 46],
]
ans = 11 * 17
while True:
flag = True
for num, mod in nums:
a, b = divmod(ans, num)
if b != mod:
flag = False
break
if flag:
print(ans)
break
ans += 17
if ans > pow(10, 17):
break
print(flag, ans)

View File

@@ -0,0 +1,13 @@
import io
import sys
sys.stdin = io.StringIO('A5')
ans = [1189, 841]
s_in = input()
a = int(s_in[1:])
for i in range(a):
ans[0], ans[1] = ans[1], ans[0] // 2
print(ans[0])
print(ans[1])

View File

@@ -0,0 +1,13 @@
import io
import sys
# import time
sys.stdin = io.StringIO('1000000\n5')
# start = time.time()
n, m = int(input()), int(input())
nums = [i for i in range(1, n + 1)]
nums.sort(key=lambda x: sum(map(int, list(str(x)))))
print(nums[m - 1])
# print(time.time() - start) # 2s

View File

@@ -0,0 +1,8 @@
import io
import sys
sys.stdin = io.StringIO('0 5 3 2 3 2\n')
d1, p1, q1, d2, p2, q2 = map(int, input().split())
# print(d1, p1, q1, d2, p2, q2)
print(p1 + q2)

View File

@@ -0,0 +1,33 @@
import io
import sys
sys.stdin = io.StringIO('edda')
sys.stdin = io.StringIO('sdfhhhhcvhhxcxnnnnshh')
s_in = f"EMPTY{input()}"
while True:
flag = True
del_index = set()
for i in range(5, len(s_in) - 1):
if s_in[i - 1] == s_in[i] and s_in[i] != s_in[i + 1]:
del_index.add(i)
del_index.add(i + 1)
elif s_in[i - 1] != s_in[i] and s_in[i] == s_in[i + 1]:
del_index.add(i - 1)
del_index.add(i)
if del_index:
flag = False
s = ''
for idx, ch in enumerate(s_in):
if idx in del_index:
continue
s += ch
s_in = s
if flag:
break
print(s_in if s_in == 'EMPTY' else s_in.replace('EMPTY', ''))

View File

@@ -0,0 +1,23 @@
import io
import sys
sys.stdin = io.StringIO('3') # 9
sys.stdin = io.StringIO('2022') # 593300958
# from itertools import permutations
#
# per = list(permutations(range(5)))
# print(per)
# 冒泡的交换次数公式
# print(len(per))
n = int(input())
ans = 0
per_num = 1
for i in range(1, n + 1):
for p in range(2, i + 1):
per_num *= p
ans += per_num // 2
ans %= 998244353
print(ans)

View File

@@ -0,0 +1,21 @@
import io
import sys
sys.stdin = io.StringIO('''3 6
10 5
9 2
8 1
''') # 47
import math
n, m = map(int, input().split())
nums = []
for i in range(n):
a, b = map(int, input().split())
for j in range(math.ceil(a / b)):
nums.append(a)
a -= b
nums.sort(reverse=True)
print(sum(nums[:m]))

View File

@@ -0,0 +1,23 @@
import io
import sys
sys.stdin = io.StringIO('''5 1
1 4 2 8 5
''') # 4
n, k = map(int, input().split())
nums = list(map(int, input().split()))
def cal_dp(arr):
dp = [1] * n
for i in range(1, n):
for j in range(i):
if arr[i] > arr[j] and dp[i] < dp[j] + 1:
dp[i] = dp[j] + 1
return dp
dp = cal_dp(nums)
print(max(dp) + k)

View File

@@ -0,0 +1,55 @@
map_in = [
['A', 'E', 1],
['A', 'B', 2],
['A', 'C', 1],
['A', 'D', 1],
['A', 'E', 1],
['B', 'G', 1],
['B', 'J', 2],
['C', 'D', 3],
['C', 'G', 3],
['C', 'F', 3],
['D', 'G', 2],
['D', 'H', 1],
['D', 'I', 2],
['E', 'H', 1],
['E', 'I', 3],
['F', 'J', 1],
['F', 'G', 1],
['G', 'K', 2],
['G', 'I', 3],
['H', 'L', 2],
['H', 'I', 1],
['I', 'M', 3],
['J', 'S', 2],
['K', 'N', 1],
['K', 'L', 3],
['L', 'R', 1],
['L', 'M', 1],
['M', 'N', 2],
['M', 'Q', 1],
['M', 'S', 1],
['N', 'P', 1],
['Q', 'O', 1],
['O', 'R', 3],
['P', 'O', 1],
['R', 'S', 1],
]
def dp(now_dis, next_node):
for src, dst, dis in map_in:
if src == next_node:
now_dis += dis
if dst == 'S':
ans.append(now_dis)
else:
dp(now_dis, dst)
ans = []
for src, dst, dis in map_in:
if src == 'A':
dp(dis, dst)
print(min(ans))

View File

@@ -0,0 +1,59 @@
map_in = [
['A', 'E', 1],
['A', 'B', 2],
['A', 'C', 1],
['A', 'D', 1],
['A', 'E', 1],
['B', 'G', 1],
['B', 'J', 2],
['C', 'D', 3],
['C', 'G', 3],
['C', 'F', 3],
['D', 'G', 2],
['D', 'H', 1],
['D', 'I', 2],
['E', 'H', 1],
['E', 'I', 3],
['F', 'J', 1],
['F', 'G', 1],
['G', 'K', 2],
['G', 'I', 3],
['H', 'L', 2],
['H', 'I', 1],
['I', 'M', 3],
['J', 'S', 2],
['K', 'N', 1],
['K', 'L', 3],
['L', 'R', 1],
['L', 'M', 1],
['M', 'N', 2],
['M', 'Q', 1],
['M', 'S', 1],
['N', 'P', 1],
['Q', 'O', 1],
['O', 'R', 3],
['P', 'O', 1],
['R', 'S', 1],
]
# 点的数量 数出来的
n = 19
# 初始化图 到自身到距离为0
fig = [[float('inf')] * n for _ in range(n)]
for i in range(n):
fig[i][i] = 0
# 构建好路 每个节点之间的距离
for src, dst, dis in map_in:
src, dst = ord(src) - ord('A'), ord(dst) - ord('A')
fig[src][dst] = fig[dst][src] = dis
# 从起点到每个节点到距离 S为最后一个节点 因为是字母序排序的
dis = [float('inf')] * n
dis[0] = 0
for i in range(n):
for j in range(n):
dis[j] = min(dis[j], dis[i] + fig[i][j])
print(dis)
print(dis[-1])

View File

@@ -0,0 +1,18 @@
import io
import sys
sys.stdin = io.StringIO('lanqiao')
s_in = input()
counter = [0] * 26
count_max = 0
for ch in s_in:
idx = ord(ch) - ord('a')
counter[idx] += 1
count_max = max(count_max, counter[idx])
for i in range(26):
if count_max == counter[i]:
print(chr(i + ord('a')))
print(count_max)
break

View File

@@ -0,0 +1,24 @@
import io
import sys
sys.stdin = io.StringIO('20200202')
import datetime
s_in = input()
year, month, day = int(s_in[:4]), int(s_in[4:6]), int(s_in[-2:])
now = datetime.date(year, month, day)
one_day = datetime.timedelta(days=1)
reverse = True
while True:
now += one_day
s = str(now).replace('-', '')
if reverse and s[::] == s[::-1]:
print(s)
reverse = False
# ABABBABA
if s[0] == s[2] == s[5] == s[7] and s[1] == s[3] == s[4] == s[6]:
print(s)
break

View File

@@ -0,0 +1,20 @@
def bubble_sort(arr):
n = len(arr)
count = 0
for i in range(n):
flag = True
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
flag = False
count += 1
if flag:
break
return count
s = list('jonmlkihgfedcba')
print(bubble_sort(s))

0
lanqiao/test/__init__.py Normal file
View File

0
learn_sort/__init__.py Normal file
View File

31
learn_sort/heap.py Normal file
View File

@@ -0,0 +1,31 @@
def heapify(nums, start, end):
parent = start
son = parent * 2 + 1
while son <= end:
if son + 1 <= end and nums[son] < nums[son + 1]:
son += 1
if nums[parent] > nums[son]:
return
else:
nums[parent], nums[son] = nums[son], nums[parent]
parent = son
son = parent * 2 + 1
def heap_sort(nums):
n = len(nums)
for i in range(n // 2 - 1, -1, -1):
heapify(nums, i, n - 1)
print(nums)
for i in range(n - 1, 0, -1):
nums[0], nums[i] = nums[i], nums[0]
heapify(nums, 0, i - 1)
print(nums)
if __name__ == '__main__':
# https://leetcode.cn/problems/sort-an-array/description/
_nums = [7, 10, 13, 15, 4, 20, 19, 8]
heap_sort(_nums)
print(_nums)

21
luogu/P1216/main.py Normal file
View File

@@ -0,0 +1,21 @@
import io
import sys
sys.stdin = io.StringIO('''5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5''')
n = int(input())
nums = []
for i in range(n):
nums.append(list(map(int, input().split())))
f = [[0] * (n + 1) for _ in range(n + 1)]
for i in range(n - 1, -1, -1):
for j in range(i + 1):
f[i][j] = max(f[i + 1][j], f[i + 1][j + 1]) + nums[i][j]
print(f[0][0])

0
luogu/__init__.py Normal file
View File

View File

@@ -0,0 +1,2 @@
if __name__ == '__main__':
pass

View File

@@ -0,0 +1,29 @@
import io
import sys
sys.stdin = io.StringIO('''5
5 3 2 8
100 101 102 105
3 2 1 100000000
10 20 15 14
101 101 101 3''')
def fun(a, b, c, n):
total = a + b + c + n
if total % 3 != 0:
return False
avg = total // 3
if avg - a < 0 or avg - b < 0 or avg - c < 0:
return False
return True
t = int(input())
for _ in range(t):
print('YES' if fun(*map(int, input().split())) else 'NO')
if __name__ == '__main__':
pass

View File

@@ -0,0 +1,25 @@
import io
import sys
sys.stdin = io.StringIO('''4
100 98
42 32
1000000000000000000 1
41 40''')
# 数论基础 唯一分解定理(算术基本定理)
# x - n*p = y
# 则 n*p = x - y = z
# 大于1的整数就满足 可分解为任意素数乘积
def fun(x, y):
z = x - y
return z > 1
t = int(input())
for _ in range(t):
print('YES' if fun(*map(int, input().split())) else 'NO')
if __name__ == '__main__':
pass

View File

@@ -0,0 +1,41 @@
import io
import sys
sys.stdin = io.StringIO('''3
6
4 5 1 3 2 6
5
5 3 1 2 4
4
1 4 3 2''')
def fun(n, loc):
ans = [0] * n
ans[0] = 1
l, r = loc[1], loc[1]
for i in range(2, n + 1):
l = min(l, loc[i])
r = max(r, loc[i])
if r - l + 1 == i:
ans[i - 1] = 1
print(''.join(map(str, ans)))
t = int(input())
for _ in range(t):
n = int(input())
p = []
loc = [0] * (n + 1)
nums = input().split()
for i in range(n):
num = int(nums[i])
p.append(num)
loc[num] = i
fun(n, loc)
if __name__ == '__main__':
pass

View File

9
main.py Normal file
View File

@@ -0,0 +1,9 @@
import io
import sys
sys.stdin = io.StringIO('''5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5''')

0
utils/__init__.py Normal file
View File

15
utils/cal_prime.py Normal file
View File

@@ -0,0 +1,15 @@
def cal_prime(n):
prime = [1] * (n + 1)
prime[0] = prime[1] = 0
for i in range(2, n + 1):
j = i * i
while j <= n:
prime[j] = 0
j += i
res = []
for idx, num in enumerate(prime):
if num:
res.append(idx)
return res