#30411: 請問有甚麼可以不memory error 有可以對R做二分搜的方法


u11031107 (立波器)

學校 : 臺北市立麗山高級中學
編號 : 175270
來源 : [1.169.72.252]
最後登入時間 :
2023-10-22 20:52:04
c575. APCS 2017-0304-4基地台 -- 2017年3月APCS | From: [1.34.205.12] | 發表日期 : 2022-05-19 21:45

def iscovered(R):
    #stack
    global K,inf
    q=inf.copy()
    times=K
    tag=False
    while times>0:
        #print(q)
        if q==[]:
            break
        target=q[0]+R
        while q[0]<=target:#並不是O(n*k)
            q.pop(0)
            if q==[]:
                break
        times-=1
        #print(q)
    if len(q)>=1:
        return False
    else:
        return True
x=list(map(int,input().split()))
N=x[0]
K=x[1]
inf=list(map(int,input().split()))
inf.sort()
a=[i for i in range(1,(inf[-1]-inf[0]+1)]
right=len(a)-1
left=0
#抓出可以和不可以的交界
goodR=inf[-1]-inf[0]
#print(a)
while right>=left:
  #print(inf,K,sep=" ")
  mid=(left+right)//2
  #print(a[mid])
  if iscovered(a[mid])==True:
    goodR=a[mid]
    right=mid-1
  else:
    left=mid+1
print(goodR)
 
 
 
 
 
這行發生memoryerror    a=[i for i in range(1,(inf[-1]-inf[0]+1)]
 
#30446: Re: 請問有甚麼可以不memory error 有可以對R做二分搜的方法


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [101.136.203.77]
最後登入時間 :
2024-04-07 15:34:14
c575. APCS 2017-0304-4基地台 -- 2017年3月APCS | From: [59.115.17.102] | 發表日期 : 2022-05-21 23:32

def iscovered(R):
    #stack
    global K,inf
    q=inf.copy()
    times=K
    tag=False
    while times>0:
        #print(q)
        if q==[]:
            break
        target=q[0]+R
        while q[0]<=target:#並不是O(n*k)
            q.pop(0)
            if q==[]:
                break
        times-=1
        #print(q)
    if len(q)>=1:
        return False
    else:
        return True
x=list(map(int,input().split()))
N=x[0]
K=x[1]
inf=list(map(int,input().split()))
inf.sort()
a=[i for i in range(1,(inf[-1]-inf[0]+1)]
right=len(a)-1
left=0
#抓出可以和不可以的交界
goodR=inf[-1]-inf[0]
#print(a)
while right>=left:
  #print(inf,K,sep=" ")
  mid=(left+right)//2
  #print(a[mid])
  if iscovered(a[mid])==True:
    goodR=a[mid]
    right=mid-1
  else:
    left=mid+1
print(goodR)
 
 
 
 
 
這行發生memoryerror    a=[i for i in range(1,(inf[-1]-inf[0]+1)]


你的a是多餘的,後面a[mid]直接改用mid+1就好了

 
ZeroJudge Forum