Submission #3419357


Source Code Expand

#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<map>
#include<functional>
#include<set>
#include<numeric>

#pragma region
using namespace std;
#define FOR(i,r,n) for(ll i = (ll)(r); i < (ll)(n); i++)
#define rep(i,n) FOR(i,0LL,n)
#define RFOR(i,r,n) for(ll i=(ll)(n-1);i>=r;i--)
#define rrep(i,n) RFOR(i,0LL,n)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define COUNT(a,y,x) upper_bound(all(a), y) - lower_bound(all(a), x)
#define UNIQUE(a) sort(all(a)); a.erase(unique(all(a)), a.end())
#define SUM(a) accumulate(all(a),0LL)
#define pb push_back
#define endl '\n'
typedef long long int ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;
typedef map<ll, ll> MAP;
const ll inf = 2222222222222222222LL;
const ll mod = 1000000007LL;

ll n = 0, m = 0, ans = 0, cnt = 0, tmp = 0, ma = -inf, mi = inf;
string s;
bool ok = true, flag = false;
ll dx[5] = { 1,-1,0,0,0 }, dy[5] = { 0,0,1,-1,0 };
ll ddx[9] = { 1,-1,0,0,1,1,-1,-1,0 }, ddy[9] = { 0,0,1,-1,1,-1,1,-1,0 };
#pragma endregion
#define MAX 222222


vll fac, inv, invfac;
void comb_set(ll x = MAX) {
  fac.resize(x + 1, 0);
  invfac.resize(x + 1, 0);
  inv.resize(x + 1, 0);
  fac[0] = inv[1] = invfac[0] = 1;
  FOR(i, 1, x + 1) (fac[i] = fac[i - 1] * i) %= mod;
  FOR(i, 2, x + 1) (inv[i] = (mod - mod / i) * inv[mod%i]) %= mod;
  FOR(i, 1, x + 1) (invfac[i] = invfac[i - 1] * inv[i]) %= mod;
}
ll comb(ll x, ll y) {
  if (x < y) return 1;
  return fac[x] * invfac[x - y] % mod * invfac[y] % mod;
}

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


  cin >> n;
  vpll v;
  pll p;
  comb_set();
  rep(i, n) {
	cin >> p.first;
	p.second = 0;
	v.pb(p);
  }
  rep(i, n) {
	cin >> p.first;
	p.second = 1;
	v.pb(p);
  }
  sort(all(v));
  ans = 1;
  ll acnt = 0, bcnt = 0;
  if (v[0].second) bcnt++;
  else acnt++;
  FOR(i, 1, 2 * n) {
	if (v[i].second != v[i - 1].second) {
	  if (v[i].second == 0) {
		if (bcnt >= acnt) {
		  (ans *= fac[acnt]) %= mod;
		  bcnt -= acnt;
		  acnt = 0;
		}
		else {
		  (ans *= comb(max(acnt, bcnt), min(acnt, bcnt))) %= mod;
		  m = min(acnt, bcnt);
		  (ans *= fac[m]) %= mod;
		  acnt -= m;
		  bcnt -= m;
		}
	  }
	  else {
		if (acnt >= bcnt) {
		  (ans *= fac[bcnt]) %= mod;
		  acnt -= bcnt;
		  bcnt = 0;
		}
		else {
		  (ans *= comb(max(acnt, bcnt), min(acnt, bcnt))) %= mod;
		  m = min(acnt, bcnt);
		  (ans *= fac[m]) %= mod;
		  acnt -= m;
		  bcnt -= m;
		}
	  }
	}
	if (v[i].second == 0) acnt++;
	else bcnt++;
	//cout << acnt << " " << bcnt << " " << ans << endl;
  }
  (ans *= fac[acnt]) %= mod;
  cout << ans << endl;

  return 0;
}

Submission Info

Submission Time
Task A - 1D Matching
User hide1214
Language C++14 (GCC 5.4.1)
Score 500
Code Size 2872 Byte
Status AC
Exec Time 50 ms
Memory 11632 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 14
Set Name Test Cases
Sample example0.txt, example1.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, example0.txt, example1.txt
Case Name Status Exec Time Memory
000.txt AC 34 ms 7668 KB
001.txt AC 19 ms 6648 KB
002.txt AC 22 ms 6648 KB
003.txt AC 24 ms 7668 KB
004.txt AC 45 ms 11376 KB
005.txt AC 49 ms 10992 KB
006.txt AC 49 ms 10736 KB
007.txt AC 49 ms 10608 KB
008.txt AC 49 ms 11248 KB
009.txt AC 50 ms 10864 KB
010.txt AC 48 ms 11632 KB
011.txt AC 48 ms 9712 KB
example0.txt AC 11 ms 5504 KB
example1.txt AC 11 ms 5504 KB