結果
| 問題 |
No.1001 注文の多い順列
|
| コンテスト | |
| ユーザー |
ゆきのん
|
| 提出日時 | 2020-02-28 23:16:48 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 79 ms / 2,000 ms |
| コード長 | 1,575 bytes |
| コンパイル時間 | 1,967 ms |
| コンパイル使用メモリ | 176,020 KB |
| 実行使用メモリ | 73,728 KB |
| 最終ジャッジ日時 | 2024-10-13 19:07:43 |
| 合計ジャッジ時間 | 3,855 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 31 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<LL,LL> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const LL mod=1000000007;
const LL LINF=1LL<<62;
const int INF=1<<30;
int dx[]={1,0,-1,0,1,-1,1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};
int main(){
int n;cin >> n;
vector<int> a,b;
for (int i = 0; i < n; i++) {
int t,x;cin >> t >> x;
x--;
if(t==1) a.pb(x);
else b.pb(x);
}
sort(ALL(a));
sort(ALL(b));
int A=a.size(),B=b.size();
vector<vector<LL>> dp(n+1,vector<LL> (n+1,0));
int pa=0,pb=0;
dp[0][0] = 1;
for (int i = 0; i < n; i++) {
LL cnta = 0,cntb = 0;
while(pa < A && a[pa] <= i) pa++;
while(pb < B && b[pb] <= i){
pb++;
cntb++;
}
for (int j = 0; j <= i; j++) {
if(dp[i][j] == 0) continue;
if(j-cntb>=0&&pa-(i-(j-cntb+pb))>=0) dp[i+1][j-cntb] = (dp[i+1][j-cntb] + dp[i][j] * (pa-(i-(j-cntb+pb))))%mod; //aとマッチング
if(j+1-cntb >= 0) dp[i+1][j+1-cntb] = (dp[i+1][j+1-cntb] + (j + 1) * dp[i][j])%mod; //bとマッチング
}
}
cout << dp[n][0] << endl;
return 0;
}
ゆきのん