結果

問題 No.489 株に挑戦
ユーザー data9824
提出日時 2017-02-26 16:32:42
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 54 ms / 1,000 ms
コード長 1,210 bytes
コンパイル時間 595 ms
コンパイル使用メモリ 66,356 KB
実行使用メモリ 13,264 KB
最終ジャッジ日時 2024-07-19 23:31:53
合計ジャッジ時間 2,994 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>

using namespace std;

struct Quote {
    Quote(long long price, int date) : price(price), date(date) {}
    long long price;
    int date;
};

bool operator<(const Quote& lhs, const Quote& rhs) {
    if (lhs.price != rhs.price) {
        return lhs.price < rhs.price;
    }
    return lhs.date > rhs.date;
}

int main(int argc, const char * argv[]) {
    int n, d;
    long long k;
    cin >> n >> d >> k;
    long long x[1000000];
    for (int i = 0; i < n; ++i) {
        cin >> x[i];
    }
    priority_queue<Quote> max;
    for (int i = 0; i < d - 1; ++i) {
        max.push(Quote(x[i], i));
    }
    long long maxProfit = 0;
    int maxIn = 0, maxOut = 0;
    for (int i = 0; i < n; ++i) {
        if (i + d < n) {
            max.push(Quote(x[i + d], i + d));
        }
        while (max.top().date < i) {
            max.pop();
        }
        long long profit = max.top().price - x[i];
        if (maxProfit < profit) {
            maxProfit = profit;
            maxIn = i;
            maxOut = max.top().date;
        }
    }
    cout << (maxProfit * k) << endl;
    if (maxProfit > 0) {
        cout << maxIn << " " << maxOut << endl;
    }
    return 0;
}
0