此题暴力判断所有2 by 2的方格是否都一样即可
from typing import *
def happy(grid:List[str])->bool:
for i in range(3):
for j in range(3):
if grid[i][j]==grid[i+1][j]==grid[i][j+1]==grid[i+1][j+1]:
return True
return False
t=int(input())
for i in range(t):
grid=[]
for j in range(4):
grid.append(input())
print("Yes" if happy(grid) else "No")
字符2既可以看作0也可以看做1,那么本题救是求最长的不是0或者1的连续字符的长度
def check(s:str,c:str)->int:
ans=0
i=0
while i<len(s):
while i<len(s) and s[i]==c:
i+=1
j=i
while j<len(s) and s[j]!=c:
j+=1
ans=max(ans,j-i)
i=j
return ans
t=int(input())
for i in range(t):
input()
s=input()
print(max(check(s,'1'),check(s,'0')))
由于lr的范围是小于2^63的,因此数字个数最多62个,暴力枚举即可,注意,0的次方只能是0和1,1的次方只能1,这俩需要特殊处理
t=int(input())
for i in range(t):
l,r,k=map(int,input().split())
arr=[]
if k==0:
if 0>=l and 0<=r:
arr.append(0)
if 1>=l and 1<=r:
arr.append(1)
if arr:
for a in arr:
print(a,end=' ')
print()
else:
print('None.')
elif k==1:
if 1>=l and 1<=r:
print(1)
else:
print('None.')
else:
for i in range(100):
a=k**i
if a>=l and a<=r:
arr.append(a)
if a>r:
break
if arr:
for a in arr:
print(a,end=' ')
print()
else:
print('None.')
记录每一列的棋子落下的位置,以及整个棋盘的棋子布局,每落下一个棋子,判断一下四个对角线是否有连续k个即可
from typing import *
def isEnd(grid:List[List[int]],i:int,j:int,k:int)->bool:
#先判断向下:
p1=i
while p1>=0 and grid[p1][j]==grid[i][j]:
p1-=1
if i-p1>=k:
return True
#判断左右:
l,r=j,j
while l>=0 and grid[i][l]==grid[i][j]:
l-=1
while grid[i][r]==grid[i][j]:
r+=1
if r-l>k:
return True
#判断对角线1:
l,r=0,0
while grid[i+l][j+l]==grid[i][j]:
l+=1
while i-r>=0 and j-r>=0 and grid[i-r][j-r]==grid[i][j]:
r+=1
if r+l>k:
return True
#判断对角线2:
l,r=0,0
while i-l>=0 and grid[i-l][j+l]==grid[i][j]:
l+=1
while j-r>=0 and grid[i+r][j-r]==grid[i][j]:
r+=1
if r+l>k:
return True
return False
n,m,k,t=map(int,input().split())
arr=list(map(int,input().split()))
count,grid=[0]*1005,[[-1] *1005 for i in range(1005)]
for i in range(t):
a=arr[i]
b=0 if i&1 else 1
grid[count[a]][a]=b
if isEnd(grid,count[a],a,k):
print(i+1)
break
count[a]+=1
其实两个弹珠碰撞后相当于各自在原地待一个单位时间,然后各自继续按照原方向走,因此只需要计算出每个想左走的弹珠的左边有几个向右的走的(也就是被拦住了几次),以及每个向右走的弹珠右边有几个想左的即可
n,m=map(int,input().split())
d=list(map(int,input().split()))
p=list(map(int,input().split()))
left,right,pos=[0]*(n+5),[0]*(n+5),[-1]*(n+5)
for i in range(m):
pos[p[i]]=d[i]
for i in range(1,n+2):
left[i]=left[i-1]
if pos[i]==1:
left[i]+=1
for i in range(n+2,-1,-1):
right[i]=right[i+1]
if pos[i]==0:
right[i]+=1
ans=0
for k in p:
if pos[k]==0:
ans=max(ans,left[k]+k)
else:
ans=max(ans,n+1-k+right[k])
print(ans)
注意到数组总和有限制,因此相同的数字可能很多,可以先统计每种数字的个数,再进行计算
import math
input()
a=list(map(int,input().split()))
b=list(map(int,input().split()))
dic1,dic2={},{}
for k in a:
if k not in dic1:
dic1[k]=1
else:
dic1[k]+=1
for k in b:
if k not in dic2:
dic2[k]=1
else:
dic2[k]+=1
ans=0
for m in dic1:
for n in dic2:
ans+=int(math.sqrt(abs(m-n)))*dic1[m]*dic2[n]
print(ans)