結果

問題 No.489 株に挑戦
ユーザー lam6er
提出日時 2025-04-16 01:04:19
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,287 bytes
コンパイル時間 266 ms
コンパイル使用メモリ 81,628 KB
実行使用メモリ 77,180 KB
最終ジャッジ日時 2025-04-16 01:06:28
合計ジャッジ時間 3,661 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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
    q = deque()
    
    for current_k in range(1, n):
        j_new = current_k - 1
        # Maintain deque in increasing order of x[j]
        while q and x[j_new] <= x[q[-1]]:
            q.pop()
        q.append(j_new)
        
        # Calculate the left boundary of the window
        left = max(0, current_k - d)
        # Remove elements from the front that are out of the window
        while q[0] < left:
            q.popleft()
        
        j_min = q[0]
        current_profit = (x[current_k] - x[j_min]) * k
        
        if current_profit > max_profit:
            max_profit = current_profit
            best_j = j_min
            best_k = current_k
        elif current_profit == max_profit and max_profit > 0:
            # Check lexicographical order
            if (j_min < best_j) or (j_min == best_j and current_k < best_k):
                best_j = j_min
                best_k = current_k
    
    if max_profit <= 0:
        print(0)
    else:
        print(max_profit)
        print(best_j, best_k)

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