結果

問題 No.489 株に挑戦
ユーザー lam6er
提出日時 2025-03-31 17:38:37
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,391 bytes
コンパイル時間 134 ms
コンパイル使用メモリ 82,460 KB
実行使用メモリ 77,744 KB
最終ジャッジ日時 2025-03-31 17:39:37
合計ジャッジ時間 3,664 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

def main():
    n, d, k = map(int, sys.stdin.readline().split())
    x = [int(sys.stdin.readline()) for _ in range(n)]
    
    max_profit = 0
    best_j = -1
    best_k = -1
    
    dq = deque()
    
    for current_k in range(1, n):
        j_current = current_k - 1
        
        # Maintain the deque to have increasing values of x[j]
        while dq and x[dq[-1]] >= x[j_current]:
            dq.pop()
        dq.append(j_current)
        
        # Calculate the left boundary of the valid window
        left = max(0, current_k - d)
        
        # Remove elements out of the window's left boundary
        while dq and dq[0] < left:
            dq.popleft()
        
        if dq:
            current_j = dq[0]
            profit = (x[current_k] - x[current_j]) * k
            if profit > max_profit:
                max_profit = profit
                best_j = current_j
                best_k = current_k
            elif profit == max_profit:
                # Check if the new pair is lexicographically smaller
                if (current_j < best_j) or (current_j == best_j and current_k < best_k):
                    best_j = current_j
                    best_k = current_k
    
    if max_profit <= 0:
        print(0)
    else:
        print(max_profit)
        print(best_j, best_k)

if __name__ == "__main__":
    main()
0