zoukankan      html  css  js  c++  java
  • 洛谷P4141消失之物

    题目描述

    ftiasch 有 N 个物品, 体积分别是 W1, W2, …, WN。 由于她的疏忽, 第 i 个物品丢失了。 “要使用剩下的 N – 1 物品装满容积为 x 的背包,有几种方法呢?” — 这是经典的问题了。她把答案记为 Count(i, x) ,想要得到所有1 <= i <= N, 1 <= x <= M的 Count(i, x) 表格。

    输入输出格式

    输入格式:

    第1行:两个整数 N (1 ≤ N ≤ 2 × 10^3)N(1N2×103) 和 M (1 ≤ M ≤ 2 × 10^3)M(1M2×103),物品的数量和最大的容积。

    第2行: N 个整数 W1, W2, …, WN, 物品的体积。

    输出格式:

    一个 N × M 的矩阵, Count(i, x)的末位数字。

    This DP is pretty hard.

    First we should know that F[i][j] means that how many funcation what we can have when we put i's stuff in the bag which has j's volume.

    If the i's stuff had to taken, it wil  be f[i-1][j-w[i]], else, it will be f[i-1][j], So we can get the funcation :f[i][j]=f[i-1][j]+f[i-1][j-w[i];

    we can use rounded array change it to f[j]=f[j]+f[j-w[i]].

    So, how can we get the count ?

    we had to enumeration whitch stuff we had lost.

    if w[i]>j, that means, all of the answer has include the stuff i, because it was bigger than the volume. Therefore, the answer should be f[j] , which means take all of the answer.

    if w[i]<=j, that means there are some anwer has be counted. what we should do is minus the rest of stuff(except i) to pull in j-w[i]. which is f[j]-c[i][j-w[i]]

    if w[i]==0 , the c[i][j] will be 1.

    that's all.

     

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #define in(a) a=read()
    #define REP(i,k,n)  for(int i=k;i<=n;i++) 
    using namespace std;
    inline int read(){
        int x=0,f=1;
        char ch=getchar();
        for(;!isdigit(ch);ch=getchar())
            if(ch=='-')
                f=-1;
        for(;isdigit(ch);ch=getchar())
            x=x*10+ch-'0';
        return x*f;
    } 
    int n,m;
    int f[2010],c[2010][2010],w[2010]; 
    int main(){
        in(n),in(m);
        REP(i,1,n)  in(w[i]); 
        f[0]=1;  
        REP(i,1,n)
            for(int j=m;j>=w[i];j--) 
                f[j]=(f[j]+f[j-w[i]])%10;
        REP(i,1,n){
            c[i][0]=1;
            REP(j,1,m){ 
                if(j<w[i])  c[i][j]=f[j];
                else  c[i][j]=(f[j]-c[i][j-w[i]]+10)%10;
                printf("%d",c[i][j]);
            }
            printf("
    ");     
        } 
        return 0;
    } 
  • 相关阅读:
    frida rpc调用维护ios手机脚本
    latex表格调整行距
    latex插图自动在双栏的最top,IEEE期刊格式
    latex插图egin{minipage}强制左移hspace命令
    SYNTHIA-RAND-CITYSCAPES数据集云盘下载
    一种用于多张图片同时缩放比较细节的软件faststone
    shopxo安装插件被限制绑定账号的问题
    ShopXo框架去掉绑定商店的提示
    拓扑排序
    前缀和和差分
  • 原文地址:https://www.cnblogs.com/jason2003/p/10321479.html
Copyright © 2011-2022 走看看