Heros and Swords (周赛1)

Problem Description

There are n swords of different weights Wi and n heros of power Pi.

Your task is to find out how many ways the heros can carry the swords so that each hero carries exactly one sword.

Swords

Here are some rules:

(1) Every sword is carried by one hero and a hero cannot carry a sword whose weight is larger than his power.

(2) Two ways will be considered different if at least one hero carries a different sword.

Input

The first line of the input gives the number of test cases T(1 ≤ T ≤ 50).

Each case starts with a line containing an integer n (1 ≤ n ≤ 105)denoting the number of heros and swords.

The next line contains n space separated distinct integers denoting the weight of swords.

The next line contains n space separated distinct integers denoting the power for the heros.

The weights and the powers lie in the range [1, 109].

Output

For each case, output one line containing "Case #x: " followed by the number of ways those heros can carry the swords.

This number can be very big. So print the result modulo 1000 000 007.

Sample Input
3
5
1 2 3 4 5
1 2 3 4 5
2
1 3
2 2
3
2 3 4
6 3 5
Sample Output
Case #1: 1
Case #2: 0
Case #3: 4


首先排序,然后求出每个hero所能拿的sword的最大范围。
然后可以从第一个开始拿,考虑对于当前第i个hero来说,假设第i个hero最多能拿到第j个sword,那么首先这j个中一定有i-1个已经被前i-1个拿到了,所以第i个只能从后面的剩下的j-i个中选,于是可以拿j-i个。然后将每一个的可选个数乘起来即可。


#include <stdio.h>
#include <math.h>

void qsort(int a[],int l,int r)
{
    int x=a[l],i=l,j=r;
    if (l>=r) return ;
    while(i<j)
    {
        while(i<j && a[j]>=x) j--;
        a[i]=a[j];
        while(i<j && a[i]<=x) i++;
        a[j]=a[i];
    }

    a[i]=x;
    qsort(a,l,i-1);
    qsort(a,i+1,r);
}

int a[100000],b[100000];

int main()
{
    int t,n;
    int k=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);

        int i,j;

        for(i=0;i<n;i++)
        scanf("%d",&a[i]);

        for(i=0;i<n;i++)
        scanf("%d",&b[i]);

        long long  sum=1;
        j=0;

        qsort(a,0,n-1);
        qsort(b,0,n-1);

        for(i=0;i<n;i++)
        {
            while(j<n&&a[j]<=b[i])
            {
                j++;
            }

            sum=(sum*(j-i))%1000000007;
        }

        printf("Case #%d: %lld\n",++k,sum);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值