結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー kkktymkkktym
提出日時 2021-01-29 22:11:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,773 bytes
コンパイル時間 1,084 ms
コンパイル使用メモリ 102,700 KB
実行使用メモリ 7,544 KB
最終ジャッジ日時 2023-09-09 15:37:29
合計ジャッジ時間 2,622 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 43 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 64 ms
7,152 KB
testcase_06 AC 64 ms
6,264 KB
testcase_07 AC 63 ms
6,884 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <limits>
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; }
template<class T> inline int  sz(T &a) { return a.size(); }
using ll = long long; using ld = long double;
using pi = pair<int,int>; using pl = pair<ll,ll>;
using vi = vector<int>; using vvi = vector<vi>;
using vl = vector<ll>; using vvl = vector<vl>;
const int inf = numeric_limits<int>::max();
const ll infll = numeric_limits<ll>::max();
int main()
{
  auto check = [&](ll a, ll b, ll c)->bool {
    if(a != b && b != c && c != a && ((a < b && b > c) || (a > b && b < c))) return true;
    else return false;
  };
  int q; cin >> q;
  while(q-- > 0) {
    int n; cin >> n;
    vl a(n);
    rep(i,n) cin >> a[i];
    a.push_back(a[0]);
    a.push_back(a[1]);
    ll res = 0;
    vl dp(n+3, 0);
    rep(i,n) {
      if(i+3 <= n && check(a[i], a[i+1], a[i+2])) chmax(dp[i+3], dp[i] + a[i]);
      else chmax(dp[i+1], dp[i]);
    }
    chmax(res, dp[n]);
    dp.assign(n+3, 0);
    rep1(i,n) {
      if(i+3 <= n+1 && check(a[i], a[i+1], a[i+2])) chmax(dp[i+3], dp[i] + a[i]);
      else chmax(dp[i+1], dp[i]);
    }
    chmax(res, dp[n+1]);
    dp.assign(n+3, 0);
    for(int i = 2; i <= n+1; ++i) {
      if(i+3 <= n+2 && check(a[i], a[i+1], a[i+2])) chmax(dp[i+3], dp[i] + a[i]);
      else chmax(dp[i+1], dp[i]);
    }
   chmax(res, dp[n+2]);
    cout << res << "\n";
  }
  return 0;
}
0