Codeforces Round 873 (Div. 1) 1827A. Counting Orders Solution
Problem Link: https://codeforces.com/problemset/problem/1827/A
Solution in C++:
- /// Author : AH_Tonmoy
- #include <bits/stdc++.h>
- using namespace std;
- const int mod = 1e9 + 7 ;
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t;
- cin >> t;
- while (t--) {
- int n ; cin >> n ;
- vector<int>a(n),b(n) ;
- for ( int i = 0 ; i < n ; i++){
- cin >> a[i] ;
- }
- for ( int i = 0 ; i < n ; i++){
- cin >> b[i] ;
- }
- sort(a.begin(),a.end()) ;
- sort(b.begin(),b.end()) ;
- long long ans = - 1;
- for ( int i = 0 ; i < n ; i++){
- if(a[i] <= b[i]){
- ans = 0 ;
- break ;
- }
- }
- if(ans == 0){
- cout <<0<<'\n';
- continue ;
- }
- ans = 1 ;
- for ( int i = 0 ; i < n ; i++){
- int low = i , high = n - 1 ;
- int index ;
- while(low <= high){
- int mid = low + ( ( high - low ) / 2 );
- if( a[i] > b[mid] ){
- index = mid ;
- low = mid + 1 ;
- }
- else {
- high = mid - 1 ;
- }
- }
- long long value = index - i + 1 ;
- ans *= value ;
- ans %= mod ;
- }
- cout <<ans <<'\n';
- }
- return 0 ;
- }
No comments