poj 2184 Cow Exhibition 01背包

来源:互联网 发布:数据可视化软件 ta 编辑:程序博客网 时间:2024/06/06 02:43


Cow Exhibition
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 10878 Accepted: 4305

Description

"Fat and docile, big and dumb, they look so stupid, they aren't much 
fun..." 
- Cows with Guns by Dana Lyons 

The cows want to prove to the public that they are both smart and fun. In order to do this, Bessie has organized an exhibition that will be put on by the cows. She has given each of the N (1 <= N <= 100) cows a thorough interview and determined two values for each cow: the smartness Si (-1000 <= Si <= 1000) of the cow and the funness Fi (-1000 <= Fi <= 1000) of the cow. 

Bessie must choose which cows she wants to bring to her exhibition. She believes that the total smartness TS of the group is the sum of the Si's and, likewise, the total funness TF of the group is the sum of the Fi's. Bessie wants to maximize the sum of TS and TF, but she also wants both of these values to be non-negative (since she must also show that the cows are well-rounded; a negative TS or TF would ruin this). Help Bessie maximize the sum of TS and TF without letting either of these values become negative. 

Input

* Line 1: A single integer N, the number of cows 

* Lines 2..N+1: Two space-separated integers Si and Fi, respectively the smartness and funness for each cow. 

Output

* Line 1: One integer: the optimal sum of TS and TF such that both TS and TF are non-negative. If no subset of the cows has non-negative TS and non- negative TF, print 0. 

Sample Input

5-5 78 -66 -32 1-8 -5

Sample Output

8

Hint

OUTPUT DETAILS: 

Bessie chooses cows 1, 3, and 4, giving values of TS = -5+6+2 = 3 and TF 
= 7-3+1 = 5, so 3+5 = 8. Note that adding cow 2 would improve the value 
of TS+TF to 10, but the new value of TF would be negative, so it is not 
allowed. 

Source

USACO 2003 Fall

题意:

这是又是一道01背包的变体,题目要求选出一些牛,使smartness和funness值的和最大,而这些牛有些smartness或funness的值是负的,还要求最终的smartness之和以及funness之和不能为负。


解法:枚举+背包



01背包,对一头牛的两个值a、b,一个看成花费,一个看成是价值。

这也是数组的运用dp[],一个数组其实就是一个映射。在x->dp[x]中,不仅要注意dp[x]的意义,还要充分利用x的意义。

dp[x]表示a值总和为x时,b值和的最大值。

如果花费大于0(一般的01背包),要逆序遍历。

如果花费小于0,                                       则要升序。

虽然总体积有负数值,我们可以按一定的规则把它存到对应的正值下标x的dp[x]下。





#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<climits>#include<queue>#include<vector>#include<map>#include<sstream>#include<set>#include<stack>#include<cctype>#include<utility>#pragma comment(linker, "/STACK:102400000,102400000")#define PI (4.0*atan(1.0))#define eps 1e-10#define sqr(x) ((x)*(x))#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)#define  lson   ind<<1,le,mid#define rson    ind<<1|1,mid+1,ri#define MID   int mid=(le+ri)>>1#define zero(x)((x>0? x:-x)<1e-15)#define mk    make_pair#define _f     first#define _s     secondusing namespace std;//const int INF=    ;typedef long long ll;//const ll inf =1000000000000000;//1e15;//ifstream fin("input.txt");//ofstream fout("output.txt");//fin.close();//fout.close();//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);const int INF =0x3f3f3f3f;const int maxn=   100+20  ;//const int maxm=    ;int n;int a[maxn],b[maxn];int dp[100020*2];int up,down;int f(int x)  {return x+100000;}/*void print(){    for(int i=down;i<=up;i++)        cout<<i<<" "<<dp[f(i)]<<endl;}*/int main(){  while(~scanf("%d",&n) )  {      down=up=0;      for(int i=1;i<=n;i++)      {          scanf("%d%d",&a[i],&b[i]);          if(a[i]>=0)  up+=a[i];          else down+=a[i];      }//      cout<<down<<" "<<up<<endl;      for(int i=down;i<=up;i++)  dp[f(i)]=-INF;     //  print();      for(int i=1;i<=n;i++)      {//          cout<<endl<<endl<<i<<endl;;          int cost=a[i];          if(cost>=0)          {              for(int v=up;v-cost>=down;v--)              {                  dp[f(v)]=max(dp[f(v)],dp[f(v-cost)]+b[i]);              }          }          else          {              for(int v=down;v-cost<=up;v++)              {                  dp[f(v)]=max(dp[f(v)],dp[f(v-cost)]+b[i]);              }          }           dp[f(cost)]=max(dp[f(cost)],b[i]);         // print();      }      int maxi=0;      for(int i=0;i<=up;i++)      {          if(dp[f(i)]<0) continue;          maxi=max(dp[f(i)]+i,maxi);      }      printf("%d\n",maxi);  }    return 0;}


0 0
原创粉丝点击