其他分享
首页 > 其他分享> > HDU 1187 Big Event in HDU

HDU 1187 Big Event in HDU

作者:互联网

题意:有n个物品,希望尽可能均分

尽可能均分,就是找尽可能接近sum/2的方案

考虑生成函数 \(G(x)=(1+x^{v[i]}+x^{2v[i]}+...+x^{a[i]v[i]})(...\)

#include<bits/stdc++.h>

using namespace std;

int rd(){
  int ret=0, f=1;char c;
  while(c=getchar(),!isdigit(c))f=c=='-'?-1:1;
  while(isdigit(c))ret=ret*10+c-'0',c=getchar();
  return ret*f;
}

const int MAXN = 500005;

int n;

int val[MAXN],num[MAXN],sum,all;

int f[MAXN][2];

void solve(){
  sum=0;
  all=0;
  for(int i=1;i<=n;i++){
    val[i]=rd();num[i]=rd();
    sum+=val[i]*num[i];
  }
  all=sum;
  memset(f,0,sizeof(f));
  f[0][0]=1;
  for(int i=1;i<=n;i++){
    int u=i&1,v=1-(i&1);
    for(int j=0;j<=sum;j++){
      for(int k=0;k<=num[i]&&k*val[i]+j<=sum;k++){
        f[j+k*val[i]][u]+=f[j][v];
      }
    }
    for(int j=0;j<=sum;j++){
      f[j][v]=0;
    }
  }
  for(int i=sum/2;i>=0;i--){
    if(f[i][n&1]){
      cout<<all-i<<" "<<i<<endl;
      return;
    }
  }
}

int main(){
  while(~scanf("%d",&n)){
    if(n<0) return 0;
    solve();
  }
  return 0;
}

标签:HDU,int,1187,sum,ret,MAXN,isdigit,Event
来源: https://www.cnblogs.com/ghostcai/p/15098919.html