Add 11-03-2024

This commit is contained in:
augustin64 2024-03-25 15:27:12 +01:00
parent 7af4a4864a
commit 3fc540bebe
4 changed files with 133 additions and 0 deletions

43
11-03-24/b.py Normal file
View File

@ -0,0 +1,43 @@
def main():
n = int(input())
fa = [int(i)-1 for i in input().split()]
est_feuille = [True for _ in range(n)]
for i in range(n):
if fa[i] != i:
est_feuille[fa[i]] = False
f_count = 0
for i in range(n):
if est_feuille[i]:
f_count += 1
print(f_count)
used = set()
def find_path(s):
l = []
length = 0
while s not in used:
l.append(s)
used.add(s)
length += 1
s = fa[s]
return reversed(l), length
paths = []
for i in range(n):
if est_feuille[i]:
p, n = find_path(i)
print(n)
print(" ".join([str(i+1) for i in p]))
print()
for _ in range(int(input())):
main()

53
11-03-24/c.py Normal file
View File

@ -0,0 +1,53 @@
def main():
n, m = [int(i) for i in input().split()]
data = [[a for a in input()] for _ in range(n)]
colors = set()
for i in data:
for j in i:
colors.add(j)
def voisins(i, j):
col = data[i][j]
v = [
(i-1, j),
(i+1, j),
(i, j-1),
(i, j+1)
]
return [
(a, b) for (a, b) in v
if a >= 0 and b >= 0
and a < n and b < m
and data[a][b] == col
]
vus = set()
def parcours(i, j, pere=None):
vus.add((i, j))
vois = voisins(i, j)
#print(f"voisins de {(i, j)}: {vois}")
for v_ in vois:
if v_ != pere:
if v_ in vus:
return True
vus.add(v_)
if parcours(*v_, pere=(i, j)):
return True
return False
for i in range(n):
for j in range(m):
if (i, j) not in vus:
# print(vus)
if parcours(i, j):
return True
return False
if main():
print("Yes")
else:
print("No")

25
18-03-24/a.py Normal file
View File

@ -0,0 +1,25 @@
[r1, c1, r2, c2] = [int(i) for i in input().split()]
def tower():
s = 0
if r1 != r2:
s += 1
if c1 != c2:
s += 1
return s
def rook():
if ((r1+c1)%2 != (r2+c2)%2) or (r1, c1)==(r2, c2):
return 0
s = 1
if abs(r2-r1) != abs(c2-c1):
s += 1
return s
def king():
return max(abs(r2-r1), abs(c2-c1))
print(tower(), rook(), king())

12
18-03-24/b.py Normal file
View File

@ -0,0 +1,12 @@
for i in range(int(input())):
n = int(input())
dists = sorted([int(i) for i in input().split()])
weights = 0
prev = 0
for i, w in enumerate(dists):
weights += w - prev
weights -= (i*(n-i))*(w - prev)
prev = w
print(weights)