您的位置:首页 > 其它

1371 - Energetic Pandas

2016-05-01 14:58 429 查看
1371 - Energetic Pandas

PDF (English) Statistics Forum
Time Limit: 2 second(s)Memory Limit: 32 MB
There are n bamboos of different weights Wi. There are n pandas of different capacity CAPi. How many ways the pandas can carry the bamboos so that each panda carries exactly one bamboo, every bamboo is carried by one panda and a panda cannot carry a bamboo that is heavier than its capacity. Two ways will be considered different if at least one panda carries a different bamboo.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 1000) denoting the number of pandas and bamboos. The next line contains n space separated distinct integers denoting the weights of be bamboos. The next line contains n space separated distinct integers denoting the capacities for the pandas. The weights and the capacities lie in the range [1, 109].

Output

For each case, print the case number and the number of ways those pandas can carry the bamboos. This number can be very big. So print the result modulo 1000 000 007.

Sample Input

Output for 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

Case 1: 1

Case 2: 0

Case 3: 4

Problem Setter: F.A. Rezaur Rahman Chowdhury
Special Thanks: Jane Alam Jan
思路:离散化+排列;

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<queue>
#include<stack>
using namespace std;
typedef long long LL;
int ans[10000];
int  bns[10000];
int  dns[10000];
int cn[10000];
int aa[10000];
int bb[10000];
const int N=1e9+7;
int main(void)
{
int k,i,j;
scanf("%d",&k);
int ca;
int n;
for(ca=1; ca<=k; ca++)
{
scanf("%d",&n);
int cnt=0;
memset(cn,0,sizeof(cn));
for(i=0; i<n; i++)
{
scanf("%d",&ans[i]);
dns[cnt++]=ans[i];
}
for(i=0; i<n; i++)
{
scanf("%d",&bns[i]);
dns[cnt++]=bns[i];
}
sort(dns,dns+2*n);
for(i=0; i<n; i++)
{
int l=0;
int r=2*n-1;
int id=0;
while(l<=r)
{
int mid=(l+r)/2;
if(dns[mid]>=ans[i])
{
id=mid;
r=mid-1;
}
else l=mid+1;
}
ans[i]=id;
}
sort(ans,ans+n);
for(i=0; i<n; i++)
{
int l=0;
int r=2*n-1;
int id=0;
while(l<=r)
{
int mid=(l+r)/2;
if(bns[i]<=dns[mid])
{
id=mid;
r=mid-1;
}
else l=mid+1;
}
bns[i]=id;
cn[id]++;
}
int gg=0;
for(i=0; i<3000; i++)
{
if(cn[i]>0)
{
aa[gg]=i;
bb[gg++]=cn[i];
}
}
LL sum=1;
int cc=gg-1;
LL pp=0;
for(i=n-1; i>=0; i--)
{

while(aa[cc]>=ans[i]&&cc>=0)
{
pp=(pp+bb[cc]);
cc --;
}
if(pp>0)
{
sum=(sum*pp)%N;
pp-=1;
}
else
{
sum=0;
break;
}
}printf("Case %d: ",ca);
printf("%lld\n",sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: