結果

問題 No.1687 What the Heck?
ユーザー Iván SotoIván Soto
提出日時 2021-09-24 21:48:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,161 bytes
コンパイル時間 2,443 ms
コンパイル使用メモリ 207,500 KB
実行使用メモリ 14,324 KB
最終ジャッジ日時 2023-09-18 21:09:31
合計ジャッジ時間 4,463 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 44 ms
6,940 KB
testcase_10 AC 27 ms
5,600 KB
testcase_11 AC 30 ms
5,732 KB
testcase_12 AC 176 ms
13,856 KB
testcase_13 AC 173 ms
13,864 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *  author: ivanzuki   
 *  created: Fri Sep 24 2021
**/
#include <bits/stdc++.h>

using namespace std;

string to_string(string s) {
  return '"' + s + '"';
}
 
string to_string(const char* s) {
  return to_string((string) s);
}
 
string to_string(bool b) {
  return (b ? "true" : "false");
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
 
void debug_out() { cerr << endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
 
template<typename T> void dout(string name, int idx, T arg) {
  cerr << name << " = " << to_string(arg);
}

template<typename T1, typename... T2> void dout(string names, int idx, T1 arg, T2... args) {
  cerr << names.substr(0, names.find(',')) << " = " << to_string(arg) << ", ";
  dout(names.substr(names.find(',') + 2), idx + 1, args...);
}

#ifdef LOCAL
#define debug(...) cerr << "[", dout(#__VA_ARGS__, 0, __VA_ARGS__), cerr << "]" << endl;
#else
#define debug(...) 42
#endif

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> p(n);
  set<int> s;
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    --p[i];
    s.insert(i);
  }
  if (n == 2 && p[0] == 0) {
    cout << 0 << '\n';
    return 0;
  }
  vector<int> ans(n, -1);
  for (int i = n - 1; i >= 0; i--) {
    auto it = s.upper_bound(p[i]);
    if (it != s.end()) {
      ans[i] = *it;
      s.erase(it);
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    if (ans[i] == -1) {
      auto it = s.lower_bound(p[i]);
      if (it == s.end()) {
        --it;
      }
      ans[i] = *it;
    }
  }
  debug(ans);
  long long res = 0;
  for (int i = 0; i < n; i++) {
    if (ans[i] > p[i]) {
      res += i + 1;
    } else if (ans[i] < p[i]) {
      res -= i + 1;
    }
  }
  cout << res << '\n';
  return 0;
}
0