結果
| 問題 |
No.1001 注文の多い順列
|
| コンテスト | |
| ユーザー |
risujiroh
|
| 提出日時 | 2020-03-02 07:51:43 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 75 ms / 2,000 ms |
| コード長 | 1,723 bytes |
| コンパイル時間 | 1,772 ms |
| コンパイル使用メモリ | 177,444 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-10-13 20:55:05 |
| 合計ジャッジ時間 | 3,054 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 31 |
ソースコード
#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';
}
risujiroh