結果

問題 No.2036 Max Middle
ユーザー recta001recta001
提出日時 2022-08-12 23:16:59
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 1,062 bytes
コンパイル時間 2,367 ms
コンパイル使用メモリ 205,052 KB
実行使用メモリ 7,888 KB
最終ジャッジ日時 2023-10-24 11:37:27
合計ジャッジ時間 3,716 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 48 ms
5,184 KB
testcase_09 AC 81 ms
5,784 KB
testcase_10 AC 85 ms
5,856 KB
testcase_11 AC 69 ms
4,888 KB
testcase_12 AC 60 ms
4,888 KB
testcase_13 AC 88 ms
6,964 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 92 ms
6,936 KB
testcase_17 AC 37 ms
7,888 KB
testcase_18 AC 59 ms
4,888 KB
testcase_19 AC 62 ms
4,888 KB
testcase_20 AC 71 ms
6,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
    ll n;
    cin >> n;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];

    ll ans = 0;

    int direction = -1;
    vector<ll> cnt;
    ll cnt_now = 0;
    rep(i, n - 1) {
        int direction_now = (a[i + 1] - a[i]) / abs(a[i + 1] - a[i]);
        if (direction_now != direction) {
            if ((int)cnt.size() == 0 && direction == -1) {
            } else {
                cnt.push_back(cnt_now);
            }
            cnt_now = 1;
            direction = direction_now;
        } else {
            cnt_now++;
        }

        if (i == n - 2 && direction == -1) {
            cnt.push_back(cnt_now);
        }
    }

    int k = (int)cnt.size();
    ll sum = 0;
    rep(i, k) {
        if (i % 2 == 1) sum += cnt[i];
    }
    rep(i, k - 1) {
        if (i % 2 == 0) {
            ans += cnt[i] * sum;
        } else {
            sum -= cnt[i];
        }
    }

    cout << ans << endl;
}
0