結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー itohdakitohdak
提出日時 2020-07-03 13:41:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,567 bytes
コンパイル時間 1,503 ms
コンパイル使用メモリ 174,888 KB
実行使用メモリ 17,272 KB
最終ジャッジ日時 2023-10-14 23:30:02
合計ジャッジ時間 5,747 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 121 ms
16,976 KB
testcase_29 AC 122 ms
17,032 KB
testcase_30 AC 203 ms
17,124 KB
testcase_31 AC 203 ms
17,272 KB
testcase_32 AC 204 ms
17,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,n-1,0)
#define all(v) v.begin(), v.end()
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N; cin >> N;
  vector<ll> A(N); rep(i, N) cin >> A[i];
  map<ll, int> mpl, mpr;
  rep(i, N) mpr[A[i]]++;
  ll mn = longinf;
  rep(i, N) {
    mpr[A[i]]--;
    if(mpr[A[i]]==0) mpr.erase(A[i]);
    if(!mpl.empty() && !mpr.empty()) {
      auto itrl = mpl.begin(), itrr = mpr.begin();
      while(itrl != mpl.end() && itrl->first == A[i]) itrl++;
      while(itrr != mpl.end() && itrr->first == A[i]) itrr++;
      if(itrl != mpl.end() && itrr != mpr.end()) {
        if(itrl->first > A[i] || itrr->first > A[i]) continue;
        if(itrl->first == itrr->first) {
          auto tmp = itrl;
          while(tmp != mpl.end() && tmp->first == A[i]) tmp++;
          if(tmp != mpl.end() && tmp->first < A[i]) {
            mn = min(A[i]+tmp->first+itrr->first, mn);
          }
          tmp = itrr;
          while(tmp != mpr.end() && tmp->first == A[i]) tmp++;
          if(tmp != mpr.end() && tmp->first < A[i]) {
            mn = min(A[i]+itrl->first+tmp->first, mn);
          }
        } else {
          mn = min(A[i]+itrl->first+itrr->first, mn);
        }
      }
    }
    mpl[A[i]]++;
  }
  cout << (mn == longinf ? -1 : mn) << "\n";
  return 0;
}
0