列表

详情


NC222461. [USACOJan2021B]JustStalling

描述

Farmer John has N cows (1≤N≤20) of heights . His barn has N stalls with max height limits (so for example, if , then a cow of height at most 17 can reside in stall 5). In how many distinct ways can Farmer John arrange his cows so that each cow is in a different stall, and so that the height limit is satisfied for every stall?

输入描述

The first line contains N. The second line contains N space-separated integers . The third line contains N space-separated integers . All heights and limits are in the range .

输出描述

The number of ways Farmer John can place each cow into a different stall such that the height limit is satisfied for every stall. Note that the large size of the output might require the use of a 64-bit integer, like a "long long" in C++.

示例1

输入:

4
1 2 3 4
2 4 3 4

输出:

8

说明:

In this example, we cannot place the third cow into the first stall since 3=a3>b1=2. Similarly, we cannot place the fourth cow into the first or third stalls. One way to satisfy the height limits is to assign cow 1 to stall 1, cow 2 to stall 2, cow 3 to stall 3, and cow 4 to stall 4.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 3ms, 内存消耗: 420K, 提交时间: 2021-12-04 10:47:31

#include<iostream>
#include<algorithm>
using namespace std;

int cows[30];
int stalls[30];
int chooseNumber[30];

int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>cows[i];
    }
    for(int i=0;i<n;i++){
        cin>>stalls[i];
    }
    
    sort(cows,cows+n);
    sort(stalls,stalls+n);
    
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(cows[j]<=stalls[i]){
                chooseNumber[i]++;
            }else{
                break;
            }
        }
    }
    
    long long res=chooseNumber[0];
    
    for(int i=1;i<n;i++){
        res*=(chooseNumber[i]-i);
    }
    
    cout<<res<<endl;
    return 0;
}

上一题