Submission #1419813


Source Code Expand

#pragma region include
#include <iostream>
#include <iomanip>
#include <stdio.h>

#include <sstream>
#include <algorithm>
#include <cmath>
#include <complex>

#include <string>
#include <cstring>
#include <vector>
#include <tuple>
#include <bitset>

#include <queue>
#include <complex>
#include <set>
#include <map>
#include <stack>
#include <list>

#include <fstream>
#include <random>
//#include <time.h>
#include <ctime>
#pragma endregion //#include
/////////
#define REP(i, x, n) for(int i = x; i < n; ++i)
#define rep(i,n) REP(i,0,n)
/////////
#pragma region typedef
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
#pragma endregion //typedef
////定数
const int INF = (int)1e9;
const LL MOD = (LL)1e9+7;
const LL LINF = (LL)1e18;
const double PI = acos(-1.0);
const double EPS = 1e-9;
/////////
using namespace::std;

void solve(){
	int N;
	cin >> N;
	vector< pair<int,int> > pos(2*N);
	for(int i=0;i<N;++i){
		cin >> pos[i].first;
		pos[i].second = 0;
	}
	for(int i=0;i<N;++i){
		cin >> pos[i+N].first;
		pos[i+N].second = 1;
	}
	//
	sort(pos.begin(), pos.end() );
	
	{
		LL ans = 1;
		LL num = 0;
		int kind = -1;
		for(int i=0;i<2*N;++i){
			if( kind == -1 ){
				kind = pos[i].second;
				num = 1;
			}
			else if( pos[i].second == kind ){
				++num;
			}else{
				ans = (ans * num) % MOD;
				--num;
				if( num == 0 ){
					kind = -1;
				}
			}
		}
		cout << ans << endl;
		return;
	}

	vector<int> A(2,0),need(2,0);
	vector<LL> fact(N+1,0);
	fact[1] = 1;
	for(LL i=2;i<=N;++i){
		fact[2] = (fact[i-1]*i) % MOD;
	}
	LL ans = 1;
	int prev = pos[0].second;
	++A[prev];
	for(int i=1;i<2*N;++i){
		int kind = pos[i].second;
		if( need[kind] ){
			--need[kind];
			continue;
		}
		if( prev == kind ){
			A[ kind ]++;
		}else{
			ans = ( ans * fact[ A[prev] ] ) % MOD;
			need[(kind+1)%2] += A[prev];
			A[prev] = 0;
			prev = kind;
		}
	}
	cout << ans << endl;
}

#pragma region main
signed main(void){
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	std::cout << std::fixed;//小数を10進数表示
	cout << setprecision(16);//小数点以下の桁数を指定//coutとcerrで別	

	solve();
}
#pragma endregion //main()

Submission Info

Submission Time
Task A - 1D Matching
User akarin55
Language C++14 (GCC 5.4.1)
Score 500
Code Size 2289 Byte
Status AC
Exec Time 38 ms
Memory 1792 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 23 ms 1152 KB
001.txt AC 9 ms 640 KB
002.txt AC 12 ms 768 KB
003.txt AC 13 ms 768 KB
004.txt AC 34 ms 1664 KB
005.txt AC 38 ms 1792 KB
006.txt AC 38 ms 1792 KB
007.txt AC 38 ms 1792 KB
008.txt AC 38 ms 1792 KB
009.txt AC 38 ms 1792 KB
010.txt AC 37 ms 1792 KB
011.txt AC 37 ms 1792 KB
example0.txt AC 1 ms 256 KB
example1.txt AC 1 ms 256 KB