結果

問題 No.489 株に挑戦
ユーザー hedwig100hedwig100
提出日時 2022-09-22 21:28:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 16 ms / 1,000 ms
コード長 5,684 bytes
コンパイル時間 2,241 ms
コンパイル使用メモリ 206,432 KB
実行使用メモリ 7,500 KB
最終ジャッジ日時 2023-08-23 20:39:20
合計ジャッジ時間 3,650 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
4,376 KB
testcase_01 AC 8 ms
4,432 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 12 ms
5,336 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 7 ms
4,376 KB
testcase_19 AC 6 ms
4,380 KB
testcase_20 AC 12 ms
5,344 KB
testcase_21 AC 4 ms
4,380 KB
testcase_22 AC 3 ms
4,376 KB
testcase_23 AC 8 ms
4,376 KB
testcase_24 AC 14 ms
5,296 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 16 ms
7,184 KB
testcase_27 AC 11 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 16 ms
7,500 KB
testcase_31 AC 16 ms
7,416 KB
testcase_32 AC 8 ms
4,380 KB
testcase_33 AC 13 ms
5,116 KB
testcase_34 AC 11 ms
4,376 KB
testcase_35 AC 5 ms
4,376 KB
testcase_36 AC 4 ms
4,380 KB
testcase_37 AC 9 ms
4,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL_
void debug_out() {
    cerr << "\033[0m" << endl;
}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << ' ' << H << ',';
    debug_out(T...);
}
#define debug(...) cerr << "\033[1;36m" << __func__ << ":L" << __LINE__ << " " << #__VA_ARGS__ << ":", debug_out(__VA_ARGS__)
#define dump(x) cerr << __func__ << ":L" << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep(i, n) for (int i = (int)(n)-1; i >= 0; --i)

using ll  = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

constexpr int INF        = 1000'000'000;
constexpr long long HINF = 4000'000'000000'000'000;
constexpr long long MOD  = 1000000007; // = 998244353;
constexpr double EPS     = 1e-4;
constexpr double PI      = 3.14159265358979;

#pragma region Macros
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '[';
    for (auto &e : v) {
        os << e << ',';
    }
    os << ']';
    return os;
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &st) {
    os << '{';
    for (auto itr = st.begin(); itr != st.end(); itr++) {
        os << *itr << ',';
    }
    os << '}';
    return os;
}
template <typename K, typename V>
ostream &operator<<(ostream &os, const map<K, V> &mp) {
    os << '{';
    for (auto itr = mp.begin(); itr != mp.end(); itr++) {
        os << itr->first << ": " << itr->second << ',';
    }
    os << '}';
    return os;
}

void yn(bool cond, string Yes = "Yes", string No = "No") {
    cout << (cond ? Yes : No) << '\n';
}

template <typename T>
bool chmax(T &x, const T &y) {
    return (x < y) ? (x = y, true) : false;
}

template <typename T>
bool chmin(T &x, const T &y) {
    return (x > y) ? (x = y, true) : false;
}

#pragma endregion

// SlidingWindowAggregation
// 半群に対して以下のことが行えるデータ構造
//
// push(x): xを追加する
// pop(): queueの要領で要素を取り除く(FIFO)
// fold(): 今入っている要素を早く入っていた方からa0,a1,...,anとしたときにa0*a1*...anを計算する.
template <typename SemiGrp>
struct SlidingWindowAggregation {
    using Fx = function<SemiGrp(const SemiGrp &, const SemiGrp &)>;

    vector<SemiGrp> left, left_cum, right, right_cum;
    Fx op;

    SlidingWindowAggregation(Fx op) : op(op) {}

    inline int size() { return (int)left.size() + (int)right.size(); }
    inline bool is_empty() { return size() == 0; }

    // push
    // xを追加する
    // 計算量: O(1)
    void push(SemiGrp x) {
        if ((int)right.size() == 0) {
            right.push_back(x);
            right_cum.push_back(x);
        } else {
            right.push_back(x);
            right_cum.push_back(op(right_cum.back(), x));
        }
    }

    // pop
    // データをFIFOで取り出す. 空の場合は何もしない.
    // 計算量: 償却 O(1)
    void pop() {
        if (is_empty()) return;
        if ((int)left.size() != 0) {
            left.pop_back();
            left_cum.pop_back();
            return;
        }

        int sz = (int)right.size();
        left.push_back(right.back());
        left_cum.push_back(right.back());
        right.pop_back(), right_cum.pop_back();
        for (int i = 1; i < sz - 1; i++) {
            left.push_back(right.back());
            left_cum.push_back(op(right.back(), left_cum.back()));
            right.pop_back(), right_cum.pop_back();
        }
        if (sz > 1) right.pop_back(), right_cum.pop_back();
    }

    // fold
    // 今入っている要素を早く入っていた方からa0,a1,...,anとしたときにa0*a1*...anを返す.
    // 制約: 空ではない
    // 計算量: O(1)
    SemiGrp fold() {
        assert(!is_empty());
        if ((int)left.size() == 0)
            return right_cum.back();
        else if ((int)right.size() == 0)
            return left_cum.back();
        else
            return op(left_cum.back(), right_cum.back());
    }

    friend ostream &operator<<(ostream &os, const SlidingWindowAggregation<SemiGrp> &swag) {
        for (int i = (int)swag.left.size() - 1; i >= 0; i--)
            os << swag.left[i] << ' ';
        for (int i = 0; i < (int)swag.right.size(); i++)
            os << swag.right[i] << ' ';
        return os;
    }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    int n, d, k;
    cin >> n >> d >> k;
    vector<int> X(n);
    for (int i = 0; i < n; i++)
        cin >> X[i];

    using SemiGrp = pair<int, int>;
    SlidingWindowAggregation<SemiGrp> swag([](const SemiGrp &a, const SemiGrp &b) {
        if (a.first < b.first)
            return b;
        else
            return a;
    });

    int max_diff = 0;
    int ai = -1, bi = -1;
    for (int i = 0; i <= d; i++)
        swag.push({X[i], i});
    for (int i = 0; i < n; i++) {
        auto [x, j] = swag.fold();
        debug(i, max_diff, swag);
        if (max_diff < x - X[i]) {
            max_diff = x - X[i];
            ai = i, bi = j;
        }
        swag.pop();
        if (i + d + 1 < n) swag.push({X[i + d + 1], i + d + 1});
    }

    if (max_diff == 0)
        cout << 0 << '\n';
    else {
        cout << (long long)k * max_diff << '\n';
        cout << ai << ' ' << bi << '\n';
    }

    return 0;
}
0