結果
問題 | No.1001 注文の多い順列 |
ユーザー | milanis48663220 |
提出日時 | 2023-07-18 00:01:03 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 75 ms / 2,000 ms |
コード長 | 2,722 bytes |
コンパイル時間 | 1,687 ms |
コンパイル使用メモリ | 131,812 KB |
実行使用メモリ | 38,556 KB |
最終ジャッジ日時 | 2023-10-18 03:13:14 |
合計ジャッジ時間 | 4,140 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
4,348 KB |
testcase_01 | AC | 2 ms
4,348 KB |
testcase_02 | AC | 2 ms
4,348 KB |
testcase_03 | AC | 2 ms
4,348 KB |
testcase_04 | AC | 3 ms
4,348 KB |
testcase_05 | AC | 2 ms
4,348 KB |
testcase_06 | AC | 2 ms
4,348 KB |
testcase_07 | AC | 2 ms
4,348 KB |
testcase_08 | AC | 2 ms
4,348 KB |
testcase_09 | AC | 2 ms
4,348 KB |
testcase_10 | AC | 2 ms
4,348 KB |
testcase_11 | AC | 2 ms
4,348 KB |
testcase_12 | AC | 2 ms
4,348 KB |
testcase_13 | AC | 2 ms
4,348 KB |
testcase_14 | AC | 2 ms
4,348 KB |
testcase_15 | AC | 3 ms
4,348 KB |
testcase_16 | AC | 3 ms
4,348 KB |
testcase_17 | AC | 2 ms
4,348 KB |
testcase_18 | AC | 41 ms
32,484 KB |
testcase_19 | AC | 38 ms
29,580 KB |
testcase_20 | AC | 25 ms
20,076 KB |
testcase_21 | AC | 23 ms
18,492 KB |
testcase_22 | AC | 50 ms
38,556 KB |
testcase_23 | AC | 50 ms
38,292 KB |
testcase_24 | AC | 44 ms
38,292 KB |
testcase_25 | AC | 48 ms
37,500 KB |
testcase_26 | AC | 46 ms
36,972 KB |
testcase_27 | AC | 46 ms
37,764 KB |
testcase_28 | AC | 45 ms
37,764 KB |
testcase_29 | AC | 41 ms
38,028 KB |
testcase_30 | AC | 39 ms
36,708 KB |
testcase_31 | AC | 42 ms
38,292 KB |
testcase_32 | AC | 35 ms
38,556 KB |
testcase_33 | AC | 75 ms
38,556 KB |
ソースコード
#include <iostream> #include <algorithm> #include <iomanip> #include <vector> #include <queue> #include <deque> #include <set> #include <map> #include <tuple> #include <cmath> #include <numeric> #include <functional> #include <cassert> #include <atcoder/modint> #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; template<typename T> vector<vector<T>> vec2d(int n, int m, T v){ return vector<vector<T>>(n, vector<T>(m, v)); } template<typename T> vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){ return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v))); } template<typename T> void print_vector(vector<T> v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } using mint = atcoder::modint1000000007; ostream& operator<<(ostream& os, const mint& m){ os << m.val(); return os; } #define N_MAX 4002 mint inv[N_MAX],fac[N_MAX],finv[N_MAX]; void init(){ const ll MOD = mint::mod(); fac[0]=1;fac[1]=1; finv[0]=1;finv[1]=1; inv[1]=1; for(int i=2;i<N_MAX;i++){ inv[i]=mint(MOD)-inv[MOD%i]*(MOD/i); fac[i]=fac[i-1]*(ll) i; finv[i]=finv[i-1]*inv[i]; } } using P = pair<int, int>; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; init(); int n; cin >> n; vector<int> t(n), x(n); vector<P> vp; for(int i = 0; i < n; i++){ cin >> t[i] >> x[i]; if(t[i] == 1){ x[i]--; }else{ } vp.push_back({x[i], i}); } sort(vp.begin(), vp.end()); int cnt = 0; auto dp = vec2d(n+1, n+1, mint(0)); dp[0][0] = 1; for(int i = 0; i < n; i++){ auto [x, idx] = vp[i]; for(int ng = 0; ng <= i; ng++){ int small = x-(cnt+ng); if(t[idx] == 1){ if(small >= 1) dp[i+1][ng+1] += dp[i][ng]*small; dp[i+1][ng] += dp[i][ng]; }else{ if(small >= 1) dp[i+1][ng] += dp[i][ng]*small; } } if(t[idx] == 0) cnt++; } mint ans = 0; for(int ng = 0; ng <= n; ng++){ mint sgn = ng%2 == 0 ? 1 : -1; ans += sgn*dp[n][ng]*fac[n-(cnt+ng)]; } cout << ans << endl; }