Compare commits
8 Commits
2aa12fbb6e
...
master
Author | SHA1 | Date | |
---|---|---|---|
f014389281 | |||
94021b3455 | |||
4d1c595a6b | |||
f674b758aa | |||
ab7ad7c09e | |||
7d65b8c204 | |||
fe1a61e369 | |||
6b40f708b2 |
7
.idea/Algorithm.iml
generated
7
.idea/Algorithm.iml
generated
@@ -1,8 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<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>
|
1
.idea/inspectionProfiles/profiles_settings.xml
generated
1
.idea/inspectionProfiles/profiles_settings.xml
generated
@@ -1,5 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="PROJECT_PROFILE" value="Default" />
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
|
5
.idea/misc.xml
generated
5
.idea/misc.xml
generated
@@ -1,4 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
|
||||
<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>
|
0
ct/__init__.py
Normal file
0
ct/__init__.py
Normal file
17
ct/l1/8-平衡数.py
Normal file
17
ct/l1/8-平衡数.py
Normal 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
24
ct/l1/星之卡比.py
Normal 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
19
ct/l1/求阶乘位数.py
Normal 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
15
ct/l2/14-Ruby和薯条.py
Normal 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
19
ct/l2/15-小红无敌.py
Normal 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)
|
22
ct/l2/16-优美字符串.py
Normal file
22
ct/l2/16-优美字符串.py
Normal 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))
|
33
ct/l2/17-重排字符串.py
Normal file
33
ct/l2/17-重排字符串.py
Normal 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)
|
22
ct/l2/18-指定值倍数取数.py
Normal file
22
ct/l2/18-指定值倍数取数.py
Normal 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])
|
16
ct/l2/19-寻找公倍数和公约数.py
Normal file
16
ct/l2/19-寻找公倍数和公约数.py
Normal 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)
|
27
ct/l2/20-Ranko的手表.py
Normal file
27
ct/l2/20-Ranko的手表.py
Normal 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)
|
13
ct/l2/3-动态规划-不相邻取数.py
Normal file
13
ct/l2/3-动态规划-不相邻取数.py
Normal 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
0
ct/l2/__init__.py
Normal file
13
ct/l2/两数乘积k.py
Normal file
13
ct/l2/两数乘积k.py
Normal 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
12
ct/l2/回文.py
Normal 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
44
ct/l2/打家劫舍3.py
Normal 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
0
ct/main.py
Normal file
0
learn_sort/__init__.py
Normal file
0
learn_sort/__init__.py
Normal file
31
learn_sort/heap.py
Normal file
31
learn_sort/heap.py
Normal 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
21
luogu/P1216/main.py
Normal 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
0
luogu/__init__.py
Normal file
2
luogu/njit-baidu2023/main.py
Normal file
2
luogu/njit-baidu2023/main.py
Normal file
@@ -0,0 +1,2 @@
|
||||
if __name__ == '__main__':
|
||||
pass
|
29
luogu/njit-baidu2023/ta.py
Normal file
29
luogu/njit-baidu2023/ta.py
Normal 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
|
25
luogu/njit-baidu2023/tb.py
Normal file
25
luogu/njit-baidu2023/tb.py
Normal 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
|
41
luogu/njit-baidu2023/tc.py
Normal file
41
luogu/njit-baidu2023/tc.py
Normal 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
|
0
luogu/njit-baidu2023/td.py
Normal file
0
luogu/njit-baidu2023/td.py
Normal file
Reference in New Issue
Block a user