【HDU 1171】Big Event in HDU 多重背包巧妙优化 DP

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

题意:给一个序列,每个元素有若干个,问能凑到的最贴近sum/2的值

思路:

本来是一个多重背包的问题,但是可以简化成01背包。用dp[i]表示能否凑到i这么多的数,那么状态转移方程就是对于一个a[j].val,我要看dp[i - a[j].val]是否能凑到,连这个数都凑不肯定凑不到我现在的i,如果凑得到,还要看当初凑到i - a[j].val的时候还剩下多少个a[j].val,如果剩下的值>=1,就可以dp[i] = 1了(表示凑得到)。所以就相当于一个普通的01背包,同时记录一下Left数组,表示凑到i的时候还剩下多少个a[j],val。详见代码。

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 3e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline int read(){ int f = 1; int x = 0;char ch = getchar();while(ch>'9'|ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

int dp[maxn];
int Left[105];

typedef struct Pos
{
    int val;
    int m;
}P;
P a[55];

int main()
{
    int n;
    while(~scanf("%d",&n)&&n>=0)
    {
        int pos = 0; int sum = 0; mem(dp,0);
        rep(i,1,n)
        {
            int val = read(), m = read();
            sum += val * m;
            a[i].val = val, a[i].m = m;
        }
        dp[0] = 1;
        rep(i,1,n)
        {
            rep(j,0,sum/2) Left[j] = a[i].m;        //每次面值的数量初始化一下
            rep(j,a[i].val,sum/2) if(!dp[j]&&dp[j - a[i].val]&&Left[j - a[i].val]>=1) dp[j] = 1, Left[j] = Left[j - a[i].val] - 1;      //当前还没凑到且前一位凑到了而且有剩
        }
        int ans = 0;
        per(i,sum/2,1) if(dp[i]) {ans=i;break;}
        printf("%d %d\n", sum-ans, ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45492531/article/details/107689553
今日推荐