結果

問題 No.1001 注文の多い順列
ユーザー momoharamomohara
提出日時 2020-03-03 22:06:33
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 2,738 bytes
コンパイル時間 1,575 ms
コンパイル使用メモリ 111,620 KB
実行使用メモリ 73,856 KB
最終ジャッジ日時 2024-04-22 00:35:03
合計ジャッジ時間 3,460 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 64 ms
61,440 KB
testcase_19 AC 58 ms
55,552 KB
testcase_20 AC 37 ms
36,992 KB
testcase_21 AC 34 ms
33,536 KB
testcase_22 AC 76 ms
73,856 KB
testcase_23 AC 76 ms
73,344 KB
testcase_24 AC 93 ms
72,960 KB
testcase_25 AC 74 ms
71,424 KB
testcase_26 AC 77 ms
70,400 KB
testcase_27 AC 78 ms
71,680 KB
testcase_28 AC 78 ms
72,064 KB
testcase_29 AC 73 ms
72,960 KB
testcase_30 AC 70 ms
69,760 KB
testcase_31 AC 74 ms
72,960 KB
testcase_32 AC 74 ms
73,728 KB
testcase_33 AC 80 ms
73,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<cstring>
#include<string>
#include<cassert>
#include<cmath>
#include<climits>
#include<iomanip>
#include<bitset>
#include<unordered_map>

using namespace std;

#define REP(i,n) for(ll (i)=0;(i)<(n);(i)++)
#define rep(i,j,n) for(ll (i)=(j);(i)<(n);(i)++)
#define FOR(i,c) for(decltype((c).begin())i=(c).begin();i!=(c).end();++i)
#define ll long long
#define ull unsigned long long
#define all(hoge) (hoge).begin(),(hoge).end()
#define en '\n'
typedef pair<ll, ll> P;
const long long INF = 1LL << 60;
const int INF_INT = 1 << 25;
const long long MOD = 1e9 + 7;
typedef vector<ll> Array;
typedef vector<Array> Matrix;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
const int loose = 0;
const int tight = 1;

template<class T> inline bool chmin(T& a, T b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}
template<class T> inline bool chmax(T& a, T b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}

//グラフ関連
struct Edge {//グラフ
	ll to, cap, rev;
	Edge(ll _to, ll _cap, ll _rev) {
		to = _to; cap = _cap; rev = _rev;
	}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph& G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
	G[from].push_back(Edge(to, cap, (ll)G[to].size()));
	if (revFlag)G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	ll n;
	cin >> n;
	vec<P> xt(n);

	ll con = 0;
	REP(i, n) {
		ll t,x;
		cin >> t >> x;
		if (t == 1) {
			//x以上の条件のときは以下の場合になおす
			x--;
			con++;
		}
		xt[i] = { x,t };
	}

	sort(all(xt));//小さい方からだと、使える個数が確定する

	Matrix dp(n+1, Array(n+1, 0));//i番目までで、j個の制約を除外した場合
	dp[0][0] = 1;
	REP(i, n) {
		ll t = xt[i].second;
		ll x = xt[i].first;
		REP(j, i + 1) {
			ll num = max(x - i + j, 0LL);//使える個数
			if (t == 0) {
				(dp[i + 1][j] += dp[i][j] * num % MOD) %= MOD;
			}
			else {
				(dp[i + 1][j] += dp[i][j] * num % MOD) %= MOD;
				(dp[i + 1][j + 1] += dp[i][j]) %= MOD;//i個目の制約条件を除外
			}
		}
	}

	//除外した制約の分の余った数字は自由に並べられる->i!
	ll fact = 1;
	REP(i, n+1) {
		(dp[n][i] *= fact) %= MOD;
		(fact *= i + 1) %= MOD;
	}

	//包除原理
	ll ans = 0;
	REP(i, con+1) {
		if (i % 2 == 0) {
			(ans += dp[n][con - i]) %= MOD;//制約を偶数個含む場合は足す
		}
		else {
			(ans += MOD - dp[n][con - i]) %= MOD;//制約を奇数個含む場合は引く
		}
	}
	cout << ans << en;

	return 0;
}
0