結果

問題 No.1001 注文の多い順列
コンテスト
ユーザー vjudge1
提出日時 2026-10-05 12:35:16
言語 C++17(gcc12)
(gcc 12.4.0 + boost 1.92.0 + ACL)
コンパイル:
g++-12 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 28 ms / 2,000 ms
+ 872µs
コード長 890 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,299 ms
コンパイル使用メモリ 203,352 KB
実行使用メモリ 18,304 KB
最終ジャッジ日時 2026-10-05 12:35:25
合計ジャッジ時間 3,964 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int,int>
#define ull unsigned long long
#define add(a,b) (a+=b,a=a>=mod?a-mod:a)
const int mod=1e9+7;
const int N=3010;
int cnt0[N],cnt1[N];
int dp[N][N];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int n;cin>>n;int s0=0,s1=0;
	for(int i=1;i<=n;i++){
		int t,x;cin>>t>>x;
		if(t)cnt1[x]++,s1++;
		else cnt0[x]++,s0++;
	}
	for(int i=1;i<=n;i++)cnt1[i]+=cnt1[i-1];
	for(int i=n;i>=1;i--)cnt0[i]+=cnt0[i+1];
	dp[0][0]=1;
	for(int i=0;i<n;i++){
		for(int c0=0;c0<=min(i,s0);c0++){
			int c1=i-c0;if(c1>s1||!dp[c0][c1])continue;
//			if(i==2)cout<<dp[c0][c1]<<" "<<c0<<" "<<c1<<"\n";
			int v0=cnt0[i+1]-(s0-c0-1),v1=cnt1[i+1]-c1;
			if(v0>0&&c0+1<=s0)add(dp[c0+1][c1],dp[c0][c1]*v0%mod);
			if(v1>0&&c1+1<=s1)add(dp[c0][c1+1],dp[c0][c1]*v1%mod);
		}
	}
	cout<<dp[s0][s1];
	return 0;
}
0