結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー pekempeypekempey
提出日時 2020-04-17 22:13:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 57 ms / 3,500 ms
コード長 1,288 bytes
コンパイル時間 1,742 ms
コンパイル使用メモリ 171,816 KB
実行使用メモリ 15,684 KB
最終ジャッジ日時 2024-04-14 14:30:25
合計ジャッジ時間 5,430 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,136 KB
testcase_01 AC 5 ms
8,136 KB
testcase_02 AC 4 ms
8,260 KB
testcase_03 AC 5 ms
8,468 KB
testcase_04 AC 5 ms
8,644 KB
testcase_05 AC 5 ms
8,388 KB
testcase_06 AC 6 ms
8,644 KB
testcase_07 AC 5 ms
8,468 KB
testcase_08 AC 5 ms
8,512 KB
testcase_09 AC 6 ms
8,516 KB
testcase_10 AC 6 ms
8,516 KB
testcase_11 AC 46 ms
14,108 KB
testcase_12 AC 46 ms
14,132 KB
testcase_13 AC 57 ms
15,432 KB
testcase_14 AC 49 ms
14,404 KB
testcase_15 AC 52 ms
14,916 KB
testcase_16 AC 47 ms
13,892 KB
testcase_17 AC 52 ms
14,872 KB
testcase_18 AC 50 ms
14,792 KB
testcase_19 AC 56 ms
14,916 KB
testcase_20 AC 57 ms
15,252 KB
testcase_21 AC 52 ms
14,656 KB
testcase_22 AC 45 ms
13,892 KB
testcase_23 AC 46 ms
14,020 KB
testcase_24 AC 53 ms
15,048 KB
testcase_25 AC 57 ms
15,376 KB
testcase_26 AC 52 ms
14,912 KB
testcase_27 AC 49 ms
14,276 KB
testcase_28 AC 57 ms
15,428 KB
testcase_29 AC 57 ms
15,424 KB
testcase_30 AC 55 ms
15,296 KB
testcase_31 AC 57 ms
15,384 KB
testcase_32 AC 55 ms
15,256 KB
testcase_33 AC 57 ms
15,424 KB
testcase_34 AC 30 ms
15,304 KB
testcase_35 AC 35 ms
15,512 KB
testcase_36 AC 33 ms
15,424 KB
testcase_37 AC 34 ms
15,684 KB
testcase_38 AC 32 ms
15,376 KB
testcase_39 AC 34 ms
15,300 KB
testcase_40 AC 33 ms
15,428 KB
testcase_41 AC 34 ms
15,560 KB
testcase_42 AC 33 ms
15,300 KB
testcase_43 AC 33 ms
15,256 KB
testcase_44 AC 34 ms
15,044 KB
testcase_45 AC 32 ms
15,044 KB
testcase_46 AC 33 ms
15,660 KB
testcase_47 AC 34 ms
15,640 KB
testcase_48 AC 31 ms
15,520 KB
testcase_49 AC 33 ms
15,668 KB
testcase_50 AC 33 ms
15,428 KB
testcase_51 AC 38 ms
15,232 KB
testcase_52 AC 37 ms
15,400 KB
testcase_53 AC 38 ms
15,388 KB
testcase_54 AC 4 ms
8,136 KB
testcase_55 AC 4 ms
8,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define range(a) a.begin(), a.end()

list<int> L[100000], R[100000];
int par[100000];

int find(int x) {
  if (par[x] == x) return x;
  return par[x] = find(par[x]);
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  while (not L[x].empty() and not L[y].empty() and L[x].back() < L[y].front()) L[x].pop_back();
  while (not R[x].empty() and not R[y].empty() and R[x].back() > R[y].front()) R[y].pop_front();
  L[x].splice(L[x].end(), move(L[y]));
  R[x].splice(R[x].end(), move(R[y]));
  par[y] = x;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N; cin >> N;
  vector<int> A(N); rep(i, N) cin >> A[i], A[i]--;
  vector<int> ord(N); rep(i, N) ord[i] = i;
  sort(range(ord), [&](int i, int j) { return A[i] > A[j]; });
  rep(i, N) {
    L[i].push_back(A[i]);
    R[i].push_back(A[i]);
    par[i] = i;
  }
  ll ans = 0;
  for (int i : ord) {
    if (i - 1 >= 0 and A[i - 1] > A[i]) {
      ans += L[find(i - 1)].size();
      unite(i - 1, i);
    }
    if (i + 1 < N and A[i] < A[i + 1]) {
      ans += R[find(i + 1)].size();
      unite(i, i + 1);
    }
  }
  cout << ans << endl;
}
0