結果

問題 No.1001 注文の多い順列
ユーザー startcppstartcpp
提出日時 2020-02-28 22:36:25
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 149 ms / 2,000 ms
コード長 1,649 bytes
コンパイル時間 743 ms
コンパイル使用メモリ 75,684 KB
実行使用メモリ 49,792 KB
最終ジャッジ日時 2024-04-21 19:44:21
合計ジャッジ時間 2,745 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 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 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 4 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 60 ms
28,800 KB
testcase_19 AC 58 ms
27,392 KB
testcase_20 AC 38 ms
19,968 KB
testcase_21 AC 36 ms
19,072 KB
testcase_22 AC 73 ms
33,536 KB
testcase_23 AC 70 ms
32,896 KB
testcase_24 AC 70 ms
32,640 KB
testcase_25 AC 68 ms
32,128 KB
testcase_26 AC 102 ms
48,256 KB
testcase_27 AC 99 ms
47,104 KB
testcase_28 AC 92 ms
45,568 KB
testcase_29 AC 25 ms
19,328 KB
testcase_30 AC 28 ms
19,840 KB
testcase_31 AC 32 ms
21,760 KB
testcase_32 AC 16 ms
15,744 KB
testcase_33 AC 149 ms
49,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//貪欲+包除
#include <iostream>
#include <vector>
#include <algorithm>
#define rep(i, n) for(i = 0; i < n; i++)
#define int long long
using namespace std;
typedef pair<int, int> P;

int mod = 1000000007;
int n;
int t[3000], x[3000];
int rt[3001];			//rt[i] = t[0] + … + t[i - 1]
int dp[3001][3001];		//dp[i][j] = 個数 (t[i] == 0:全部守る. t[i] == 1: j個を選んで違反. それ以外は違反してもしなくてもいい(1通りとして後回し).)
int fact[3001];

signed main() {
	int i, j;
	
	cin >> n;
	rep(i, n) cin >> t[i] >> x[i];
	
	P pa[3000];
	rep(i, n) {
		if (t[i] == 0) pa[i] = P(x[i], t[i]);
		else pa[i] = P(x[i] - 1, t[i]);
	}
	sort(pa, pa + n);
	rep(i, n) { t[i] = pa[i].second; x[i] = pa[i].first; }
	rep(i, n) { rt[i + 1] = rt[i] + t[i]; }
	
	//rep(i, n) { cout << t[i] << ", " << x[i] << endl; }
	
	dp[0][0] = 1;
	rep(i, n) {
		rep(j, rt[i] + 1) {
			int tempura = i - (rt[i] - j);
			
			if (t[i] == 0) {
				if (x[i] - tempura > 0) {
					dp[i + 1][j] += dp[i][j] * (x[i] - tempura);
					dp[i + 1][j] %= mod;
				}
			}
			
			else {
				dp[i + 1][j] += dp[i][j];
				dp[i + 1][j] %= mod;
				
				if (x[i] - tempura > 0) {
					dp[i + 1][j + 1] += dp[i][j] * (x[i] - tempura);
					dp[i + 1][j + 1] %= mod;
				}
			}
		}
	}
	
	/*rep(i, n + 1) {
		rep(j, i + 1) {
			cout << dp[i][j] << " ";
		}
		cout << endl;
	}*/
	
	fact[0] = 1;
	rep(i, n) { fact[i + 1] = (i + 1) * fact[i] % mod; }
	
	int ans = 0;
	rep(j, rt[n] + 1) {
		int res = dp[n][j];
		res *= fact[rt[n] - j];
		res %= mod;
		
		if (j % 2 == 0) { ans += res; }
		else { ans += mod - res; }
		ans %= mod;
	}
	
	cout << ans << endl;
	return 0;
}
0