Problem p02774 (python)

Fixed submission:
import numpy as np
N,K=map(int,input().split())
A=list(map(int,input().split()))

A=np.array(A)

A_pos=np.sort(A[A>0])
A_neg=np.sort(A[A<0])
A_neg2=-A_neg[::-1]
n_pos=len(A_pos)
n_neg=len(A_neg)
n_zero=N-n_pos-n_neg

def position_in_neg(x):
    y=n_pos-np.searchsorted(A_pos,x//A_neg,side='right')
    return y.sum()

def position_in_pos(x):
    z=np.searchsorted(A_pos,-(-x//A_pos),side='left')
    w=np.searchsorted(A_neg2,-(-x//A_neg2),side='left')
    tmp=len(A_pos[A_pos**2<x])+len(A_neg2[A_neg2**2<x])
    return (z.sum()+w.sum()-tmp)//2

def position(x):
    if x<0:
        return position_in_neg(x)
    elif x==0:
        return n_pos*n_neg
    else:
        return position_in_pos(x)+n_pos*n_neg+(n_pos+n_neg)*n_zero+n_zero*(n_zero-1)//2

l=-pow(10,18)
r=pow(10,18)
while(r-l>1):
    mid=(l+r)//2
    if position(mid)<K:
        l=mid
    else:
        r=mid
print(l)

Buggy submission (verdict: Runtime Error):
from ABC155_D2 import *



import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """4 3
3 3 -4 -2"""
        output = """-6"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """10 40
5 4 3 2 -1 0 0 0 0 0"""
        output = """6"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """30 413
-170202098 -268409015 537203564 983211703 21608710 -443999067 -937727165 -97596546 -372334013 398994917 -972141167 798607104 -949068442 -959948616 37909651 0 886627544 -20098238 0 -948955241 0 -214720580 277222296 -18897162 834475626 0 -425610555 110117526 663621752 0"""
        output = """448283280358331064"""
        self.assertIO(input, output)

if __name__ == "__main__":
    unittest.main()

Buggy submission (verdict: Runtime Error):
import numpy as np
N,K=map(int,input().split())
A=list(map(int,input().split()))

A=np.array(A)

A_pos=np.sort(A[A>0])
A_neg=np.sort(A[A<0])
A_neg2=np.flip(-A_neg)
n_pos=len(A_pos)
n_neg=len(A_neg)
n_zero=N-n_pos-n_neg

def position_in_neg(x):
    y=n_pos-np.searchsorted(A_pos,x//A_neg,side='right')
    return y.sum()

def position_in_pos(x):
    z=np.searchsorted(A_pos,-(-x//A_pos),side='left')
    w=np.searchsorted(A_neg2,-(-x//A_neg2),side='left')
    tmp=len(A_pos[A_pos**2<x])+len(A_neg2[A_neg2**2<x])
    return (z.sum()+w.sum()-tmp)//2

def position(x):
    if x<0:
        return position_in_neg(x)
    elif x==0:
        return n_pos*n_neg
    else:
        return position_in_pos(x)+n_pos*n_neg+(n_pos+n_neg)*n_zero+n_zero*(n_zero-1)//2

l=-pow(10,18)
r=pow(10,18)
while(r-l>1):
    mid=(l+r)//2
    if position(mid)<K:
        l=mid
    else:
        r=mid
print(l)

Buggy submission (verdict: Runtime Error):


import numpy as np
N,K=map(int,input().split())
A=list(map(int,input().split()))

A=np.array(A)

A_pos=np.sort(A[A>0])
A_neg=np.sort(A[A<0])
A_neg2=np.sort(-A_neg)

np=len(A_pos)
nn=len(A_neg)
nz=N-np-nn
n_post=np*(np-1)//2+(nn*(nn-1))//2
n_zero=nz*(np+nn)+nz*(nz-1)//2
n_neg=np*nn

#array内で積がx未満になるペアの数
def syakutori_pos1(x):
    res=0
    j=n-1
    for i in range(n_pos):
        tmp=0
#         j=n-1
        y=-(-x//A_pos[i])
        while j!=-1:
            
            if A_pos[j]>=y:
                j-=1
            else:
                break
        
        tmp+=j+1
        if A_pos[i]**2<x:
            tmp-=1

        res+=tmp
    res=res//2
    return res

def syakutori_pos2(x):
    res=0
    j=n-1
    for i in range(n_neg):
        tmp=0
#         j=n-1
        y=-(-x//A_neg2[i])
        while j!=-1:
            
            if A_neg2[j]>=y:
                j-=1
            else:
                break
        
        tmp+=j+1
        if A_neg2[i]**2<x:
            tmp-=1

        res+=tmp
    res=res//2
    return res


#array内で積がx未満になるペアの数
def syakutori_neg(x):
    y=x//A_neg
#     print(A_pos,y)
    res=0
    i=0
    j=np-1
    for z in reversed(y):
        while(j!=-1):
            if z<A_pos[j]:
                j-=1
            else:
                break
#         print(z,j+1)
        res+=j+1
    return np*nn-res



def syakutori(x):
    if x>0:
        return syakutori_pos1(x)+syakutori_pos2(x)+n_neg+n_zero
    elif x==0:
        return n_neg
    else:
        return syakutori_neg(x)




r=10**18
l=-10**18
while(r-l>1):
    mid=(l+r)//2
    z=syakutori(mid)
#     z= count_pair(mid)
#     print(l,r)
    if z < K:
        l=mid
    else:
        r=mid
print(l)

Buggy submission (verdict: Runtime Error):
import numpy as np
N,K=map(int,input().split())
A=list(map(int,input().split()))
A=sorted(A)
#B=list(reversed(A))
A=np.array(A)
Aneg=A[A<0]
Azero=A[A==0]
Apos=A[A>0]

n_neg=len(Aneg)*len(Apos)
n_zero=len(Azero)*(len(Azero)-1)//2+len(Azero)*(len(Aneg)+len(Apos))
n_pos=(len(Apos)*(len(Apos)-1)+len(Aneg)*(len(Aneg)-1))//2

if K <=n_neg:
    n=K-n_neg
    B=sorted(Aneg)[:n+1]
    C=sorted(Apos)[:n+1]
    ans=[]
    for i in range(min(len(B),n-1)):
        for j in range(min(len(C),n-1)):
            ans.append(B[i]*C[j])
    ans=sorted(ans)
    print(ans[n-1])
    
elif K>n_neg+n_zero:
    n=K-(n_neg+n_zero)
    B=sorted(Aneg)
    B=list(reversed(B))
    C=sorted(Apos)
    ans=[]
    if len(B)>2:
        for i in range(len(B)-1):
            for j in range(i+1,len(B)):
                ans.append(B[i]*B[j])
    if len(C)>2:
#         k=min(len(C),n-1)
        for i in range(len(C)-1):
            for j in range(i+1,len(C)):
                ans.append(C[i]*C[j])
    ans=sorted(ans)
    print(ans[n-1])
    
else:
    print(0)