結果

問題 No.1001 注文の多い順列
ユーザー pekempeypekempey
提出日時 2020-02-28 23:00:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 1,884 bytes
コンパイル時間 2,010 ms
コンパイル使用メモリ 172,380 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 20:11:58
合計ジャッジ時間 3,740 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 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 1 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 49 ms
5,376 KB
testcase_19 AC 45 ms
5,376 KB
testcase_20 AC 29 ms
5,376 KB
testcase_21 AC 27 ms
5,376 KB
testcase_22 AC 60 ms
5,376 KB
testcase_23 AC 59 ms
5,376 KB
testcase_24 AC 59 ms
5,376 KB
testcase_25 AC 57 ms
5,376 KB
testcase_26 AC 59 ms
5,376 KB
testcase_27 AC 60 ms
5,376 KB
testcase_28 AC 60 ms
5,376 KB
testcase_29 AC 56 ms
5,376 KB
testcase_30 AC 54 ms
5,376 KB
testcase_31 AC 57 ms
5,376 KB
testcase_32 AC 56 ms
5,376 KB
testcase_33 AC 63 ms
5,376 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()

constexpr int MOD = 1000000007;

class mint {
  int n;
public:
  mint(int n_ = 0) : n(n_) {}
  explicit operator int() { return n; }
  friend mint operator-(mint a) { return -a.n + MOD * (a.n != 0); }
  friend mint operator+(mint a, mint b) { int x = a.n + b.n; return x - (x >= MOD) * MOD; }
  friend mint operator-(mint a, mint b) { int x = a.n - b.n; return x + (x < 0) * MOD; }
  friend mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; }
  friend mint &operator+=(mint &a, mint b) { return a = a + b; }
  friend mint &operator-=(mint &a, mint b) { return a = a - b; }
  friend mint &operator*=(mint &a, mint b) { return a = a * b; }
  friend bool operator==(mint a, mint b) { return a.n == b.n; }
  friend bool operator!=(mint a, mint b) { return a.n != b.n; }
  friend istream &operator>>(istream &i, mint &a) { return i >> a.n; }
  friend ostream &operator<<(ostream &o, mint a) { return o << a.n; }
};

mint f(int n) {
  mint res = 1;
  rep(i, n) {
    res *= i + 1;
  }
  return res;
}

int main() {
  int N; cin >> N;
  vector<int> T(N), X(N);
  rep(i, N) {
    cin >> T[i] >> X[i];
    if (T[i] == 1) {
      X[i]--;
    }
  }
  vector<int> ord(N); rep(i, N) ord[i] = i;
  sort(ord.begin(), ord.end(), [&](int i, int j) {
    return X[i] < X[j];
    });
  vector<mint> dp(N + 1);
  dp[0] = 1;
  rep(i_, N) {
    int i = ord[i_];
    vector<mint> ep(N + 1);
    rep(j, N) {
      if (T[i] == 0) {
        ep[j + 1] += dp[j] * max(0, X[i] - j);
      } else {
        ep[j + 1] -= dp[j] * max(0, X[i] - j);
        ep[j] += dp[j];
      }
    }
    dp = ep;
  }
  mint ans;
  rep(i, N + 1) {
    ans += dp[i] * f(N - i);
  }
  cout << ans << endl;
}
0