n,m=list(map(int,input().split())) lst=list(map(int,input().split())) for _ inrange(m): q=int(input()) l=0 r=n-1 while l<r: mid=(l+r)>>1 if lst[mid]>=q: r=mid else: l=mid+1 if lst[l]!=q: print("-1 -1") else: print(l,end=' ') l=0 r=n-1 while l<r: mid=(r+l+1)>>1 if lst[mid]>q: r=mid-1 else: l=mid print(l) # -------- import bisect n,m=list(map(int,input().split()))
lst=list(map(int,input().split()))
for _ inrange(m): q=int(input()) l=bisect.bisect_left(lst,q) r=bisect.bisect_right(lst,q) if l>=n or r<=0: print("-1 -1") elif r!=0and lst[r-1]!=q: print("-1 -1") else: r-=1 print(f'{l}{r}')
前缀和
1 2
差分
1 2 3 4 5 6 7 8 9 10 11 12 13 14
n,m=map(int,input().split()) nums=list(map(int,input().split())) diff=[0]*(n+1) diff[0]=nums[0] for i inrange(1,len(nums)): diff[i]=nums[i]-nums[i-1] for i inrange(m): a,b,c=map(int,input().split()) diff[a-1]+=c diff[b]-=c temp=0 for i inrange(n): temp+=diff[i] print(temp,end=' ')
i=0 while i<len(s): if s[i].isdigit(): temp=0 while i<len(s) and s[i].isdigit(): temp=temp*10+int(s[i]) i+=1 num_stack.append(temp) continue elif s[i]=='(': op_stack.append('(') elif s[i]==')': while op_stack[-1]!='(': eval() op_stack.pop() else: while op_stack and op_stack[-1]!='('and pro[s[i]]<=pro[op_stack[-1]]: eval() op_stack.append(s[i]) i+=1
while op_stack: eval() print(num_stack[0])
队列
单调栈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
n=int(input())
lst=list(map(int,input().split()))
stack=[lst[0]] ans=[-1]
for i inrange(1,len(lst)): while stack and stack[-1]>=lst[i]: stack.pop() ans.append(stack[-1] if stack else -1) stack.append(lst[i]) for i in ans: print(i,end=' ')
from collections import deque n,k=map(int,input().split()) lst=list(map(int,input().split()))
ans=[] dq=deque() ans2=[] dq2=deque() for i inrange(n): while (dq and lst[dq[-1]]<lst[i]): dq.pop() while dq and i-dq[0]>=k: dq.popleft() dq.append(i) ans.append(lst[dq[0]]) for i inrange(n): while (dq2 and lst[dq2[-1]]>lst[i]): dq2.pop() while dq2 and i-dq2[0]>=k: dq2.popleft() dq2.append(i) ans2.append(lst[dq2[0]]) for item in ans2[k-1:]: print(item ,end=' ') print() for item in ans[k-1:]: print(item ,end=' ')
tire=[[0]*2for i inrange(3000000)] count=0 definsert(x): p=0 for i inrange(32,-1,-1): t=(x>>i)&1 ifnot tire[p][t]: global count count+=1 tire[p][t]=count p=tire[p][t] deffind(x): p=0 res=0 for i inrange(32,-1,-1): t=(x>>i)&1 if tire[p][not t]: res+=(1<<i) p=tire[p][not t] else: p=tire[p][t] return res ans=-1 for i in lst: insert(i) ans=max(ans,find(i)) print(ans)
for i inrange(1,n+1): p[i]=(p[i-1]*seed)%Q h[i]=(h[i-1]*seed+ord(s[i]))%Q while m: m-=1 a,b,x,y=map(int,sys.stdin.readline().split()) if get(a,b)==get(x,y): print("Yes") else: print("No")
from collections import defaultdict,deque graph=defaultdict(list) degr=[0]*(n+1) for i inrange(m): a,b=map(int,input().split()) graph[a].append(b) degr[b]+=1
deftopsort(): ans=[] dq=deque() for i inrange(1,n+1): if degr[i]==0: dq.append(i) while dq: cur = dq.pop() ans.append(cur) for neigh in graph.get(cur,[]): degr[neigh]-=1 if degr[neigh]==0: dq.append(neigh) return ans iflen(ans)==n elseNone
ans=topsort() if ans: for i in ans: print(i,end=' ') else: print(-1)
from collections import defaultdict import heapq n,m=list(map(int,input().split()))
graph=[[-1]*(n+1) for _ inrange(n+1)]
for i inrange(m): a,b,c=list(map(int,input().split())) graph[a-1][b-1]=c if graph[a-1][b-1]==-1elsemin(graph[a-1][b-1],c)
defdijkstra():
dis=[float('inf')]*(n) dis[0]=0 visited=set() min_heap=[(0,0)] # 依次确定n个点的距离 for i inrange(n): # 没有可达的点了 iflen(min_heap)==0: break # 未确定点最近的一个 _,min_index=heapq.heappop(min_heap) visited.add(min_index) # 寻找邻居 for v inrange(n): # 可达且未访问 if v notin visited and graph[min_index][v]>0: new_dis=dis[min_index]+graph[min_index][v] # 更新 if dis[v]>new_dis: dis[v]=new_dis heapq.heappush(min_heap,(dis[v],v)) return dis d=dijkstra() print(d[n-1] if d[n-1]!=float('inf') else -1)
from collections import defaultdict import heapq import sys n,m=map(int,input().split())
graph=defaultdict(list)
for i inrange(m): a,b,c=map(int,sys.stdin.readline().split()) graph[a-1].append((b-1,c)) defdijkstra():
dis=[float('inf')]*n dis[0]=0 visit=set() min_heap=[(0,0)] whilelen(min_heap): _,node=heapq.heappop(min_heap) if node in visit:continue visit.add(node) for v,d in graph.get(node,[]): new_dis=dis[node]+d if dis[v]>new_dis: dis[v]=new_dis heapq.heappush(min_heap,(dis[v],v)) return dis d=dijkstra() print(d[n-1] if d[n-1]!=float('inf') else -1)
from collections import defaultdict import heapq import sys n,m,k=map(int,input().split())
graph=[]
for i inrange(m): a,b,c=map(int,sys.stdin.readline().split()) graph.append((a,b,c)) defbellmen_ford(): dis=[float('inf')]*(n+1) dis[1]=0 backup=[] for _ inrange(k): backup=dis.copy() for a,b,c in graph: dis[b]=min(backup[a]+c,dis[b]) iffloat(dis[n])>float('inf')/2or dis[n]==float('inf'): return'impossible' return dis[n] ans=bellmen_ford() print(ans)
for i inrange(m): a,b,c=map(int,sys.stdin.readline().split()) graph[b][a]=min(c,graph[b][a]) graph[a][b]=graph[b][a]
for i inrange(n+1): graph[i][i]=0
defprime(): dist= [INF] * (n+1) visit= [False] * (n+1) res=0 for i inrange(n): t=-1 for j inrange(1,n+1): ifnot visit[j] and (t==-1or dist[t]>dist[j] ): t=j if i and dist[t]==INF:return INF if i:res+=dist[t] for j inrange(1,n+1): dist[j]=min(dist[j],graph[t][j]) visit[t]=True return res
ans=prime() print(ans if ans!=INF else'impossible')
n,m=map(int,input().split()) INF=float('inf') graph = [] p = [i for i inrange(n+1)] for i inrange(m): a,b,c=map(int,sys.stdin.readline().split()) graph.append((c,a,b)) graph=sorted(graph)
deffind(x): if p[x]!=x: p[x]=find(p[x]) return p[x]
defkruskal(): res=0 cnt=0 for w,a,b in graph: a=find(a) b=find(b) if a!=b: p[a]=b cnt+=1 res+=w return INF if cnt<n-1else res
defget_prime(n): count=0 isprime=[1]*(n+1) isprime[0]=isprime[1]=0 for i inrange(2,n+1): if isprime[i]: count+=1 for j inrange(2,n+1): if j*i>n: break isprime[i*j]=0 return count # 埃式筛法 defget_prime2(n): count=0 isprime=[1]*(n+1) isprime[0]=isprime[1]=0 for i inrange(2,n+1): if isprime[i]: count+=1 for j inrange(2,n+1): if j*i>n: break isprime[i*j]=0 return count # 线性筛法 defget_prime3(n):
nums=[1]*(n+1) isprime=[] for i inrange(2,n+1): if nums[i]: isprime.append(i) for j inrange(len(isprime)): if i*isprime[j]>n: break nums[i*isprime[j]]=0 if i%isprime[j]==0: break returnlen(isprime) n=int(input()) print(get_prime3(n))
defoula(): nums=[1]*(n+1) oula=[0]*(n+1) oula[1]=1 prime=[] for i inrange(2,n+1): if nums[i]: prime.append(i) oula[i]=i-1 for j inrange(len(prime)): if i*prime[j]>n: break nums[i*prime[j]]=0 if i%prime[j]==0: oula[i*prime[j]]=oula[i]*prime[j] break oula[i*prime[j]]=oula[i]*(prime[j]-1)
returnsum(oula) print(oula())
快速幂
1 2 3 4 5 6 7 8 9 10 11 12 13
n=int(input())
for _ inrange(n): a,b,c=list(map(int,input().split())) ans=1 while b: if b&1: ans*=a ans%=c a*=a a%=c b//=2 print(int(ans))
# 1<=b<=a<=2000 n=int(input()) MOD=10**9+7 C=[[0]*(2005+1) for _ inrange(2005+1)] definit(): for i inrange(2005+1): for j inrange(i+1): if j==0: C[i][j]=1 else: C[i][j]=(C[i-1][j]+C[i-1][j-1])%MOD init() for _ inrange(n): a,b=map(int,input().split()) print(C[a][b])
# 1<=b<=a<=$10^5$ n=int(input()) MOD=10**9+7 N=100010 fact=[0]*(N) infact=[0]*N defqmi(a,b,m): res=1 while b : if b&1: res=(res*a)%MOD a=(a*a)%MOD b>>=1 return res
definit(): fact[0]=infact[0]=1 for i inrange(1,N): fact[i]=(fact[i-1]*i)%MOD infact[i]=infact[i-1]*qmi(i,MOD-2,MOD)%MOD
init() for _ inrange(n): a,b=map(int,input().split()) print(fact[a]*infact[a-b]%MOD*infact[b]%MOD)
# 1<=b<=a<=$10^18$ n=int(input()) p=0 defqmi(x,q): res=1 while q: if q&1: res=res*x%p x=(x*x)%p q>>=1 return res
defC(a,b): res=1 i=1 j=a for _ inrange(1,b+1): res=(res*j)%p res=(res*qmi(i,p-2))%p i+=1 j-=1 return res
deflucas(a,b): if a<p and b<p: return C(a,b) else: return C(a%p,b%p)*lucas(a//p,b//p)%p
for _ inrange(n): a,b,p=map(int,input().split()) print(lucas(a,b))
n,m=map(int,input().split()) lst=list(map(int,input().split())) res=0 for num inrange(1,1<<m): t=1 s=0 for i inrange(m): if num>>i&1: if t*lst[i]>n: t=-1 break s+=1 t*=lst[i] if t!=-1: if s&1: res+=(n//t) else: res-=(n//t) print(res)
defsg(x): if memory[x]!=-1: return memory[x] mex=set() for i in count: if x>=i: mex.add(sg(x-i)) i=0 whileTrue: if i notin mex: memory[x]=i return memory[x] i+=1
res=0 for num in store: res^=sg(num) print('Yes'if res else'No')
weight=[0] value=[0] for _ inrange(n): a,b=map(int,input().split()) weight.append(a) value.append(b)
defdp1(): dp=[[0]*1005for i inrange(1005)] for i inrange(1,n+1): for j inrange(m+1): dp[i][j]=dp[i-1][j] if j>=weight[i]: dp[i][j]=max(dp[i-1][j],dp[i-1][j-weight[i]]+value[i]) print(dp[n][m])
defdp2(): dp=[0]*1005 for i inrange(1,n+1): for j inrange(m,-1,-1): if j>=weight[i]: dp[j]=max(dp[j],dp[j-weight[i]]+value[i]) print(dp[m]) dp2()
weight=[0] value=[0] for _ inrange(n): a,b=map(int,input().split()) weight.append(a) value.append(b)
defdp1(): dp=[[0]*1005for i inrange(1005)] for i inrange(1,n+1): for j inrange(m+1): dp[i][j]=dp[i-1][j] if j>=weight[i]: # dp[i][j]=max(dp[i-1][j],dp[i-1][j-weight[i]]+value[i],dp[i][j-weight[i]]+value[i])
weight=[0] value=[0] nums=[0] for _ inrange(n): a,b,c=map(int,input().split()) weight.append(a) value.append(b) nums.append(c)
defdp1(): dp=[[0]*1005for i inrange(1005)] for i inrange(1,n+1): for j inrange(m+1): dp[i][j]=dp[i-1][j] for k inrange(nums[i]+1): if j<k*weight[i]: break dp[i][j]=max(dp[i][j],dp[i-1][j-k*weight[i]]+k*value[i])
n,m=map(int,input().split()) from collections import defaultdict weight=defaultdict(list) value=defaultdict(list)
for i inrange(1,1+n): nums=int(input()) for j inrange(nums): a,b=map(int,input().split()) weight[i].append(a) value[i].append(b)
defdp1(): dp=[[0]*105for i inrange(10005)] for i inrange(1,1+n): for j inrange(m+1): dp[i][j]=dp[i-1][j] for k inrange(len(weight[i])): if j>=weight[i][k]:
defdp2(): dp=[0]*10005 for i inrange(1,n+1): for j inrange(m+1,-1,-1): for k inrange(len(weight[i])): if j>=weight[i][k]: dp[j]=max(dp[j],dp[j-weight[i][k]]+value[i][k]) print(dp[m]) dp2()
dp=[[0]*(m+1) for _ inrange(n+1)] for i inrange(n+1): dp[i][0]=i for j inrange(m+1): dp[0][j]=j
for i inrange(1,n+1): for j inrange(1,m+1): if s[i]==t[j]: dp[i][j]=min(dp[i-1][j-1],dp[i-1][j]+1,dp[i][j-1]+1) else: dp[i][j]=min(dp[i-1][j-1],dp[i-1][j],dp[i][j-1])+1 print(dp[n][m])
s,t=map(int,input().split()) n=int(input()) pair=[] for _ inrange(n): a,b=map(int,input().split()) if a>t or b<s: continue pair.append([a,b])
pair=sorted(pair) start=s end=float('-inf') count=1 i=0 while end<t and i <len(pair): a,b=pair[i] if a<=start: end=max(end,b) i+=1 elif a>start: if i==0: break start=end count+=1 if end>=t: break if a>end: end=float('-inf') break print(count if end>=t else -1)
区间分组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
n=int(input()) pair=[] for _ inrange(n): pair.append(list(map(int,input().split()))) pair=sorted(pair)
import heapq count=[] for i inrange(len(pair)): a,b=pair[i] iflen(count)==0or a<=count[0]: heapq.heappush(count,b) else: heapq.heappop(count) heapq.heappush(count,b)