AtCoder Beginner Contest 182 题解
程序员文章站
2022-07-15 11:13:44
...
A - twiblr
直接输出2A+100-B即可,时间复杂度O(1)。
a, b = map(int, input().split())
print(2 * a + 100 - b)
Problem B - Almost GCD
暴力穷举即可。
IA = lambda: map(int, input().split())
N = 1005
cnt = [0 for i in range(N)]
n = int(input())
a = list(IA())
maxx = -1
res = -1
for it in a:
for i in range(2, it + 1):
if it % i == 0:
cnt[i] += 1
if maxx < cnt[i]:
maxx = cnt[i]
res = i
print(res)
Problem C - To 3
题意:
题解
利用一个数能被3整除当且仅当其各位之和sum能被3整除。
- 如果sum本身能被3整除,则不需要删除。
- 否则统计原数的每一位数%3后的个数,比较%3 =1与%3 =2 的个数,有两种方法可以使其sum变为 %3 =0:
- %3=1 与%3=2,相互抵消,还剩下的差值即为答案。
- %3=1 与%3=2,先内部消化,%3 =1的 三个一消除,%3 =2的三个一消除,最后再相互抵消,差值即为答案。
IA = lambda: map(int, input().strip().split())
s = input()
n = len(s)
num = [0 for i in range(3)]
summ = 0
for it in s:
x = int(it)
num[x % 3] += 1
summ = (summ + x) % 3
if summ % 3 == 0:
print(0)
else:
# print(num)
cha = abs(num[1] - num[2])
num[1] %= 3
num[2] %= 3
cha = min(cha, abs(num[1] - num[2]))
if cha == n:
print(-1)
else:
print(cha)
Problem D - Wandering
模拟即可。
记录最远位置maxx,当前位置res,前缀和sum,以及前缀和的最大值r。
在每一轮中,首先更新前缀和,然后更新前缀和的最大值,本轮能达到的最大值显然是res+r,用其更新maxx,再用res+sum更新res。
IA = lambda: map(int, input().strip().split())
n = int(input())
a = list(IA())
summ = 0
res = 0
maxx = res
r = -1e9
for i in range(n):
r = max(summ, r)
maxx = max(maxx, res + r)
summ += a[i]
res += summ
maxx = max(maxx, res)
print(maxx)
Problem E - Akari
题意
题解
将所有灯和墙都放到矩形中,然后逐行从左到右扫描一遍,再从右到左扫描一遍;逐列从上到下扫描一遍,再从下到上扫描一遍。最后统计亮着的格子即可。
时间复杂度O(HW)。
IA = lambda: map(int, input().strip().split())
h, w, n, m = map(int, input().split())
field = [[0] * w for _ in range(h)]
for _ in range(n):
a, b = map(int, input().split())
field[a - 1][b - 1] = 1
for _ in range(m):
a, b = map(int, input().split())
field[a - 1][b - 1] = -1
# for l in field:
# print(l)
# print()
for i in range(h):
for j in range(1, w):
if (field[i][j - 1] == 1 or field[i][j - 1] == 2) and field[i][j] == 0:
field[i][j] = 2
for j in range(w - 2, -1, -1):
if (field[i][j + 1] == 1 or field[i][j + 1] == 2) and field[i][j] == 0:
field[i][j] = 2
for j in range(w):
for i in range(1, h):
if (field[i - 1][j] == 1
or field[i - 1][j] == 3) and field[i][j] != -1:
field[i][j] = 3
for i in range(h - 2, -1, -1):
if (field[i + 1][j] == 1
or field[i + 1][j] == 3) and field[i][j] != -1:
field[i][j] = 3
ans = 0
for l in field:
ans += l.count(1)
ans += l.count(2)
ans += l.count(3)
# print(l)
print(ans)
python超时,但C++稳过
IA = lambda: map(int, input().strip().split())
H, W, n, m = IA()
a = [[0] * (W + 1) for i in range(H + 1)]
for i in range(n):
x, y = IA()
a[x][y] = 1
for i in range(m):
x, y = IA()
a[x][y] = -1
for i in range(1, H + 1):
light = False
for j in range(1, W + 1):
if a[i][j] == 1:
light = True
elif a[i][j] == -1:
light = False
elif light:
a[i][j] = 2
light = False
for j in range(W, 0, -1):
if a[i][j] == 1:
light = True
elif a[i][j] == -1:
light = False
elif light:
a[i][j] = 2
for j in range(1, W + 1):
light = False
for i in range(1, H + 1):
if a[i][j] == 1:
light = True
elif a[i][j] == -1:
light = False
elif light:
a[i][j] = 2
light = False
for i in range(H, 0, -1):
if a[i][j] == 1:
light = True
elif a[i][j] == -1:
light = False
elif light:
a[i][j] = 2
res = 0
for i in range(1, H + 1):
for j in range(1, W + 1):
# print(a[i][j], end=" ")
res += a[i][j] > 0
# print()
print(res)
F – Valid payments
题意
推荐阅读
-
AtCoder Beginner Contest 173(E 思维模拟 F 容斥 思维题 )
-
AtCoder Beginner Contest 163 D - Sum of Large Numbers(递推&找规律)
-
AtCoder Beginner Contest 182----C. To 3
-
AtCoder Beginner Contest 182----E. Akari
-
AtCoder Beginner Contest 182 题解
-
AtCoder Beginner Contest 182----D. Wandering
-
【Atcoder】Atcoder Beginner Contest 143
-
AtCoder Beginner Contest 176 D-Wizard in Maze(双端队列)
-
AtCoder Beginner Contest 176
-
AtCoder Beginner Contest 176总结