1828C - Counting Orders - CodeForces Solution


combinatorics sortings

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <climits>
#include <set>
#include <cstring>
#include <map>
#include <cmath>

using namespace std;
typedef long long ll;
#define endl "\n";

ll G[200005];
const ll mod = 1000000007;

void solve() {
    int n;
    cin >> n;
    vector<ll> 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());
    
    if (a[0] <= b[0]) {
        cout << 0 << endl;
        return;
    }

    G[0] = n;
    ll less = 0;
    for (int i=1; i<n; i++) {
        ll diff = 0;
        while (less < n && a[less] <= b[i]) {
            less++;
            diff++;
        }
        G[i] = G[i-1] - diff;
        if (G[i] <= 0) {
            cout << 0 << endl;
            return;
        }
    }

    ll ans = 1, df = 0;
    for (int i=n-1; i>=0; i--) {
        if (G[i] - df <= 0) {
            cout << 0 << endl;
            return;
        }
        ans *= (G[i] - df) % mod;
        ans %= mod;
        df++;
    }
    cout << ans % mod << endl;
}

int main() {
    int t;
    cin >> t;
    while (t--){ 
        solve();
        memset(G, 0, sizeof(G));
    }
}


Comments

Submit
0 Comments
More Questions

13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses
Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division