在一条线上有两组点 连起来最短的方案个数

A - 1D Matching
Time limit : 2sec / Memory limit : 256MB

Score : 500 points

Problem Statement
There are N computers and N sockets in a one-dimensional world. The coordinate of the i-th computer is ai, and the coordinate of the i-th socket is bi. It is guaranteed that these 2N coordinates are pairwise distinct.

Snuke wants to connect each computer to a socket using a cable. Each socket can be connected to only one computer.

In how many ways can he minimize the total length of the cables? Compute the answer modulo 109+7.

Constraints
1≤N≤105
0≤ai,bi≤109
The coordinates are integers.
The coordinates are pairwise distinct.
#include<bits/stdc++.h>
using namespace std;

const int mod = 1e9+7;

int main(){
    int n,a,cnt=0;
    long long ans=1;
    vector<pair<int,int> > v;

    cin >> n;
    for(int i=0;i<2;i++){
        for(int j=0;j<n;j++){
            cin >> a;
            v.push_back(pair<int,int>(a,i));
        }
    }
    sort(v.begin(),v.end());

    for(int i=0;i<v.size();i++){
        if(v[i].second==1){
            if(cnt < 0) ans = ans * -cnt % mod;
            cnt++;
        }
        else{
            if(cnt > 0) ans = ans * cnt % mod;
            cnt--;
        }
    }

    cout << ans << endl;
    return 0;
}

results matching ""

    No results matching ""