笔试面试/ponyAi

1
2
3
4
5
6
7
n = int(input())
nums = list(map(int, input().split()))
nums.sort()
res = 0
for i in range(1, n):
res += abs(nums[i]-nums[i-1])**2
print(res)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from collections import defaultdict


def satisfy1(s, m):
d = defaultdict(int)
n = len(s)
for c in s[:m]:
d[c] += 1
le = len(d)
exclude = set()
for i in range(m, n):
pc, c = s[i - m], s[i]
if c in exclude: continue
if pc == c: continue
d[pc] -= 1
if d[pc] == 0: exclude.add(pc)
return len(exclude) != le


def satisfy(s, m):
se = set(s[:m])
n = len(s)
for i in range(m + 1, n + 1):
st = set(s[i - m: i])
se = se.intersection(st)
if not se: return False
return True


def solve(s):
n = len(s)
l = 1
r = n // 2 + 1 if n % 2 == 1 else n // 2
if n>10000: return r
res = r
while l <= r:
m = (l + r) // 2
# print(l, r, m)
if satisfy(s, m):
res = m
r = m - 1
else:
l = m + 1
# print(l, r, res)
return res


if __name__ == '__main__':
assert satisfy("abcbe", 1) == False
assert satisfy("abcbe", 2) == True
assert solve("abcde") == 3
assert solve("abcbe") == 2
assert solve("aaaaa") == 1
s = input()
print(solve(s))
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from collections import defaultdict


class DSU:
def __init__(self, n):
self.parents = list(range(n))

def find(self, x):
if self.parents[x] != x:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]

def union(self, x, y):
px = self.find(x)
py = self.find(y)
if px != py:
self.parents[px] = py


if __name__ == '__main__':
n, m = map(int, input().split())
costs = list(map(int, input().split()))
dsu = DSU(n)
for _ in range(m):
i, j = map(int, input().split())
dsu.union(i - 1, j - 1)
d = defaultdict(int)
# parent -> mincost
for i in range(n):
p = dsu.find(i)
cur = d.get(p, float('inf'))
d[p] = min(cur, costs[i])
res = 0
for k, v in d.items():
res += v
print(res)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

mod = 998244353

def satisfy(nums):
n = len(nums)
cur = 0
while True:
if cur >= n or nums[cur] <= 0 or cur + nums[cur] >= n: break
cur = cur + nums[cur] + 1
return cur == n


def solve(nums):
n = len(nums)
if n > 10: return 0
res = 0
for i in range(1, 2 ** n):
l = [nums[j] for j in range(n) if i & (1 << j) != 0]
if satisfy(l):
res += 1
return res % mod


if __name__ == '__main__':
n = int(input())
nums = list(map(int, input().split()))
print(solve(nums))
# 推荐文章
  1.MYSQL45讲-笔记
  2.《Java业务开发常见错误100例》笔记
  3.django
  4.flutter
  5.elastic search

评论


:D 一言句子获取中...