結果

問題 No.1001 注文の多い順列
ユーザー yurujirou
提出日時 2021-12-31 13:09:32
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 273 ms / 2,000 ms
コード長 1,244 bytes
コンパイル時間 4,510 ms
コンパイル使用メモリ 235,236 KB
実行使用メモリ 65,152 KB
最終ジャッジ日時 2024-10-08 07:49:34
合計ジャッジ時間 8,121 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

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