2018 东北赛i 简单dpI

Shadowverse is a funny card game. One day you are playing a round of this game. 
You have n cards, each with two attributes wi and xi. If you use card i, you will cost wi points of power and cause xi damage to the enemy.
Among them, there are two special types of cards: some cards are magic cards and some have “spell boost effect”. Everytime you have used a magic card, for each unused “spell boost effect” card i: if the the current cost of i (i.e. wi) is positive, then wi will be reduced by 1. Note that some cards may be both 
magic cards and have spell boost effect. 
Now you have W points of power, you need to calculate maximum total damage you can cause.

Input

Input is given from Standard Input in the following format: 
n W 
w1 x1 is_magic1 is_spell_boost1 
w2 x2 is_magic2 is_spell_boost2 
... 
... 
... 
wn xn is_magicn is_spell_boostn 
Constraints 
1 ≤ n ≤ 500 
0 ≤ W, wi, xi ≤ 500, and all of them are integers. 
is_magici means: If this card is magic card, the value is 1, otherwise the value is 0. 
is_spell_boosti means: If this card has spell boost effect, the value is 1, otherwise 0

Output

One integer representing the maximum total damage.

Sample Input

3 3
3 3 1 1
2 3 1 1
1 3 1 1
4 3
3 3 1 1
3 4 1 0
1 3 0 1
1 0 1 0

Sample Output

9
7

就是一个带顺序的背包 极其简单

还有就是注意一下斜背包的时候的枚举顺序

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
struct node
{
    int w,x;
    int ma,bo;
};
node a[509];
int dp[505][505];//mo  huafei
int cmp (node p,node q)
{
    if(p.ma==q.ma)
    {
        if(p.bo==q.bo)
        {
            return p.w<q.w;
        }
        return p.bo<q.bo;
    }
    return  p.ma>q.ma;
}
int main()
{
    int n,w;
    while (~scanf("%d%d",&n,&w))
    {
        int sum=0;
        memset(dp,-1,sizeof(dp));
        for (int i=1; i<=n; i++)
        {
            scanf("%d%d%d%d",&a[i].w,&a[i].x,&a[i].ma,&a[i].bo);
        }
        sort(a+1,a+1+n,cmp);
//        for(int i=1;i<=n;i++)
//        {
//            cout<<a[i].w<<a[i].x<<" "<<a[i].ma<<endl;
//        }
        int MAX=0;
        dp[0][0]=0;
        for (int i=1; i<=n; i++) //mofaka
        {
            for(int j=w; j>=0; j--)
            {
                for(int k=n; k>=0; k--)
                {

                    if(a[i].ma==1)
                    {
                        int temp=max(a[i].w-k+1,0);
                        if(a[i].bo==0&&j-a[i].w>=0&&k>=1&&(dp[k-1][j-a[i].w]!=-1))
                            dp[k][j]=max(dp[k-1][j-a[i].w]+a[i].x,dp[k][j]);
                        if(a[i].bo==1&&j-temp>=0&&k>=1&&dp[k-1][j-temp]!=-1)
                            dp[k][j]=max(dp[k-1][j-temp]+a[i].x,dp[k][j]);
                    }
                    else
                    {
                        int temp=max(a[i].w-k,0);
                         if(a[i].bo==0&&j-a[i].w>=0&&(dp[k][j-a[i].w]!=-1))
                            dp[k][j]=max(dp[k][j-a[i].w]+a[i].x,dp[k][j]);
                        if(a[i].bo==1&&j>=temp&&dp[k][j-temp]!=-1)
                            dp[k][j]=max(dp[k][j-temp]+a[i].x,dp[k][j]);
                    }
                    //cout<<k<<" "<<j<<" "<<dp[k][j]<<endl;
                    MAX=max(MAX,dp[k][j]);
                }
            }
        }
        printf("%d
",MAX);

    }return 0;
}
原文地址:https://www.cnblogs.com/caowenbo/p/11852277.html