zoukankan      html  css  js  c++  java
  • Big Event in HDU(HDU 1171 多重背包)

    Big Event in HDU

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 32578    Accepted Submission(s): 11377


    Problem Description
    Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don't know that Computer College had ever been split into Computer College and Software College in 2002.
    The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).
     
    Input
    Input contains multiple test cases. Each test case starts with a number N (0 < N <= 50 -- the total number of different facilities). The next N lines contain an integer V (0<V<=50 --value of facility) and an integer M (0<M<=100 --corresponding number of the facilities) each. You can assume that all V are different.
    A test case starting with a negative integer terminates input and this test case is not to be processed.
     
    Output
    For each case, print one line containing two integers A and B which denote the value of Computer College and Software College will get respectively. A and B should be as equal as possible. At the same time, you should guarantee that A is not less than B.
     
    Sample Input
    2
    10 1
    20 1
    3
    10 1
    20 2
    30 1
    -1
     
    Sample Output
    20 10
    40 40
    不明白在二进制优化时位运算要比自己乘2慢好多。
     1 #include <cstring>
     2 #include <algorithm>
     3 #include <iostream>
     4 #include <cstdio>
     5 using namespace std;
     6 struct node                /*多重背包最终模板*/
     7 {
     8     int v,m;
     9 }a[1000];
    10 int dp[1000010];
    11 int sum;
    12 void completepack(int cost)
    13 {
    14     for(int i=cost;i<=sum;i++)
    15         dp[i]=max(dp[i],dp[i-cost]+cost);
    16     return;
    17 }
    18 void zeroonepack(int cost)
    19 {
    20     for(int i=sum;i>=cost;i--)
    21         dp[i]=max(dp[i],dp[i-cost]+cost);
    22     return;
    23 }
    24 void multiplepack(int cost,int amount)
    25 {
    26     int t=1;
    27     if(cost*amount>sum)            //转化为完全背包,可视为无限多
    28         completepack(cost);
    29     else                            //二进制优化,将amount分解
    30     {
    31         for(int k=1;k<amount;k*=2)
    32         {
    33             zeroonepack(k*cost);
    34             amount-=k;
    35         }
    36         if(amount)
    37             zeroonepack(amount*cost);
    38     }
    39     return;
    40 }
    41 int main()
    42 {
    43     int i,j,k;
    44     int n;
    45     freopen("in.txt","r",stdin);
    46     while(~scanf("%d",&n)&&n>=0)
    47     {
    48         sum=0;
    49         int t=0;
    50         for(i=1;i<=n;i++)
    51         {
    52             scanf("%d%d",&a[i].v,&a[i].m);
    53             t+=a[i].v*a[i].m;
    54         }
    55         sum=t/2;
    56         memset(dp,0,sizeof(dp));
    57         for(i=1;i<=n;i++)
    58             multiplepack(a[i].v,a[i].m);
    59         printf("%d %d
    ",t-dp[sum],dp[sum]);
    60     }
    61     return 0;
    62 }
     模板:
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<queue>
     7 using namespace std;
     8 const int maxn=100005;
     9 int n,m,t;
    10 int dp[maxn],w[maxn],v[maxn];
    11 int nValue;
    12 //0-1背包,代价为cost,获得的价值为weight
    13 void ZeroOnePack(int cost,int weight)
    14 {
    15     for(int i=nValue;i>=cost;i--)
    16       dp[i]=max(dp[i],dp[i-cost]+weight);
    17 }
    18 
    19 //完全背包,代价为cost,获得的价值为weight
    20 void CompletePack(int cost,int weight)
    21 {
    22     for(int i=cost;i<=nValue;i++)
    23       dp[i]=max(dp[i],dp[i-cost]+weight);
    24 }
    25 
    26 //多重背包
    27 void MultiplePack(int cost,int weight,int amount)
    28 {
    29     if(cost*amount>=nValue) CompletePack(cost,weight);
    30     else
    31     {
    32         int k=1;
    33         while(k<amount)
    34         {
    35             ZeroOnePack(k*cost,k*weight);
    36             amount-=k;
    37             k<<=1;
    38         }
    39         ZeroOnePack(amount*cost,amount*weight);//这个不要忘记了,经常掉了
    40     }
    41 }
    42 int main()
    43 {
    44     int i,j,k;
    45     #ifndef ONLINE_JUDGE
    46     freopen("1.in","r",stdin);
    47     #endif
    48     while(scanf("%d%d",&nValue,&n)!=EOF)
    49     {
    50         for(i=0;i<n;i++)
    51         {
    52             scanf("%d%d",&w[i],&v[i]);
    53         }
    54         memset(dp,0,sizeof(dp));
    55         for(i=0;i<n;i++)
    56         {
    57             MultiplePack(v[i],v[i],w[i]);
    58         }
    59         printf("%d
    ",dp[nValue]);
    60     }
    61 }
  • 相关阅读:
    云级Keyvalue数据库大比较
    如何诊断ORA125XX连接问题
    Oracle Internal Event:10235 Heap Checking诊断事件
    Oracle Database 11g R2 在Oracle Linux 6上获得认证
    一张图帮你搞懂oracle UGA User Global Area的概念
    Oracle Buffer Cache Chain图解
    ORA04036: 实例使用的 PGA 内存超出 PGA_AGGREGATE_LIMIT
    如何诊断ASMLIB故障
    Oracle RDBMS Server 11gR2 Preinstall RPM For Oracle Linux 6
    图解Oracle Buffer Cache LRU List
  • 原文地址:https://www.cnblogs.com/a1225234/p/5278830.html
Copyright © 2011-2022 走看看