結果

問題 No.2854 -1 Subsequence
ユーザー Daniel K
提出日時 2024-08-25 14:21:34
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,694 bytes
コンパイル時間 996 ms
コンパイル使用メモリ 111,392 KB
最終ジャッジ日時 2025-02-24 01:09:10
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <list>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <utility>
#include <sstream>
#include <queue>
#include <queue>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  bool has_last_pos = false;
  bool has_last_neg = false;
  int64_t best_if_last_pos = 0;
  int64_t best_if_last_neg = 0;

  int n;
  cin >> n;
  vector<int64_t> vals(n);
  for (int i = 0; i < n; ++i) {
    int64_t val;
    cin >> val;
    vals[i] = val;

    // can always start here
    int64_t best_last_neg = -val;
    if (has_last_neg) {
      best_last_neg = max(best_if_last_neg, best_last_neg);
    }
    if (has_last_pos) {
      best_last_neg = max(best_if_last_pos - val, best_last_neg);
    }

    bool has_new_last_pos = false;
    int64_t best_last_pos = 0;
    if (has_last_pos) {
      has_new_last_pos = true;
      best_last_pos = max(best_if_last_pos, best_last_pos);
    }
    if (has_last_neg) {
      has_new_last_pos = true;
      best_last_pos = max(best_if_last_neg + val, best_last_pos);
    }

    best_if_last_neg = best_last_neg;
    has_last_neg = true;

    if (has_new_last_pos) {
      best_if_last_pos = best_last_pos;
      has_last_pos = true;
    }
  }

  int64_t ans = best_if_last_neg;
  if (has_last_pos) {
    ans = max(best_if_last_pos, ans);
  }

  //  int64_t max_single_val = -vals[0];
  //  if (ans == 0) {
  //    for (int i = 1; i < n; ++i) {
  //      max_single_val = std::max(vals[i], max_single_val);
  //    }
  //    ans = max_single_val;
  //  }

  cout << ans << endl;

  return 0;
}
0