A. Setting up Camp
数学
a每人需要一个帐篷,然后看b,c能否使所有b都有3人帐篷住即可
from math import ceil
t = int(input())
for _ in range(t):
a, b, c = map(int, input().split())
if b % 3 == 0 or (b % 3)+c >= 3:
res = a+ceil((b+c)/3)
else:
res = -1
print(res)
B. Fireworks
x数论
之前的wa了。。每个烟花会持续一个区间,不限时间,所有情况都可能在某一时刻出现,无论是否整除都可以错开多放入区间一个
t = int(input())
for _ in range(t):
a, b, m = map(int, input().split())
res = m//a+1+m//b+1
print(res)
C. Left and Right Houses
暴力枚举
暴力枚举每个位置,记录左边0的个数和右面1的个数,只有当左边的个数超过左边现有村庄的一半时,右边同理,才可取答案,取其中离中心较小的一个,距离相同取左边,当n为奇数时,中心用浮点数表示,即n/2
from math import ceil
t = int(input())
for _ in range(t):
n = int(input())
a = input()+"x"
res = -1
z = int(1e9)
lz, ro = 0, a.count("1")
i = 0
while i < n+1:
if lz >= ceil(i/2) and ro >= ceil((n-i)/2):
if abs(n/2-i) < z:
res = i
z = abs(n/2-i)
if a[i] == "0":
lz += 1
else:
ro -= 1
i += 1
print(res)
D. Seraphim the Owl
贪心,暴力
从后往前,除最终位置外,其他位置可任取ai或bi,取其中较小的一个即可,
前缀和预处理后n个时的最少花费,遍历一遍m以内各点为终点时的取值,取较小的一个
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
mi = [min(a[i], b[i]) for i in range(n)]
s = [0]
for i in range(n-1, -1, -1):
s.append(s[-1]+mi[i])
s.reverse()
res = int(1e18)
for i in range(m):
res = min(res, a[i]+s[i+1])
print(res)
E. Binary Search
o二分,构造
数组不单调,要使最终结果为正确的值,只要将正确的值转移到最终的位置上即可,转移时不可改变原二分路径
若目标点在二分路径上,需要找到更小的点来顶替它,然后再将其移动到最终位置上
用列表存储各点是否在路径中,选择一个不在路径上的更小的点来替换目标点,终点位置因为不会被再次使用,所以也可作为替换点
当二分结果与正确结果相同时,无需进行操作
t = int(input())
for _ in range(t):
n, x = map(int, input().split())
p = [-1]+list(map(int, input().split()))
resl = []
xp = p.index(x)
st = [0]*(n+1)
l, r = 1, n+1
while r-l != 1:
m = (r+l)//2
if p[m] <= x:
st[m] = 1
l = m
else:
st[m] = 2
r = m
if l == xp:
print(0)
else:
st[l] = 0
if st[xp] == 1:
need = -1
for i in range(1, n+1):
if not st[i] and p[i] < x:
need = i
break
resl.append((need, xp))
resl.append((need, l))
else:
resl.append((xp, l))
print(len(resl))
for i, j in resl:
print(i, j)