結果

問題 No.919 You Are A Project Manager
ユーザー vwxyzvwxyz
提出日時 2023-03-26 04:04:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 856 ms / 3,000 ms
コード長 6,728 bytes
コンパイル時間 948 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 136,796 KB
最終ジャッジ日時 2024-09-19 06:49:30
合計ジャッジ時間 23,892 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 604 ms
129,016 KB
testcase_01 AC 39 ms
54,912 KB
testcase_02 AC 40 ms
55,296 KB
testcase_03 AC 42 ms
55,424 KB
testcase_04 AC 192 ms
86,760 KB
testcase_05 AC 204 ms
87,096 KB
testcase_06 AC 142 ms
78,412 KB
testcase_07 AC 395 ms
100,968 KB
testcase_08 AC 213 ms
82,432 KB
testcase_09 AC 178 ms
80,920 KB
testcase_10 AC 238 ms
84,084 KB
testcase_11 AC 219 ms
82,920 KB
testcase_12 AC 66 ms
72,448 KB
testcase_13 AC 231 ms
85,212 KB
testcase_14 AC 106 ms
77,636 KB
testcase_15 AC 163 ms
79,324 KB
testcase_16 AC 279 ms
89,236 KB
testcase_17 AC 639 ms
136,664 KB
testcase_18 AC 623 ms
134,224 KB
testcase_19 AC 644 ms
129,136 KB
testcase_20 AC 785 ms
128,784 KB
testcase_21 AC 600 ms
128,888 KB
testcase_22 AC 439 ms
103,896 KB
testcase_23 AC 400 ms
101,480 KB
testcase_24 AC 431 ms
103,724 KB
testcase_25 AC 413 ms
101,424 KB
testcase_26 AC 753 ms
124,164 KB
testcase_27 AC 676 ms
123,344 KB
testcase_28 AC 820 ms
128,872 KB
testcase_29 AC 651 ms
136,796 KB
testcase_30 AC 659 ms
133,936 KB
testcase_31 AC 631 ms
136,400 KB
testcase_32 AC 629 ms
134,160 KB
testcase_33 AC 444 ms
103,852 KB
testcase_34 AC 439 ms
103,580 KB
testcase_35 AC 856 ms
129,760 KB
testcase_36 AC 846 ms
129,632 KB
testcase_37 AC 840 ms
130,008 KB
testcase_38 AC 844 ms
129,516 KB
testcase_39 AC 75 ms
76,160 KB
testcase_40 AC 175 ms
79,620 KB
testcase_41 AC 113 ms
78,080 KB
testcase_42 AC 135 ms
78,192 KB
testcase_43 AC 161 ms
78,700 KB
testcase_44 AC 176 ms
80,868 KB
testcase_45 AC 137 ms
78,228 KB
testcase_46 AC 179 ms
80,416 KB
testcase_47 AC 410 ms
101,284 KB
testcase_48 AC 415 ms
101,444 KB
testcase_49 AC 434 ms
103,424 KB
testcase_50 AC 421 ms
103,428 KB
testcase_51 AC 398 ms
100,896 KB
testcase_52 AC 342 ms
96,896 KB
testcase_53 AC 398 ms
100,768 KB
testcase_54 AC 161 ms
79,148 KB
testcase_55 AC 41 ms
55,552 KB
testcase_56 AC 42 ms
55,936 KB
testcase_57 AC 44 ms
59,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline
from collections import defaultdict

def Compress(lst):
    decomp=sorted(list(set(lst)))
    comp={x:i for i,x in enumerate(decomp)}
    return comp,decomp

