Skip to content

Commit b9c1fef

Browse files
AtCoder Beginner Contest 337
1 parent d78f2a5 commit b9c1fef

File tree

13 files changed

+436
-0
lines changed

13 files changed

+436
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
3+
# region fastio
4+
input = lambda: sys.stdin.readline().rstrip()
5+
sint = lambda: int(input())
6+
mint = lambda: map(int, input().split())
7+
ints = lambda: list(map(int, input().split()))
8+
# endregion fastio
9+
10+
# MOD = 998_244_353
11+
# MOD = 10 ** 9 + 7
12+
# DIR = ((-1, 0), (0, 1), (1, 0), (0, -1))
13+
# DIR8 = ((-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1))
14+
15+
16+
def solve() -> None:
17+
n = sint()
18+
a = b = 0
19+
for _ in range(n):
20+
x, y = mint()
21+
a += x
22+
b += y
23+
print("Takahashi" if a > b else "Aoki" if a < b else "Draw")
24+
25+
26+
solve()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
3+
# region fastio
4+
input = lambda: sys.stdin.readline().rstrip()
5+
sint = lambda: int(input())
6+
mint = lambda: map(int, input().split())
7+
ints = lambda: list(map(int, input().split()))
8+
# endregion fastio
9+
10+
# MOD = 998_244_353
11+
# MOD = 10 ** 9 + 7
12+
# DIR = ((-1, 0), (0, 1), (1, 0), (0, -1))
13+
# DIR8 = ((-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1))
14+
15+
16+
def solve() -> None:
17+
s = input()
18+
i, j = -1, len(s)
19+
while i < len(s) - 1 and s[i + 1] == 'A':
20+
i += 1
21+
while j > 0 and s[j - 1] == 'C':
22+
j -= 1
23+
print("Yes" if s.count('B') == j - i - 1 else "No")
24+
25+
26+
solve()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sys
2+
3+
# region fastio
4+
input = lambda: sys.stdin.readline().rstrip()
5+
sint = lambda: int(input())
6+
mint = lambda: map(int, input().split())
7+
ints = lambda: list(map(int, input().split()))
8+
# endregion fastio
9+
10+
# MOD = 998_244_353
11+
# MOD = 10 ** 9 + 7
12+
# DIR = ((-1, 0), (0, 1), (1, 0), (0, -1))
13+
# DIR8 = ((-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1))
14+
15+
16+
def solve() -> None:
17+
n = sint()
18+
nums = ints()
19+
d = {x:i for i, x in enumerate(nums, 1)}
20+
ans = []
21+
last = -1
22+
while last in d:
23+
last = d[last]
24+
ans.append(last)
25+
print(*ans)
26+
27+
28+
solve()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import math
2+
import sys
3+
4+
# region fastio
5+
input = lambda: sys.stdin.readline().rstrip()
6+
sint = lambda: int(input())
7+
mint = lambda: map(int, input().split())
8+
ints = lambda: list(map(int, input().split()))
9+
# endregion fastio
10+
11+
# MOD = 998_244_353
12+
# MOD = 10 ** 9 + 7
13+
# DIR = ((-1, 0), (0, 1), (1, 0), (0, -1))
14+
# DIR8 = ((-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1))
15+
16+
17+
def solve() -> None:
18+
n, m, k = mint()
19+
ans = math.inf
20+
g = []
21+
for _ in range(n):
22+
g.append(input())
23+
cnt_x = cnt_o = 0
24+
for i, c in enumerate(g[-1]):
25+
if c == 'x':
26+
cnt_x += 1
27+
elif c == 'o':
28+
cnt_o += 1
29+
if i >= k:
30+
if g[-1][i - k] == 'x':
31+
cnt_x -= 1
32+
elif g[-1][i - k] == 'o':
33+
cnt_o -= 1
34+
if i >= k - 1 and cnt_x == 0:
35+
ans = min(ans, k - cnt_o)
36+
for j in range(m):
37+
cnt_x = cnt_o = 0
38+
for i in range(n):
39+
if g[i][j] == 'x':
40+
cnt_x += 1
41+
elif g[i][j] == 'o':
42+
cnt_o += 1
43+
if i >= k:
44+
if g[i - k][j] == 'x':
45+
cnt_x -= 1
46+
elif g[i - k][j] == 'o':
47+
cnt_o -= 1
48+
if i >= k - 1 and cnt_x == 0:
49+
ans = min(ans, k - cnt_o)
50+
51+
print(-1 if ans == math.inf else ans)
52+
53+
54+
solve()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import sys
2+
3+
# region fastio
4+
input = lambda: sys.stdin.readline().rstrip()
5+
sint = lambda: int(input())
6+
mint = lambda: map(int, input().split())
7+
ints = lambda: list(map(int, input().split()))
8+
# endregion fastio
9+
10+
# MOD = 998_244_353
11+
# MOD = 10 ** 9 + 7
12+
# DIR = ((-1, 0), (0, 1), (1, 0), (0, -1))
13+
# DIR8 = ((-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1))
14+
15+
def solve() -> None:
16+
n = sint()
17+
k = 0
18+
x = n
19+
while x > 1:
20+
k += 1
21+
x = (x + 1) // 2
22+
qry = [[] for _ in range(k)]
23+
24+
def zz(l: int, r: int, k: int):
25+
if l == r: return
26+
mid = (l + r) // 2
27+
qry[k].extend(list(range(l, mid + 1)))
28+
zz(l, mid, k + 1)
29+
zz(mid + 1, r, k + 1)
30+
31+
zz(1, n, 0)
32+
33+
print(k, flush=True)
34+
for i in range(k):
35+
print(len(qry[i]), *qry[i], flush=True)
36+
res = input()
37+
l, r = 1, n
38+
for c in res:
39+
mid = (l + r) // 2
40+
if c == '1':
41+
r = mid
42+
else:
43+
l = mid + 1
44+
if l == r:
45+
break
46+
print(l)
47+
48+
solve()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import sys
2+
from collections import deque
3+
from heapq import *
4+
5+
# region fastio
6+
input = lambda: sys.stdin.readline().rstrip()
7+
sint = lambda: int(input())
8+
mint = lambda: map(int, input().split())
9+
ints = lambda: list(map(int, input().split()))
10+
# endregion fastio
11+
12+
# MOD = 998_244_353
13+
# MOD = 10 ** 9 + 7
14+
# DIR = ((-1, 0), (0, 1), (1, 0), (0, -1))
15+
# DIR8 = ((-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1))
16+
17+
18+
def solve() -> None:
19+
n, m, k = mint()
20+
color = ints()
21+
pos = [deque() for _ in range(n + 1)]
22+
cnt = [0] * (n + 1)
23+
last = [-1] * (n + 1)
24+
h = []
25+
s = 0
26+
for i, c in enumerate(color):
27+
cnt[c] += 1
28+
pos[c].append(i)
29+
if len(pos[c]) == 1:
30+
heappush(h, (i, 0, c))
31+
32+
for _ in range(m):
33+
i, j, c = heappop(h)
34+
if j + k >= len(pos[c]):
35+
s += len(pos[c] - j + 1)
36+
else:
37+
s += k
38+
j += k
39+
heappush(h, (pos[c][j], j, c))
40+
if not h:
41+
break
42+
43+
print(s)
44+
for i in range(0, n - 1):
45+
c = color[i]
46+
pos[c].popleft()
47+
pos[c].append(n + i)
48+
s -= min(cnt[c], k)
49+
heappush(h, (pos[c][0], c))
50+
i, c = heappop(h)
51+
s += cnt[c]
52+
print(s)
53+
54+
solve()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import sys
2+
3+
# region fastio
4+
input = lambda: sys.stdin.readline().rstrip()
5+
sint = lambda: int(input())
6+
mint = lambda: map(int, input().split())
7+
ints = lambda: list(map(int, input().split()))
8+
# endregion fastio
9+
10+
# MOD = 998_244_353
11+
# MOD = 10 ** 9 + 7
12+
# DIR4 = ((-1, 0), (0, 1), (1, 0), (0, -1)) #URDL
13+
# DIR8 = ((-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1))
14+
15+
16+
def solve() -> None:
17+
n = sint()
18+
A = input()
19+
B = input()
20+
C = input()
21+
ok = 0
22+
for a, b, c in zip(A, B, C):
23+
if (a == b and a == c) or (a != b and (a == c or b == c)):
24+
ok += 1
25+
print("YES" if ok < n else "NO")
26+
27+
28+
for _ in range(int(input())):
29+
solve()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import math
2+
import sys
3+
4+
# region fastio
5+
input = lambda: sys.stdin.readline().rstrip()
6+
sint = lambda: int(input())
7+
mint = lambda: map(int, input().split())
8+
ints = lambda: list(map(int, input().split()))
9+
# endregion fastio
10+
11+
# MOD = 998_244_353
12+
# MOD = 10 ** 9 + 7
13+
# DIR4 = ((-1, 0), (0, 1), (1, 0), (0, -1)) #URDL
14+
# DIR8 = ((-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1))
15+
16+
17+
def solve() -> None:
18+
n = sint()
19+
nums = ints()
20+
nums.sort()
21+
ans, i = 0, 1
22+
for j in range(2, n):
23+
while i < j and nums[i] < nums[j]:
24+
i += 1
25+
ans += (j - i) * i + math.comb(j - i, 2)
26+
print(ans)
27+
28+
29+
for _ in range(int(input())):
30+
solve()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import sys
2+
3+
# region fastio
4+
input = lambda: sys.stdin.readline().rstrip()
5+
sint = lambda: int(input())
6+
mint = lambda: map(int, input().split())
7+
ints = lambda: list(map(int, input().split()))
8+
# endregion fastio
9+
10+
# MOD = 998_244_353
11+
# MOD = 10 ** 9 + 7
12+
# DIR4 = ((-1, 0), (0, 1), (1, 0), (0, -1)) #URDL
13+
# DIR8 = ((-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1))
14+
15+
16+
def solve() -> None:
17+
n = sint()
18+
nums = ints()
19+
left, right = [0] * n, [0] * n
20+
right[1] = 1
21+
for i in range(2, n):
22+
if nums[i] - nums[i - 1] < nums[i - 1] - nums[i - 2]:
23+
right[i] = right[i - 1] + 1
24+
else:
25+
right[i] = right[i - 1] + nums[i] - nums[i - 1]
26+
left[-2] = 1
27+
for i in range(n - 3, -1, -1):
28+
if nums[i + 1] - nums[i] < nums[i + 2] - nums[i + 1]:
29+
left[i] = left[i + 1] + 1
30+
else:
31+
left[i] = left[i + 1] + nums[i + 1] - nums[i]
32+
33+
m = sint()
34+
for _ in range(m):
35+
x, y = mint()
36+
x -= 1
37+
y -= 1
38+
if x < y:
39+
print(right[y] - right[x])
40+
else:
41+
print(left[y] - left[x])
42+
43+
44+
for _ in range(int(input())):
45+
solve()

0 commit comments

Comments
 (0)