总共9题
提示
python3.7要把cache改成lru_cache才能跑
789. 数的范围
考察二分基本概念以及老生常谈但确实要注意点:
- 记得
bisect_left
是左边第一 - 注意不存在的情况与条件
代码
py
import re,os
from io import BytesIO, IOBase
import random
import sys
from math import ceil,floor,fmod,gcd,sqrt,inf
from bisect import bisect_left
from collections import defaultdict,Counter,deque,OrderedDict
from functools import cache, reduce, cmp_to_key
from itertools import accumulate, combinations, permutations
from heapq import nsmallest, nlargest, heappushpop, heapify, heappop, heappush
from copy import deepcopy
from typing import *
from string import ascii_lowercase, ascii_uppercase
# 快读区块大小
BUFSIZE = 4096
# 判断是否本地
LOCAL="--open17" in sys.argv
# 可能会导致pypy产生TLE
# if "PyPy" in sys.version:
# import pypyjit; pypyjit.set_param('max_unroll_recursion=-1')
# 调试递归极限
limits = [100000, 10000, 5000, 2000]
for limit in limits:
try:
sys.setrecursionlimit(limit)
break
except:
continue
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def fstream(func):
def wrapper(is_local):
input_file = open('data.in', 'r', encoding='utf-8') if is_local else sys.stdin
output_file = open('data.out', 'w', encoding='utf-8') if is_local else sys.stdout
sys.stdin = IOWrapper(input_file)
sys.stdout = output_file
func()
sys.stdin = sys.__stdin__
sys.stdout = sys.__stdout__
if is_local:
input_file.close()
output_file.close()
return wrapper
input = lambda: sys.stdin.readline().rstrip("\r\n")
def I():
return input()
def II():
return int(input())
def MII():
return map(int, input().split())
def LI():
return list(input().split())
def LII():
return list(map(int, input().split()))
def for_t(func):
def wrapper():
T=II()
for _ in range(T):
func()
return wrapper
@fstream
# @for_t
def solve():
n,q=MII()
a=LII()
for _ in range(q):
p=II()
x=bisect_left(a,p)
y=bisect_left(a,p+1)-1
if x==n or a[x]!=n:
x=-1
y=-1
print(x,y)
return 0
solve(LOCAL)
790. 数的三次方根
浮点二分板子,记得保留6位的格式化输出写法print("%f"%l)
代码
py
import re,os
from io import BytesIO, IOBase
import random
import sys
from math import ceil,floor,fmod,gcd,sqrt,inf
from bisect import bisect_left
from collections import defaultdict,Counter,deque,OrderedDict
from functools import cache, reduce, cmp_to_key
from itertools import accumulate, combinations, permutations
from heapq import nsmallest, nlargest, heappushpop, heapify, heappop, heappush
from copy import deepcopy
from typing import *
from string import ascii_lowercase, ascii_uppercase
# 快读区块大小
BUFSIZE = 4096
# 判断是否本地
LOCAL="--open17" in sys.argv
# 可能会导致pypy产生TLE
# if "PyPy" in sys.version:
# import pypyjit; pypyjit.set_param('max_unroll_recursion=-1')
# 调试递归极限
limits = [100000, 10000, 5000, 2000]
for limit in limits:
try:
sys.setrecursionlimit(limit)
break
except:
continue
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def fstream(func):
def wrapper(is_local):
input_file = open('data.in', 'r', encoding='utf-8') if is_local else sys.stdin
output_file = open('data.out', 'w', encoding='utf-8') if is_local else sys.stdout
sys.stdin = IOWrapper(input_file)
sys.stdout = output_file
func()
sys.stdin = sys.__stdin__
sys.stdout = sys.__stdout__
if is_local:
input_file.close()
output_file.close()
return wrapper
input = lambda: sys.stdin.readline().rstrip("\r\n")
def I():
return input()
def II():
return int(input())
def MII():
return map(int, input().split())
def LI():
return list(input().split())
def LII():
return list(map(int, input().split()))
def for_t(func):
def wrapper():
T=II()
for _ in range(T):
func()
return wrapper
def f(x):
return x**3
@fstream
# @for_t
def solve():
n=float(input())
l=-120
r=120
while r-l>=1e-8:
mid=(l+r)/2
if f(mid)>n:
r=mid
else:
l=mid
print("%f"%l)
return 0
solve(LOCAL)
795. 前缀和
一维前缀和板子
代码
py
import sys
from math import ceil,floor,fmod,gcd,sqrt,inf
from bisect import bisect_left
from collections import defaultdict,Counter,deque
from functools import lru_cache, reduce, cmp_to_key
from itertools import accumulate, combinations, permutations
from heapq import nsmallest, nlargest, heappushpop, heapify, heappop, heappush
from copy import deepcopy
limits = [100000, 10000, 5000, 2000]
for limit in limits:
try:
sys.setrecursionlimit(limit)
break
except:
continue
input = lambda: sys.stdin.readline().rstrip("\r\n")
def I():
return input()
def II():
return int(input())
def MII():
return map(int, input().split())
def LI():
return list(input().split())
def LII():
return list(map(int, input().split()))
n,m=MII()
a=LII()
a=[0]+a # 因为python低版本不支持accumalate的设置初始值参数
b = list(accumulate(a))
for _ in range(m):
i,j=MII()
print(b[j]-b[i-1])
796. 子矩阵的和
二维前缀和板子,记得开大1格
代码
py
import re,os
from io import BytesIO, IOBase
import random
import sys
from math import ceil,floor,fmod,gcd,sqrt,inf
from bisect import bisect_left
from collections import defaultdict,Counter,deque,OrderedDict
from functools import cache, reduce, cmp_to_key
from itertools import accumulate, combinations, permutations
from heapq import nsmallest, nlargest, heappushpop, heapify, heappop, heappush
from copy import deepcopy
from typing import *
from string import ascii_lowercase, ascii_uppercase
# 快读区块大小
BUFSIZE = 4096
# 判断是否本地
LOCAL="--open17" in sys.argv
# 可能会导致pypy产生TLE
# if "PyPy" in sys.version:
# import pypyjit; pypyjit.set_param('max_unroll_recursion=-1')
# 调试递归极限
limits = [100000, 10000, 5000, 2000]
for limit in limits:
try:
sys.setrecursionlimit(limit)
break
except:
continue
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def fstream(func):
def wrapper(is_local):
input_file = open('data.in', 'r', encoding='utf-8') if is_local else sys.stdin
output_file = open('data.out', 'w', encoding='utf-8') if is_local else sys.stdout
sys.stdin = IOWrapper(input_file)
sys.stdout = output_file
func()
sys.stdin = sys.__stdin__
sys.stdout = sys.__stdout__
if is_local:
input_file.close()
output_file.close()
return wrapper
input = lambda: sys.stdin.readline().rstrip("\r\n")
def I():
return input()
def II():
return int(input())
def MII():
return map(int, input().split())
def LI():
return list(input().split())
def LII():
return list(map(int, input().split()))
def for_t(func):
def wrapper():
T=II()
for _ in range(T):
func()
return wrapper
@fstream
# @for_t
def solve():
n,m,q=MII()
a=[[0 for _ in range(m+1)]]
for _ in range(n):
a.append([0]+LII())
for i in range(1, n+1):
for j in range(1, m+1):
a[i][j] = a[i][j] + a[i-1][j] + a[i][j-1] - a[i-1][j-1]
def get(x1,y1,x2,y2):
return a[x2][y2] - a[x1 - 1][y2] - a[x2][y1 - 1] + a[x1 - 1][y1 - 1]
for _ in range(q):
x1,y1,x2,y2=MII()
print(get(x1,y1,x2,y2))
return 0
solve(LOCAL)
730. 机器人跳跃问题
题目描述有点绕,比如下面这句话:
如果 H(k+1)>E,那么机器人就失去 H(k+1)−E的能量值,否则它将得到 E−H(k+1)的能量值
其实就是都获得
转换之后,不难看出: 初始能量值E越多就越容易完成,越少就越难完成,并且结果的剩余能量也是单调的
单调性->二分答案
因此,我们只需要二分答案即可,验证显然是
代码
py
import re,os
from io import BytesIO, IOBase
import random
import sys
from math import ceil,floor,fmod,gcd,sqrt,inf
from bisect import bisect_left
from collections import defaultdict,Counter,deque,OrderedDict
from functools import cache, reduce, cmp_to_key
from itertools import accumulate, combinations, permutations
from heapq import nsmallest, nlargest, heappushpop, heapify, heappop, heappush
from copy import deepcopy
from typing import *
from string import ascii_lowercase, ascii_uppercase
# 快读区块大小
BUFSIZE = 4096
# 判断是否本地
LOCAL="--open17" in sys.argv
# 可能会导致pypy产生TLE
# if "PyPy" in sys.version:
# import pypyjit; pypyjit.set_param('max_unroll_recursion=-1')
# 调试递归极限
limits = [100000, 10000, 5000, 2000]
for limit in limits:
try:
sys.setrecursionlimit(limit)
break
except:
continue
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def fstream(func):
def wrapper(is_local):
input_file = open('data.in', 'r', encoding='utf-8') if is_local else sys.stdin
output_file = open('data.out', 'w', encoding='utf-8') if is_local else sys.stdout
sys.stdin = IOWrapper(input_file)
sys.stdout = output_file
func()
sys.stdin = sys.__stdin__
sys.stdout = sys.__stdout__
if is_local:
input_file.close()
output_file.close()
return wrapper
input = lambda: sys.stdin.readline().rstrip("\r\n")
def I():
return input()
def II():
return int(input())
def MII():
return map(int, input().split())
def LI():
return list(input().split())
def LII():
return list(map(int, input().split()))
def for_t(func):
def wrapper():
T=II()
for _ in range(T):
func()
return wrapper
@fstream
# @for_t
def solve():
n=II()
a=LII()
r=max(a)+1
l=0
def check(e):
for i in a:
e*=2
e-=i
if e<0:
return False
return True
while l<r:
mid=l+r>>1
if check(mid):
r=mid
else:
l=mid+1
print(r)
return 0
solve(LOCAL)
1221. 四平方和
一个很常见的思考策略,当只能想到枚举做法的时候,我们先尽可能降低枚举的复杂度
比如折半查找(meet in middle),利用答案单调性滑窗等
这题类似leetcode两数之和的哈希,其实也可以三数之和解法的双指针的做,也可以二分出剩下的
反正枚举一半,再枚举一半找对应
哈希代码
py
import re,os
from io import BytesIO, IOBase
import random
import sys
from math import ceil,floor,fmod,gcd,sqrt,inf
from bisect import bisect_left
from collections import defaultdict,Counter,deque,OrderedDict
from functools import cache, reduce, cmp_to_key
from itertools import accumulate, combinations, permutations
from heapq import nsmallest, nlargest, heappushpop, heapify, heappop, heappush
from copy import deepcopy
from typing import *
from string import ascii_lowercase, ascii_uppercase
# 快读区块大小
BUFSIZE = 4096
# 判断是否本地
LOCAL="--open17" in sys.argv
# 可能会导致pypy产生TLE
# if "PyPy" in sys.version:
# import pypyjit; pypyjit.set_param('max_unroll_recursion=-1')
# 调试递归极限
limits = [100000, 10000, 5000, 2000]
for limit in limits:
try:
sys.setrecursionlimit(limit)
break
except:
continue
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
def fstream(func):
def wrapper(is_local):
input_file = open('data.in', 'r', encoding='utf-8') if is_local else sys.stdin
output_file = open('data.out', 'w', encoding='utf-8') if is_local else sys.stdout
sys.stdin = IOWrapper(input_file)
sys.stdout = output_file
func()
sys.stdin = sys.__stdin__
sys.stdout = sys.__stdout__
if is_local:
input_file.close()
output_file.close()
return wrapper
input = lambda: sys.stdin.readline().rstrip("\r\n")
def I():
return input()
def II():
return int(input())
def MII():
return map(int, input().split())
def LI():
return list(input().split())
def LII():
return list(map(int, input().split()))
def for_t(func):
def wrapper():
T=II()
for _ in range(T):
func()
return wrapper
@fstream
# @for_t
def solve():
n=II()
m={}
for c in range(0,ceil(sqrt(n))+1):
for d in range(c,ceil(sqrt(n))+1):
if c*c+d*d in m.keys():
continue
m[c*c+d*d]=(c,d)
for a in range(0,ceil(sqrt(n))+1):
for b in range(a,ceil(sqrt(n))+1):
ans=n-a*a-b*b
if ans in m.keys():
print(a,b,m[ans][0],m[ans][1])
return
return 0
solve(LOCAL)
1227. 分巧克力
这种最大的最小/最小的最大,二分即可
注意check的时候应该是cnt+=(h//a)*(w//a)
而不是cnt+=h*w//(a*a)
因为这个小调试了一手,顺便写写调试经验:
- 二分答案都先检验check函数是否正确
代码
py
import sys
from math import ceil,floor,fmod,gcd,sqrt,inf
from bisect import bisect_left
from collections import defaultdict,Counter,deque
from functools import lru_cache, reduce, cmp_to_key
from itertools import accumulate, combinations, permutations
from heapq import nsmallest, nlargest, heappushpop, heapify, heappop, heappush
from copy import deepcopy
limits = [100000, 10000, 5000, 2000]
for limit in limits:
try:
sys.setrecursionlimit(limit)
break
except:
continue
input = lambda: sys.stdin.readline().rstrip("\r\n")
def I():
return input()
def II():
return int(input())
def MII():
return map(int, input().split())
def LI():
return list(input().split())
def LII():
return list(map(int, input().split()))
n,k=MII()
c=[]
l=1
r=1
for _ in range(n):
h,w=MII()
tmp=min(h,w)
r=max(tmp,r)
c.append((h,w))
r+=1
def check(a,out=False):
cnt=0
for h,w in c:
if a>h or a>w:
continue
cnt+=(h//a)*(w//a)
if out:
print(cnt,h,w)
return cnt>=k
while l<r:
mid=l+r>>1
if check(mid):
l=mid+1
else:
r=mid
# print(check(6,True))
print(r-1)
99. 激光炸弹
经典题目,注意细节
代码
py
import sys
from math import ceil,floor,fmod,gcd,sqrt,inf
from bisect import bisect_left
from collections import defaultdict,Counter,deque
from functools import lru_cache, reduce, cmp_to_key
from itertools import accumulate, combinations, permutations
from heapq import nsmallest, nlargest, heappushpop, heapify, heappop, heappush
from copy import deepcopy
limits = [100000, 10000, 5000, 2000]
for limit in limits:
try:
sys.setrecursionlimit(limit)
break
except:
continue
input = lambda: sys.stdin.readline().rstrip("\r\n")
def I():
return input()
def II():
return int(input())
def MII():
return map(int, input().split())
def LI():
return list(input().split())
def LII():
return list(map(int, input().split()))
n,r=MII()
N=5003
a=[[0] * N for _ in range(N)]
for _ in range(n):
x,y,w=MII()
a[x+1][y+1]+=w
for i in range(1, N):
for j in range(1, N):
a[i][j] = a[i][j] + a[i-1][j] + a[i][j-1] - a[i-1][j-1]
def get(x1,y1,x2,y2):
return a[x2][y2] - a[x1 - 1][y2] - a[x2][y1 - 1] + a[x1 - 1][y1 - 1]
ans=0
r=min(r,N-1)
for i in range(N):
if i+r>=N:
break
for j in range(N):
if j+r>=N:
break
ans=max(ans,get(i+1,j+1,i+r,j+r))
print(ans)
1230. K倍区间
很有意思的一道题
我们很难构造某种单调性利用双指针统计K倍区间
那么我们先考虑操作转换,看看区间转单点后怎么样
前缀和处理后现在转为求满足下面性质的点对:
即:
所以我们发现只要前缀和数组两两点对同余即可构成一个k倍区间
代码
py
import sys
from math import ceil,floor,fmod,gcd,sqrt,inf
from bisect import bisect_left
from collections import defaultdict,Counter,deque
from functools import lru_cache, reduce, cmp_to_key
from itertools import accumulate, combinations, permutations
from heapq import nsmallest, nlargest, heappushpop, heapify, heappop, heappush
from copy import deepcopy
limits = [100000, 10000, 5000, 2000]
for limit in limits:
try:
sys.setrecursionlimit(limit)
break
except:
continue
input = lambda: sys.stdin.readline().rstrip("\r\n")
def I():
return input()
def II():
return int(input())
def MII():
return map(int, input().split())
def LI():
return list(input().split())
def LII():
return list(map(int, input().split()))
n,k=MII()
# 1 3 6 10 15
a=[0]
d=defaultdict(int)
d[0]=1
ans=0
for i in range(n):
p=II()
a.append((a[-1]+p%k)%k)
ans+=d[a[-1]]
d[a[-1]]+=1
print(ans)