zoukankan      html  css  js  c++  java
  • Spell Boost

     Spell Boost

    时间限制: 1 Sec  内存限制: 128 MB

    题目描述

    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 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

    输出

    One integer representing the maximum total damage.

    样例输入

    3 3
    3 3 1 1
    2 3 1 1
    1 3 1 1
    

    样例输出

    9

    题意:有n张卡片,每个卡片有一个价值和花费,而且有A,B两种属性,若使用了A属性的卡片就会使手中B属性的卡片花费减少1,问总花费不超过W时的最大价值。
    做法:若去掉属性就是一个背包问题,因此这里我们再加一维状态来表示已经用了多少张A属性的卡片。即dp[i][j][k]表示用了前i张卡片,花费不超过j,已经用了k张A属性卡片的最大价值。
    三维空间太大,所以要用滚动数组优化。而且要注意循环j和k的时候要从大到小遍历,这是为了保证每次状态都只会从上一张卡片的状态转移过来。(和背包转移的思想类似)。
    #include<bits/stdc++.h>
    #define N 505
    using namespace std;
    int dp[N][N]={0};
    
    struct ss
    {
        int x,w,is_magic,is_boost;
    
        bool operator < (const ss &s) const
        {
            if(is_magic!=s.is_magic)return is_magic>s.is_magic;
            if(is_boost!=s.is_boost)return is_boost<s.is_boost;
            return w<s.w;
        }
    
    };
    ss arr[N];
    
    int main()
    {
        int n,W;
        int x[N],w[N],is_magic[N],is_boost[N];
    
        scanf("%d %d",&n,&W);
        for(int i=1;i<=n;i++)scanf("%d %d %d %d",&arr[i].w,&arr[i].x,&arr[i].is_magic,&arr[i].is_boost);
    
        sort(arr+1,arr+1+n);
    
        for(int i=1;i<=n;i++)
        {
            if(arr[i].is_magic)
            {
                for(int k=i;k>=0;k--)
                for(int j=W;j>=0;j--)
                {
                    if(arr[i].is_boost&&j-max(0,arr[i].w-(k-1))>=0)dp[j][k]=max(dp[j][k], k-1>=0 ? dp[j-max(0,arr[i].w-(k-1))][k-1]+arr[i].x :0);
                    else
                    if(!arr[i].is_boost&&j-arr[i].w>=0)dp[j][k]=max(dp[j][k],k-1>=0 ? dp[j-arr[i].w][k-1]+arr[i].x : 0);
                }
            }
            else
            {
                for(int k=i;k>=0;k--)
                for(int j=W;j>=0;j--)
                {
                    if(arr[i].is_boost&&j-max(0,arr[i].w-(k))>=0)dp[j][k]=max(dp[j][k],dp[j-max(0,arr[i].w-(k))][k]+arr[i].x);
                    else
                    if(!arr[i].is_boost&&j-arr[i].w>=0)dp[j][k]=max(dp[j][k],dp[j-arr[i].w][k]+arr[i].x);
                }
            }
        }
    
        int ans=0;
        for(int i=0;i<=n;i++)ans=max(ans,dp[W][i]);
        printf("%d
    ",ans);
        return 0;
    }
    View Code
     
  • 相关阅读:
    解决CollectionView TableView reloadData或者reloadSections时的刷新的闪烁问题
    HTTP请求头
    Fastlane 使用笔记
    python-函数式编程
    python-高级特性
    python基础使用
    python基础-函数02
    python基础-函数01
    python基础
    Linux基础
  • 原文地址:https://www.cnblogs.com/tian-luo/p/9595707.html
Copyright © 2011-2022 走看看