def General_Binary_Increase_Search(L,R,cond,Integer=True,ep=1/(1<<20)): """条件式が単調増加であるとき,一般的な二部探索を行う. L:解の下限 R:解の上限 cond:条件(1変数関数,広義単調減少 or 広義単調減少を満たす) Integer:解を整数に制限するか? ep:Integer=Falseのとき,解の許容する誤差 """ if not(cond(R)): return False if Integer: R+=1 while R-L>1: C=L+(R-L)//2 if cond(C): R=C else: L=C return R else: while (R-L)>=ep: C=L+(R-L)/2 if cond(C): R=C else: L=C return R #================================================ def f(x): #負の時の判定 Z=0 I=0 for u in U_pos: if I0: while u*V_pos[I-1]>x: I-=1 if I==0: break Z+=I I=0 for v in V_neg: if I0] U_neg=[u for u in U if u<0] V_pos=[v for v in V if v>0] V_neg=[v for v in V if v<0] U_positive=len(U_pos) U_negative=len(U_neg) U_zero=K*L-(U_positive+U_negative) V_positive=len(V_pos) V_negative=len(V_neg) V_zero=M*N-(V_positive+V_negative) #================================================ # Eの正,ゼロ,負の個数を求める E_positive=U_positive*V_positive+U_negative*V_negative E_negative=U_positive*V_negative+U_negative*V_positive E_zero=K*L*M*N-(E_positive+E_negative) #================================================ # Jを求める. U_abs_max=abs(max(U,key=lambda u:abs(u))) V_abs_max=abs(max(V,key=lambda v:abs(v))) Abs_max=U_abs_max*V_abs_max+1 if S<=E_negative: #負確定 Ans=General_Binary_Increase_Search(-Abs_max,0,lambda x:f(x)>=S) elif E_negative+1<=S<=E_negative+E_zero: #ゼロ確定 Ans=0 else: #正確定 Ans=General_Binary_Increase_Search(0,Abs_max,lambda x:g(x)>=S-(E_negative+E_zero)) #================================================ # T=abcd なる a,b,c,dを求める. alpha,beta=h(Ans,U,V) a,b=h(alpha,A,B) c,d=h(beta ,C,D) #================================================ #出力 print(Ans) print(a,b,c,d)