結果

問題 No.335 門松宝くじ
ユーザー roarisroaris
提出日時 2019-12-11 14:41:40
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,291 bytes
コンパイル時間 263 ms
コンパイル使用メモリ 87,152 KB
実行使用メモリ 91,844 KB
最終ジャッジ日時 2023-09-06 13:04:40
合計ジャッジ時間 16,347 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
71,380 KB
testcase_01 AC 67 ms
71,444 KB
testcase_02 AC 70 ms
71,480 KB
testcase_03 AC 69 ms
71,476 KB
testcase_04 AC 68 ms
71,264 KB
testcase_05 AC 876 ms
86,516 KB
testcase_06 AC 1,109 ms
87,860 KB
testcase_07 AC 1,562 ms
89,196 KB
testcase_08 AC 1,163 ms
84,244 KB
testcase_09 AC 1,960 ms
90,692 KB
testcase_10 AC 1,985 ms
90,252 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 AC 1,992 ms
91,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegmentTree:
    def __init__(self, N, func):
        n_ = 1
        
        while n_<N:
            n_ *= 2
            
        self.n = n_
        self.func = func
        
        if self.func==max:
            self.ele = -10**18
        elif self.func==min:
            self.ele = 10**18
        
        self.arr = [self.ele]*(2*self.n-1)
    
    def update(self, k, a):
        k += self.n-1
        self.arr[k] = a
        
        while k>0:
            k = (k-1)//2
            self.arr[k] = self.func(self.arr[2*k+1], self.arr[2*k+2])
     
    def query(self, l, r): #[l:r)
        L, R = l+self.n, r+self.n
        res = self.ele
        
        while L<R:
            if R&1:
                R -= 1
                res = self.func(res, self.arr[R-1])
            
            if L & 1:
                res = self.func(res, self.arr[L-1])
                L += 1
            
            L >>= 1
            R >>= 1
        
        return res
        
N, M = map(int, input().split())
v_max = -1
ans = -1

for m in range(M):
    E = list(map(lambda x: x-1, map(int, input().split())))
    st_min = SegmentTree(N, min)
    st_max = SegmentTree(N, max)
    
    for i in range(N):
        st_min.update(i, E[i])
        st_max.update(i, E[i])
    
    v = 0
    
    for i in range(N):
        for j in range(i+1, N):
            prof = 0
            min_1 = st_min.query(0, i)
            max_1 = st_max.query(0, i)
            
            if E[i]>E[j] and min_1<E[i]:
                prof = max(prof, E[i])
            elif E[i]<E[j] and max_1>E[i]:
                prof = max(prof, max(max_1, E[j]))
            
            min_2 = st_min.query(i+1, j)
            max_2 = st_max.query(i+1, j)
            
            if min_2<E[i] and min_2<E[j]:
                prof = max(prof, max(E[i], E[j]))
            elif max_2>E[i] and max_2>E[j]:
                prof = max(prof, max_2)
            
            min_3 = st_min.query(j+1, N)
            max_3 = st_max.query(j+1, N)
            
            if E[i]<E[j] and E[j]>min_3:
                prof = max(prof, E[j])
            elif E[i]>E[j] and E[j]<max_3:
                prof = max(prof, max(E[i], max_3))
            
            v += prof
    
    if v>v_max:
        v_max = v
        ans = m

print(ans)
0