1371 - Energetic Pandas

本文介绍了一个名为EnergeticPandas的问题,该问题涉及n只不同承载能力的熊猫和n根不同重量的竹子。文章详细阐述了如何通过离散化和排列的方法来计算熊猫携带竹子的所有可能方式的数量,并提供了具体的实现代码。
1371 - Energetic Pandas
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
思路:离散化+排列;
  1 #include<stdio.h>
  2 #include<algorithm>
  3 #include<iostream>
  4 #include<string.h>
  5 #include<queue>
  6 #include<stack>
  7 using namespace std;
  8 typedef long long LL;
  9 int ans[10000];
 10 int  bns[10000];
 11 int  dns[10000];
 12 int cn[10000];
 13 int aa[10000];
 14 int bb[10000];
 15 const int N=1e9+7;
 16 int main(void)
 17 {
 18         int k,i,j;
 19         scanf("%d",&k);
 20         int ca;
 21         int n;
 22         for(ca=1; ca<=k; ca++)
 23         {
 24                 scanf("%d",&n);
 25                 int cnt=0;
 26                 memset(cn,0,sizeof(cn));
 27                 for(i=0; i<n; i++)
 28                 {
 29                         scanf("%d",&ans[i]);
 30                         dns[cnt++]=ans[i];
 31                 }
 32                 for(i=0; i<n; i++)
 33                 {
 34                         scanf("%d",&bns[i]);
 35                         dns[cnt++]=bns[i];
 36                 }
 37                 sort(dns,dns+2*n);
 38                 for(i=0; i<n; i++)
 39                 {
 40                         int l=0;
 41                         int r=2*n-1;
 42                         int id=0;
 43                         while(l<=r)
 44                         {
 45                                 int mid=(l+r)/2;
 46                                 if(dns[mid]>=ans[i])
 47                                 {
 48                                         id=mid;
 49                                         r=mid-1;
 50                                 }
 51                                 else l=mid+1;
 52                         }
 53                         ans[i]=id;
 54                 }
 55                 sort(ans,ans+n);
 56                 for(i=0; i<n; i++)
 57                 {
 58                         int l=0;
 59                         int r=2*n-1;
 60                         int id=0;
 61                         while(l<=r)
 62                         {
 63                                 int mid=(l+r)/2;
 64                                 if(bns[i]<=dns[mid])
 65                                 {
 66                                         id=mid;
 67                                         r=mid-1;
 68                                 }
 69                                 else l=mid+1;
 70                         }
 71                         bns[i]=id;
 72                         cn[id]++;
 73                 }
 74                 int gg=0;
 75                 for(i=0; i<3000; i++)
 76                 {
 77                         if(cn[i]>0)
 78                         {
 79                                 aa[gg]=i;
 80                                 bb[gg++]=cn[i];
 81                         }
 82                 }
 83                 LL sum=1;
 84                 int cc=gg-1;
 85                 LL pp=0;
 86                 for(i=n-1; i>=0; i--)
 87                 {
 88 
 89                         while(aa[cc]>=ans[i]&&cc>=0)
 90                         {
 91                                 pp=(pp+bb[cc]);
 92                                 cc --;
 93                         }
 94                         if(pp>0)
 95                         {
 96                                 sum=(sum*pp)%N;
 97                                 pp-=1;
 98                         }
 99                         else
100                         {
101                                 sum=0;
102                                 break;
103                         }
104                 }printf("Case %d: ",ca);
105                 printf("%lld\n",sum);
106         }
107         return 0;
108 }

 

转载于:https://www.cnblogs.com/zzuli2sjy/p/5450520.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值