結果

問題 No.1001 注文の多い順列
ユーザー snow39snow39
提出日時 2020-02-28 22:47:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 1,480 bytes
コンパイル時間 1,251 ms
コンパイル使用メモリ 103,756 KB
実行使用メモリ 73,600 KB
最終ジャッジ日時 2024-04-21 19:55:37
合計ジャッジ時間 3,371 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 3 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 60 ms
32,128 KB
testcase_19 AC 53 ms
28,288 KB
testcase_20 AC 35 ms
20,608 KB
testcase_21 AC 32 ms
18,304 KB
testcase_22 AC 71 ms
37,760 KB
testcase_23 AC 72 ms
38,912 KB
testcase_24 AC 33 ms
38,528 KB
testcase_25 AC 71 ms
38,272 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 11 ms
5,504 KB
testcase_28 AC 19 ms
7,936 KB
testcase_29 AC 62 ms
65,792 KB
testcase_30 AC 59 ms
60,544 KB
testcase_31 AC 60 ms
61,312 KB
testcase_32 AC 66 ms
73,600 KB
testcase_33 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

void add(ll &a,ll b){
  a=(a+b)%MOD;
}

void mul(ll &a,ll b){
  a%=MOD;b%=MOD;
  a=a*b%MOD;
}

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

  int N;
  cin>>N;
  vector<int> U,D;
  rep(i,N){
    int t,x;cin>>t>>x;
    if(t==0) U.push_back(x);
    else D.push_back(x);
  }

  int S=U.size();
  int T=D.size();
  if(S) sort(U.begin(),U.end());
  if(T) sort(D.begin(),D.end());

  vector<vector<ll>> dp(N+1,vector<ll>(S+1,0));
  dp[0][0]=1;
  for(int i=0;i<N;i++){
    int val=i+1;
    for(int j=0;j<=S;j++){
      if(dp[i][j]==0) continue;
      int t=i-j;
      if(t+1<=T){
        if(D[t]<=val){
          int z=upper_bound(D.begin(),D.end(),val)-D.begin();
          ll res=dp[i][j];
          mul(res,z-t);
          add(dp[i+1][j],res);
        }
      }

      if(j+1<=S){
        if(U[j]>=val){
          int z=lower_bound(U.begin(),U.end(),val)-U.begin();
          ll res=dp[i][j];
          mul(res,j-z+1);
          add(dp[i+1][j+1],res);
        }
      }
    }
  }

  cout<<dp[N][S]<<endl;

  return 0;
}
0