这题用python能解么?
2026-02-21 14:26:29
发布于:湖北
用人会用python解么?
全部评论 2
怎么样对了吗
1周前 来自 江西
0我试试
1周前 来自 北京
0报错了,第8行
1周前 来自 北京
0这个应该没有缩进吧
5天前 来自 北京
0
import sys
from collections import deque
it = iter(sys.stdin.read().split())
T = int(next(it))
n = int(next(it))
a = [0]*(n+1)
for i in range(1,n+1):
a[i]=int(next(it))
def gt(x,y): return x[0]>y[0] or (x[0]==y[0] and x[1]>y[1])
def lt(x,y): return x[0]<y[0] or (x[0]y[0] and x[1]<y[1])
def solve():
q1=deque((a[i],i) for i in range(1,n+1))
q2=deque()
while True:
if not q1: mx=q2.pop();mn=q2.popleft()
elif not q2: mx=q1.pop();mn=q1.popleft()
else:
m1=q1[-1] if q1 else (-1,-1)
m2=q2[-1] if q2 else (-1,-1)
mx=q1.pop() if gt(m1,m2) else q2.pop()
m1=q1[0] if q1 else (1e18,1e18)
m2=q2[0] if q2 else (1e18,1e18)
mn=q1.popleft() if lt(m1,m2) else q2.popleft()
new=(mx[0]-mn[0],mx[1])
if not q1:
q2.appendleft(new)
continue
m1=q1[0] if q1 else (1e18,1e18)
m2=q2[0] if q2 else (1e18,1e18)
cur= m1 if lt(m1,m2) else m2
if lt(new,cur):
cnt=len(q1)+len(q2)+1
while True:
cnt-=1
if cnt1: return 1
if not q1: mx=q2.pop();mn=q2.popleft()
elif not q2: mx=q1.pop();mn=q1.popleft()
else:
m1=q1[-1] if q1 else (-1,-1)
m2=q2[-1] if q2 else (-1,-1)
mx=q1.pop() if gt(m1,m2) else q2.pop()
m1=q1[0] if q1 else (1e18,1e18)
m2=q2[0] if q2 else (1e18,1e18)
mn=q1.popleft() if lt(m1,m2) else q2.popleft()
new=(mx[0]-mn[0],mx[1])
if q1 and q2:
m1=q1[0];m2=q2[0]
cur=m1 if lt(m1,m2) else m2
elif q1: cur=q1[0]
else: cur=q2[0] if q2 else (1e18,1e18)
if lt(new,cur): q2.appendleft(new)
else: return cnt
else:
q2.appendleft(new)
ans=[str(solve())]
for _ i1周前 来自 江西
0IA
3天前 来自 浙江
0













有帮助,赞一个