結果

問題 No.1001 注文の多い順列
ユーザー yurujirouyurujirou
提出日時 2021-12-31 13:09:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 266 ms / 2,000 ms
コード長 1,244 bytes
コンパイル時間 4,390 ms
コンパイル使用メモリ 228,152 KB
実行使用メモリ 65,280 KB
最終ジャッジ日時 2024-04-17 00:17:30
合計ジャッジ時間 7,548 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 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 3 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 5 ms
6,272 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 105 ms
41,984 KB
testcase_19 AC 98 ms
39,680 KB
testcase_20 AC 61 ms
29,696 KB
testcase_21 AC 57 ms
27,904 KB
testcase_22 AC 135 ms
47,744 KB
testcase_23 AC 126 ms
46,848 KB
testcase_24 AC 125 ms
46,720 KB
testcase_25 AC 120 ms
45,952 KB
testcase_26 AC 248 ms
62,976 KB
testcase_27 AC 248 ms
62,848 KB
testcase_28 AC 238 ms
61,824 KB
testcase_29 AC 44 ms
31,744 KB
testcase_30 AC 48 ms
32,128 KB
testcase_31 AC 56 ms
34,432 KB
testcase_32 AC 25 ms
28,032 KB
testcase_33 AC 266 ms
65,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#include <atcoder/all>
using namespace atcoder;

#define REP(i,n) for(int i = 0; i < (int)n; i++)
#define RREP(i,n) for(int i = (int)n-1; i >= 0; i--)
#define LREP(i,n) for(LL i = 0; i < (LL)n; i++)

#define Vi vector<int>
#define Vl vector<long long>
#define P pair<int, int>
#define LP pair<long long ,long long>
#define T3 tuple<LL, LL, LL>
#define T4 tuple<LL, LL, LL, LL>
#define INF 1000000007
#define SIZE 1000100
#define MOD 1000000007
typedef long long LL;

LL N;
vector<LP> A;

bool done[5000][5000];
LL memo[5000][5000];
LL dfs(LL i, LL j) {
	if (done[i][j]) return memo[i][j];
	LL res = 0;
	if (i == N) {
		res = 1;
		for (LL k = j; k < N; k++) {
			res = (res * (N - k)) % MOD;
		}
	}
	else {
		if (A[i].second == 0) {
			res += ((A[i].first - j) * dfs(i + 1, j + 1)) % MOD;
		}
		else {
			res += dfs(i + 1, j) % MOD;
			res -= ((A[i].first - j) * dfs(i + 1, j + 1)) % MOD;
		}
	}
	res %= MOD;
	done[i][j] = true;
	return memo[i][j] = res;
}

int main() {
	cin >> N;
	REP(i, N) {
		LL t, x;
		cin >> t >> x;
		if (t == 1) x--;
		A.push_back(LP(x, t));
	}
	sort(A.begin(), A.end());
	cout << (MOD + dfs(0, 0)) % MOD << endl;
}
0