目录
1.归并排序
def mergesort(left,right):
if left>=right:
return
mid=left+right>>1
mergesort(left,mid)
mergesort(mid+1,right)
new=[]
i,j=left,mid+1
while i<=mid and j<=right:
if martix[i]<martix[j]:
new.append(martix[i])
i+=1
else:
new.append(martix[j])
j+=1
while i<=mid:
new.append(martix[i])
i+=1
while j<=right:
new.append(martix[j])
j+=1
for i in range(left,right+1):
martix[i]=new[i-left]
martix=[1,5,2,3,6,9,3,2]
mergesort(0,len(martix)-1)
print(martix)
2.1219. 移动距离 - AcWing题库
w,m,n=map(int,input().split())
row1=(m-1)//w
col1=-1
if row1%2==0:
col1=(m-1)%w
else:
col1=w-(m-1)%w-1
row2=(n-1)//w
if row2%2==0:
col2=(n-1)%w
else:
col2=w-(n-1)%w-1
print(abs(row1-row2)+abs(col1-col2))
3.1229. 日期问题 - AcWing题库
def getdate(year):
f1=int(year*1e6+a*1e4+b*1e2+c)
f2=int(year*1e6+c*1e4+a*1e2+b)
f3=int(year*1e6+c*1e4+b*1e2+a)
return [f1,f2,f3]
monthday=[0,31,28,31,30,31,30,31,31,30,31,30,31]
def isvalid(date):
if date<start or date>end:
return False
year=date//10000
month=(date%10000)//100
day=(date%10000)%100
if month<1 or day<1 or month>12:
return False
flag=(year%400==0) or (year%4==0 and year%100!=0)
if not flag:
if day>monthday[month]:
return False
else:
if month!=2:
if day>monthday[month]:
return False
if month==2:
if day>monthday[month]+1:
return False
return True
def outs(date):
date=str(date)
print(date[:4]+'-'+date[4:6]+'-'+date[6:8])
return
date=input()
a=int(date[:2])
b=int(date[3:5])
c=int(date[6:8])
start=19600101
end=20591231
martix1=[]
martix1.append(getdate(20))
martix1.append(getdate(19))
for i in martix1:
i.sort()
i=set(i)
for j in i:
if isvalid(j):
outs(j)
4.1231. 航班时间 - AcWing题库
这个航班。。。
n=int(input())
def outs(timetable):
flag=0
if len(timetable)==3:
flag=int(timetable[2][2])
goh,gom,gos=map(int,timetable[0].split(':'))
backh,backm,backs=map(int,timetable[1].split(':'))
time1=goh*3600+gom*60+gos
time2=backh*3600+backm*60+backs
change=time2-time1
if flag:
change+=24*60*60*flag
return change
for _ in range(n):
go=input().split()
back=input().split()
change=(outs(go)+outs(back))//2
print("{:02d}:{:02d}:{:02d}".format(change//3600,(change%3600)//60,(change%3600)%60))
5.1241. 外卖店优先级 - AcWing题库
神奇的python,龟速
n,m,t=map(int,input().split())
inque=[False for i in range(n+1)]
nums=[0 for i in range(n+1)]
nobuy=[0 for i in range(n+1)]
timetable=[]
for i in range(m):
timetable.append(list(map(int,input().split())))
timetable.sort(key=lambda x:(x[0],x[1]))
start=0
def change(indextable):
for index in range(1,n+1):
for index in indextable:
nums[index]+=2
if nums[index]>5:
inque[index]=True
for i in range(1,n+1):
if i in indextable:continue
if nums[i]>0:
nums[i]-=1
if nums[i]<=3:
inque[i]=False
for i in range(1,t+1):
sets=[]
for j in range(start,len(timetable)):
if timetable[j][0]==i:
sets.append(timetable[j][1])
start+=1
else:
break
change(sets)
print(inque.count(True))
n,m,t=map(int,input().split())
inque=[False for i in range(n+1)]
nums=[0 for i in range(n+1)]
nobuy=[0 for i in range(n+1)]
timetable=[]
for i in range(m):
timetable.append(list(map(int,input().split())))
timetable.sort(key=lambda x:(x[0],x[1]))
start=0
def change(indextable):
for index in range(1,n+1):
if index in indextable:
nums[index]-=nobuy[index]
if nums[index]<0:
nums[index]=0
nobuy[index]=0
nums[index] += 2*(indextable.count(index))
if nums[index] > 5:
inque[index] = True
if nums[index]<=3:
inque[index]=False
if index not in indextable:
nobuy[index]+=1
# for index in indextable:
# nums[index]+=2
# if nums[index]>5:
# inque[index]=True
# for i in range(1,n+1):
# if i in indextable:continue
# if nums[i]>0:
# nums[i]-=1
# if nums[i]<=3:
# inque[i]=False
for i in range(1,t+1):
sets=[]
for j in range(start,len(timetable)):
if timetable[j][0]==i:
sets.append(timetable[j][1])
start+=1
else:
break
change(sets)
if i==t:
for i in range(1,n+1):
if nobuy[i]!=0:
nums[i]-=nobuy[i]
if nums[i]<=3:
inque[i]=False
print(inque.count(True))
6.1264. 动态求连续区间和 - AcWing题库
def lowbit(x):
return x&-x
def add(x,v):
i=x
while i<=n:
tree[i] += v
i+=lowbit(i)
def query(x):
res=0
i=x
while i>0:
res+=tree[i]
i-=lowbit(i)
return res
n,m=map(int,input().split())
a=[0]
a+=list(map(int,input().split()))
tree=[0 for i in range(len(a))]
for i in range(1,len(a)):
add(i,a[i])
for _ in range(m):
k,x,y=map(int,input().split())
if k==0:
print(query(y)-query(x-1))
else:
add(x,y)
7.1265. 数星星 - AcWing题库
日常python超时
def lowbit(x):
return x&-x
def add(x,v):
i=x
while i<=32000:
tree[i]+=v
i+=lowbit(i)
return
def query(x):
res=0
i=x
while i>0:
res+=tree[i]
i-=lowbit(i)
return res
n=int(input())
tree=[0 for i in range(32000+1)]
nums=[0 for i in range(n)]
for _ in range(n):
x,y=map(int,input().split())
# x+=1
nums[query(x)]+=1
add(x,1)
for i in nums:
print(i)
8.1264. 动态求连续区间和 - AcWing题库
线段树模板,python太慢了
def pushup(u):
tree[u][0]=tree[u<<1][0]+tree[u<<1|1][0]
return
def build(u,l,r):
if l==r:
tree[u]=[w[l],0,0]
else:
tree[u]=[0,l,r]
mid=l+r>>1
build(u<<1,l,mid)
build(u<<1|1,mid+1,r)
pushup(u)
def query(u,l,r):
if tree[u][1]>=l and tree[u][2]<=r:
return tree[u][0]
mid=tree[u][1]+tree[u][2]>>1
sums=0
if l<=mid:
sums=query(u<<1,l,r)
if r>mid:
sums+=query(u<<1|1,l,r)
return sums
def modify(u,x,v):
if tree[u][1]==tree[u][2]:
tree[u][0]+=v
else:
mid=tree[u][1]+tree[u][2]>>1
if x<=mid:
modify(u<<1,x,v)
else:
modify(u<<1|1,x,v)
pushup(u)
N=100010
tree=[[]for i in range(4*N)]
n,m=map(int,input().split())
w=[0]
w+=list(map(int,input().split()))
build(1,1,n)
for _ in range(m):
k,a,b=map(int,input().split())
if k==0:
print(query(1,a,b))
else:
modify(1,a,b)