結果

問題 No.1251 絶対に間違ってはいけない最小化問題
ユーザー 👑 KazunKazun
提出日時 2021-09-20 04:52:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 760 ms / 2,000 ms
コード長 3,801 bytes
コンパイル時間 469 ms
コンパイル使用メモリ 87,108 KB
実行使用メモリ 196,792 KB
最終ジャッジ日時 2023-09-15 04:31:28
合計ジャッジ時間 35,862 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
71,548 KB
testcase_01 AC 79 ms
71,720 KB
testcase_02 AC 80 ms
71,532 KB
testcase_03 AC 78 ms
71,388 KB
testcase_04 AC 78 ms
71,332 KB
testcase_05 AC 79 ms
71,760 KB
testcase_06 AC 82 ms
71,424 KB
testcase_07 AC 80 ms
71,464 KB
testcase_08 AC 84 ms
76,072 KB
testcase_09 AC 84 ms
76,256 KB
testcase_10 AC 83 ms
76,004 KB
testcase_11 AC 86 ms
76,044 KB
testcase_12 AC 84 ms
76,164 KB
testcase_13 AC 84 ms
76,052 KB
testcase_14 AC 83 ms
76,036 KB
testcase_15 AC 82 ms
76,168 KB
testcase_16 AC 84 ms
75,924 KB
testcase_17 AC 86 ms
76,072 KB
testcase_18 AC 760 ms
196,092 KB
testcase_19 AC 725 ms
191,128 KB
testcase_20 AC 631 ms
164,012 KB
testcase_21 AC 510 ms
144,548 KB
testcase_22 AC 644 ms
161,984 KB
testcase_23 AC 495 ms
144,200 KB
testcase_24 AC 693 ms
170,224 KB
testcase_25 AC 633 ms
163,988 KB
testcase_26 AC 546 ms
151,864 KB
testcase_27 AC 501 ms
144,288 KB
testcase_28 AC 759 ms
196,592 KB
testcase_29 AC 752 ms
196,416 KB
testcase_30 AC 748 ms
196,792 KB
testcase_31 AC 752 ms
196,584 KB
testcase_32 AC 746 ms
196,724 KB
testcase_33 AC 745 ms
196,760 KB
testcase_34 AC 746 ms
196,724 KB
testcase_35 AC 760 ms
196,528 KB
testcase_36 AC 746 ms
196,420 KB
testcase_37 AC 743 ms
196,592 KB
testcase_38 AC 755 ms
196,748 KB
testcase_39 AC 750 ms
196,524 KB
testcase_40 AC 759 ms
196,444 KB
testcase_41 AC 758 ms
196,696 KB
testcase_42 AC 753 ms
196,672 KB
testcase_43 AC 81 ms
75,960 KB
testcase_44 AC 79 ms
71,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Binary_Indexed_Tree():
    def __init__(self, L, calc, unit, inv, index=1):
        """ calc を演算とする N 項の Binary Indexed Tree を作成
        calc: 演算 (2変数関数, 可換群)
        unit: 群 calc の単位元 (x+e=e+x=xを満たすe)
        inv : 群 calc の逆元 (1変数関数, x+y(x)=y(x)+x=e をみたす y(x))
        """
        self.calc=calc
        self.unit=unit
        self.inv=inv
        self.index=index

        N=len(L)
        d=max(1,(N-1).bit_length())
        k=2**d

        X=[None]+[unit]*k

        self.num=k
        self.depth=d

        if L:
            for i in range(len(L)):
                p=i+1
                while p<=k:
                    X[p]=self.calc(X[p],L[i])
                    p+=p&(-p)
        self.data=X

    def index_number(self, k, index=1):
        """ 第 k 要素の値を出力する.
        k    : 数列の要素
        index: 先頭の要素の番号
        """
        return self.sum(k,k,index)

    def add(self, k, x, index=1):
        """ 第 k 要素に x を加え, 更新を行う.
        k    : 数列の要素
        x    : 加える値
        index: 先頭の要素の番号
        right:「左から」が「右から」になる
        """
        p=k+(1-index)
        while p<=self.num:
            self.data[p]=self.calc(self.data[p],x)
            p+=p&(-p)

    def update(self, k, x, index=1):
        """ 第 k 要素を x に変え, 更新を行う.
        k: 数列の要素
        x: 更新後の値
        """

        a=self.index_number(k,index)
        y=self.calc(self.inv(a),x)

        self.add(k,y,index)

    def sum(self, From, To, index=1):
        """ 第 From 要素から第 To 要素までの総和を求める.
        ※From!=1を使うならば, 群でなくてはならない.
        From : 始まり
        To   : 終わり
        index: 先頭の要素の番号
        """
        alpha=max(1,From+(1-index))
        beta=min(self.num,To+(1-index))

        if alpha==1:
            return self.__section(beta)
        else:
            return self.calc(self.inv(self.__section(alpha-1)),self.__section(beta))

    def __section(self,x):
        """ B[1]+...+B[x] を求める. """
        S=self.unit
        while x>0:
            S=self.calc(self.data[x],S)
            x-=x&(-x)
        return S

    def all_sum(self):
        return self.data[-1]

    def binary_search(self, cond, index=1):
        """ cond(B[1]+...+B[k]) を満たす最小の k を返す.

        cond: 単調増加

        ※ cond(uint)=True の場合の返り値は index-1
        ※ cond(B[1]+...+B[k]) なる k が存在しない場合の返り値は self.num+index
        """

        if cond(self.unit):
            return index-1

        j=0
        r=self.num
        t=r
        data=self.data
        alpha=self.unit

        for _ in range(self.depth+1):
            if j+t<=self.num:
                beta=self.calc(alpha,data[j+t])
                if not cond(beta):
                    alpha=beta
                    j+=t
            t>>=1

        return j+index

    def __getitem__(self,index):
        if isinstance(index,int):
            return self.index_number(index,self.index)
        else:
            return [self.index_number(t,self.index) for t in index]

    def __setitem__(self,index,val):
        self.update(index,val,self.index)

#==================================================
from operator import add,neg

N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))

A_min=min(A)
T=Binary_Indexed_Tree([0]*(max(A)-min(A)+1),add,0,neg,A_min)

for a,b in zip(A,B):
    T.add(a,b,A_min)

B_sum=sum(B)
X=T.binary_search(lambda x:x>=(B_sum+1)//2, A_min)

Y=0
for a,b in zip(A,B):
    Y+=b*abs(X-a)

print(X,Y)
0