Poj 2484 cow exhibition [deformation 0-1 backpack]

Source: Internet
Author: User

Question: poj 2484 cow Exhibition


Question: Give the nheaded ox. Each ox has a lucky Si and a smart ti. Now we need to select some cows to maximize the sum of the two values, provided that sum (SI) and sum (Ti) are both non-negative values.


Analysis: The data volume of this question is small, and it can be searched and trimmed.

Here we will talk about the idea of a 0-1 backpack. The obvious deformation of this question is that the item has two attribute values, and you must select the largest one.

So we can fix one value to find the maximum value of another. According to the idea of the 0-1 backpack, the state is defined: DP [I], which indicates loading some items to make sum (SI) the maximum sum (Ti) value.

Then the state transition equation is DP [I + Si] = max (DP [I + Si], DP [I] + Ti), which exactly represents the two choices of an item.

AC code:

#include <cstdio>#include <cstring>#include <vector>using namespace std;const int N = 210000;const int pos = 100000;int dp[N];int main(){    int n;    while(~scanf("%d",&n))    {        memset(dp,-0x3f3f3f3f,sizeof(dp));        dp[pos] = 0;        int ma = pos,mi = pos;        for(int i=0;i<n;i++)        {            int s,t;            scanf("%d%d",&s,&t);            if(s>0){                for(int f=ma ;f>=mi;f--)                    dp[f+s] = max(dp[f+s],dp[f]+t);                ma+=s;            }            else{                for(int f = mi;f<=ma;f++)                    dp[f+s] = max(dp[f+s],dp[f]+t);                mi+=s;            }        }        int ans = 0;        for(int i=pos;i<=ma;i++)            if(dp[i]>=0)                ans = max(ans,i-pos+dp[i]);        printf("%d\n",ans);    }    return 0;}

Search timeout code:

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int N = 210000;const int pos = 100000;struct Node{    int s,t;};vector<Node> v;int cmp(Node a,Node b){    if(a.s!=b.s)        return a.s>b.s;    if(a.t!=b.t)        return a.t>b.t;}int ans;void dfs(int i,int sums,int sumt){    if(i==v.size())        return ;    if(sums>=0 && sumt>=0)        ans = max(ans,sums+sumt);    if(sums<0 && v[i].s<=0)        return ;    dfs(i+1,sums,sumt);    dfs(i+1,sums+v[i+1].s,sumt+v[i+1].t);}int main(){    int n;    int sums = 0,sumt = 0;    while(~scanf("%d",&n))    {        for(int i=0;i<n;i++)        {            int s,t;            scanf("%d%d",&s,&t);            if(s>=0 && t>=0)            {                sums+=s,sumt+=t;            }            else if(s<=0 && t<=0)                continue;            else                v.push_back((Node){s,t});        }        sort(v.begin(),v.end(),cmp);        ans = 0;        dfs(0,0,0);        dfs(0,v[0].s,v[0].t);        printf("%d\n",ans+sums+sumt);    }    return 0;}


Poj 2484 cow exhibition [deformation 0-1 backpack]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.