結果

問題 No.1001 注文の多い順列
ユーザー risujirohrisujiroh
提出日時 2020-03-02 07:51:43
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 1,723 bytes
コンパイル時間 1,656 ms
コンパイル使用メモリ 173,840 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 22:29:19
合計ジャッジ時間 3,138 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 33 ms
5,376 KB
testcase_19 AC 32 ms
5,376 KB
testcase_20 AC 20 ms
5,376 KB
testcase_21 AC 18 ms
5,376 KB
testcase_22 AC 41 ms
5,376 KB
testcase_23 AC 39 ms
5,376 KB
testcase_24 AC 38 ms
5,376 KB
testcase_25 AC 37 ms
5,376 KB
testcase_26 AC 72 ms
5,376 KB
testcase_27 AC 72 ms
5,376 KB
testcase_28 AC 70 ms
5,376 KB
testcase_29 AC 10 ms
5,376 KB
testcase_30 AC 12 ms
5,376 KB
testcase_31 AC 16 ms
5,376 KB
testcase_32 AC 3 ms
5,376 KB
testcase_33 AC 76 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <unsigned M> struct modular {
  using m = modular;
  unsigned v;
  modular(long long x = 0) : v((x %= M) < 0 ? x + M : x) {}
  m operator-() const { return m() -= *this; }
  m& operator+=(m b) { if ((v += b.v) >= M) v -= M; return *this; }
  m& operator-=(m b) { if (v < b.v) v += M; v -= b.v; return *this; }
  m& operator*=(m b) { v = (uint64_t)v * b.v % M; return *this; }
  m& operator/=(m b) { return *this *= power(b, M - 2); }
  friend m operator+(m a, m b) { return a += b; }
  friend m operator-(m a, m b) { return a -= b; }
  friend m operator*(m a, m b) { return a *= b; }
  friend m operator/(m a, m b) { return a /= b; }
  friend bool operator==(m a, m b) { return a.v == b.v; }
  friend string to_string(m a) { return to_string(a.v); }
};

constexpr long long mod = 1e9 + 7;
using mint = modular<mod>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<pair<int, int>> p(n);
  for (auto&& e : p) {
    cin >> e.first >> e.second;
    e.first ^= 1;
    e.second -= e.first == 0;
  }
  sort(begin(p), end(p));
  vector<int> a(n), b(n);
  for (int i = 0; i < n; ++i) {
    a[i] = upper_bound(begin(p), end(p), make_pair(0, i)) - begin(p);
    b[i] = upper_bound(begin(p), end(p), make_pair(1, i)) - begin(p);
  }
  int m = *max_element(begin(a), end(a));
  assert(*min_element(begin(b), end(b)) == m);
  vector<mint> dp(m + 1);
  dp[0] = 1;
  for (int i = 0; i < n; ++i) {
    vector<mint> ndp(m + 1);
    for (int x = 0; x <= m; ++x) {
      if (x < m) {
        ndp[x + 1] += dp[x] * (a[i] - x);
      }
      ndp[x] += dp[x] * (m + i - x - b[i] + 1);
    }
    swap(dp, ndp);
  }
  cout << dp[m].v << '\n';
}
0