def Mo(N,Q,query):
    D=max(1,int(N/Q**.5))
    mo=[[] for d in range((N+D-1)//D+1)]
    for q,(l,r) in enumerate(query):
        mo[l//D].append(q)
    retu=[]
    for d in range((N+D-1)//D+1):
        retu+=sorted(mo[d],reverse=d%2,key=lambda q:query[q][1])
    return retu

class Segment_Tree:
    def __init__(self,N,f,e,lst=None,dynamic=False):
        self.f=f
        self.e=e
        self.N=N
        if dynamic:
            self.segment_tree=defaultdict(lambda:self.e)
        else:
            if lst==None:
                self.segment_tree=[self.e]*2*self.N
            else:
                assert len(lst)<=self.N
                self.segment_tree=[self.e]*self.N+[x for x in lst]+[self.e]*(N-len(lst))
                for i in range(self.N-1,0,-1):
                    self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])

    def __getitem__(self,i):
        if type(i)==int:
            if -self.N<=i<0:
                return self.segment_tree[i+self.N*2]
            elif 0<=i<self.N:
                return self.segment_tree[i+self.N]
            else:
                raise IndexError("list index out of range")
        else:
            a,b,c=i.start,i.stop,i.step
            if a==None:
                a=self.N
            else:
                a+=self.N
            if b==None:
                b=self.N*2
            else:
                b+=self.N
            return self.segment_tree[slice(a,b,c)]

    def __setitem__(self,i,x):
        if -self.N<=i<0:
            i+=self.N*2
        elif 0<=i<self.N:
            i+=self.N
        else:
            raise IndexError("list index out of range")
        self.segment_tree[i]=x
        while i>1:
            i>>= 1
            self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])

    def Build(self,lst):
        for i,x in enumerate(lst,self.N):
            self.segment_tree[i]=x
        for i in range(self.N-1,0,-1):
            self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])

    def Fold(self,L=None,R=None):
        if L==None:
            L=self.N
        else:
            L+=self.N
        if R==None:
            R=self.N*2
        else:
            R+=self.N
        vL=self.e
        vR=self.e
        while L<R:
            if L&1:
                vL=self.f(vL,self.segment_tree[L])
                L+=1
            if R&1:
                R-=1
                vR=self.f(self.segment_tree[R],vR)
            L>>=1
            R>>=1
        return self.f(vL,vR)

    def Fold_Index(self,L=None,R=None):
        if L==None:
            L=self.N
        else:
            L+=self.N
        if R==None:
            R=self.N*2
        else:
            R+=self.N
        if L==R:
            return None
        x=self.Fold(L-self.N,R-self.N)
        while L<R:
            if L&1:
                if self.segment_tree[L]==x:
                    i=L
                    break
                L+=1
            if R&1:
                R-=1
                if self.segment_tree[R]==x:
                    i=R
                    break
            L>>=1
            R>>=1
        while i<self.N:
            if self.segment_tree[i]==self.segment_tree[i<<1]:
                i<<=1
            else:
                i<<=1
                i|=1
        i-=self.N
        return i

    def Bisect_Right(self,L=None,f=None):
        if L==self.N:
            return self.N
        if L==None:
            L=0
        L+=self.N
        vl=self.e
        vr=self.e
        l,r=L,self.N*2
        while l<r:
            if l&1:
                vl=self.f(vl,self.segment_tree[l])
                l+=1
            if r&1:
                r-=1
                vr=self.f(self.segment_tree[r],vr)
            l>>=1
            r>>=1
        if f(self.f(vl,vr)):
            return self.N
        v=self.e
        while True:
            while L%2==0:
                L>>=1
            vv=self.f(v,self.segment_tree[L])
            if f(vv):
                v=vv
                L+=1
            else:
                while L<self.N:
                    L<<=1
                    vv=self.f(v,self.segment_tree[L])
                    if f(vv):
                        v=vv
                        L+=1
                return L-self.N

    def Bisect_Left(self,R=None,f=None):
        if R==0:
            return 0
        if R==None:
            R=self.N
        R+=self.N
        vl=self.e
        vr=self.e
        l,r=self.N,R
        while l<r:
            if l&1:
                vl=self.f(vl,self.segment_tree[l])
                l+=1
            if r&1:
                r-=1
                vr=self.f(self.segment_tree[r],vr)
            l>>=1
            r>>=1
        if f(self.f(vl,vr)):
            return 0
        v=self.e
        while True:
            R-=1
            while R>1 and R%2:
                R>>=1
            vv=self.f(self.segment_tree[R],v)
            if f(vv):
                v=vv
            else:
                while R<self.N:
                    R=2*R+1
                    vv=self.f(self.segment_tree[R],v)
                    if f(vv):
                        v=vv
                        R-=1
                return R+1-self.N

    def __str__(self):
        return "["+", ".join(map(str,self.segment_tree[self.N:]))+"]"

N=int(readline())
A=list(map(int,readline().split()))
query=[]
for K in range(1,N+1):
    for l in range(0,N-K+1,K):
        query.append((l,l+K))
    if N%K:
        for r in range(N,K-1,-K):
            query.append((r-K,r))
Q=len(query)
L,R=0,0
comp,decomp=Compress(A)
le=len(comp)
for i in range(N):
    A[i]=comp[A[i]]
ST=Segment_Tree(le,lambda x,y:x+y,0)
dct={}
for q in Mo(N,Q,query):
    l,r=query[q]
    while l<L:
        L-=1
        ST[A[L]]+=1
    while R<r:
        ST[A[R]]+=1
        R+=1
    while L<l:
        ST[A[L]]-=1
        L+=1
    while r<R:
        R-=1
        ST[A[R]]-=1
    dct[(l,r)]=decomp[ST.Bisect_Right(0,lambda x:x<=(r-l-1)//2)]
inf=1<<60
ans=-inf
for K in range(1,N+1):
    left=[0]
    for l in range(0,N-K+1,K):
        left.append(dct[(l,l+K)])
    for i in range(1,len(left)):
        left[i]+=left[i-1]
    for i in range(1,len(left)):
        left[i]=max(left[i],left[i-1])
    right=[0]
    for r in range(N,K-1,-K):
        right.append(dct[(r-K,r)])
    for i in range(1,len(right)):
        right[i]+=right[i-1]
    for i in range(1,len(left)):
        right[i]=max(right[i],right[i-1])
    for x,y in zip(left,right[::-1]):
        ans=max(ans,(x+y)*K)
print(ans)
0