BNU 13289 Energetic Pandas DP

本文解析了一道经典的组合数学算法题目,涉及n只不同承重能力的熊猫运送n根不同重量的竹子的所有可能方式,并给出了两种不同的解题思路及代码实现。

                         Energetic Pandas 

 

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

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

 

 

题意:给你n个容器,n个物品,每个人容器物品对应一个大小,容器大于等于才可将这个物品放入容器中,物品保证每个容器都放一个物品的情况下,问你方案数是多少

题解:  考虑前i个容器有多少个可以放第i个物品为 num,

            则dp[i]表示前i个物品放完的方案数,dp[i]=dp[i-1]*(num)

           可以预处理出可以放几个

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=1005;
const int Mod=1000000007;
int a[1005],b[1005],dp[1005];
int main()
{
    int T;
    scanf("%d",&T);
    for(int ca=1;ca<=T;ca++)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&b[i]);
        sort(a+1,a+n+1);
        sort(b+1,b+n+1);
        dp[0]=1;
        for(int i=1;i<=n;i++)
        {
            int cnt=0;
            for(int j=1;j<=i;j++)
                cnt+=(b[j]>=a[i]);
            dp[i]=1LL*cnt*dp[i-1]%Mod;
        }
        printf("Case %d: %d\n",ca,dp[n]);
    }
    return 0;
}
Q神
///1085422276
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));

inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';ch=getchar();
    }return x*f;
}
//****************************************
const double PI = 3.1415926535897932384626433832795;
const double EPS = 5e-9;
#define maxn 1000+5
#define mod 1000000007

int a[maxn],b[maxn],tmp[maxn],n;
int main() {
    int T=read(),oo=1;
    while(T--) {
        scanf("%d",&n);
        for(int i=1;i<=n;i++) {
            scanf("%d",&a[i]);
        }
        for(int i=1;i<=n;i++) {
            scanf("%d",&b[i]);
        }
        sort(a+1,a+n+1);
        sort(b+1,b+n+1);
        int j=1;bool flag=0;
        for(int i=1;i<=n;i++) {
            while(b[i]>=a[j]&&j<=n) {
                j++;
            }
            j--;tmp[i]=j;
            if(tmp[i]<i) {
                flag=1;
            }
        }printf("Case %d: ",oo++);
        if(flag){
           cout<<"0"<<endl;continue;
        }ll dp[maxn];mem(dp);
        for(int i=1;i<=n;i++) {
                if(i==1)
            dp[i]=tmp[1];
            else {
                if(tmp[i]==i) {
                    dp[i]=dp[i-1];
                }
                else {
                    dp[i]=dp[i-1]*(tmp[i]-tmp[i-1])%mod;
                    if(tmp[i-1]>i-1) dp[i]=(dp[i]+dp[i-1]*(tmp[i-1]-i+1))%mod;
                }
            }
        }
        cout<<dp[n]<<endl;
    }
    return 0;
}
蒟蒻

 

转载于:https://www.cnblogs.com/zxhl/p/4980398.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值