HDU 1171 Big Event in HDU(多重背包),hdu1171

來源:互聯網
上載者:User

HDU 1171 Big Event in HDU(多重背包),hdu1171

Big Event in HDUProblem DescriptionNowadays, 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).
 
InputInput 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.
 
OutputFor 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
210 120 1310 1 20 230 1-1
 
Sample Output
20 1040 40
 

題意  把一堆東西盡量分為兩份  第一份不小於第二份

把所有東西的總價值s除以2  讓它裝盡量多的東西作為第二份  剩下的就是第一份了 

題目有個小坑點  是以負數作為結束條件的  不是-1  還有不要開始把s/=2  後來第一份又用s*2-d[s]  因為s/2*2不一定等於s了 

<span style="font-family:Arial Black;">#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 55,  V = 255000;int d[V], val[N], num[N];int main(){    int n;    while (scanf ("%d", &n),n>0)    {        int s = 0;        memset(d,0,sizeof(d));        for (int i = 1; i <= n; ++i)        {            scanf ("%d%d", &val[i], &num[i]);            s += val[i] * num[i];        }                for (int i = 1; i <= n; ++i)        {            for (int k = 1; k<=num[i]; k *=2)            {                num[i] -= k;                for (int j = s / 2; j >= k * val[i]; --j)                    d[j] = max (d[j], d[j - k * val[i]] + k * val[i]);            }            if (num[i] != 0)                for (int j = s / 2; j >= num[i] * val[i]; --j)                    d[j] = max (d[j], d[j - num[i] * val[i]] + num[i] * val[i]);        }        printf ("%d %d\n", s - d[s/2], d[s/2]);    }    return 0;}</span>



HDOJ 1171 Big Event in HDU 為何逾時

看你這麼多for就知道要逾時,
輸入n對數
c1[i] c2[i]
全部相乘累加得到sum
你只需用背包解決小於等於sum/2的 最大的和數t
結果就是sum-t t
 
hdu 1171 指教,為何過不了的 (簡單背包問題)

這個題目我是用母函數做的 你要考慮有重複資料的情況 那個時候背包的處理是很微妙的
下面是我的母函數的代碼
#include<stdio.h>
int main()
{
int n,i,j,k,pj;
int a[51][2],c1[250001],c2[250002];
while(~scanf("%d",&n))
{
if(n<0)break;
pj=0;
for(i=0;i<250001;i++)
{
c1[i]=0;c2[i]=0;
}
for(i=1;i<=n;i++)
{
scanf("%d%d",&a[i][0],&a[i][1]);
pj+=a[i][0]*a[i][1];
}
c1[0]=1;
for(i=1;i<=a[1][1];i++)
c1[a[1][0]*i]=1;
for(i=2;i<=n;i++)
{
for(j=0;j<=pj;j++)
{
for(k=0;k<=a[i][0]*a[i][1];k+=a[i][0])
{
c2[j+k]+=c1[j];
}
}
for(j=0;j<=pj;j++)
{
c1[j]=c2[j];
c2[j]=0;
}
}
for (i=pj/2;i>=0;--i)
{
if (c1[i]!=0)
{
printf("%d %d\n",pj-i,i);
break;
}
}
}
return 0;
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.