自学内容网 自学内容网

【宠物小精灵之收服(待更新)】

题目

 


代码

#include <bits/stdc++.h>
using namespace std;
int f[1010][510];
int main()
{
    int n, m, k;
    cin >> n >> m >> k;

    int c = 0;
    for(int i = 1; i <= k; i++)
    {
        int cost, hp;
        cin >> cost >> hp;
        for(int j = n; j >= cost; j--)
        {
            for(int t = m; t > hp; t--)
            {
                f[j][t] = max(f[j][t], f[j-cost][t-hp] + 1);
                c = max(c, f[j][t]);
            }
        }

    }

    int r;
    if(c == 0) r = 0;
    else
    {
        r = 510;
        for(int j = 1; j <= m; j++)
        {
            if(f[n][j] == c)
            {
                r = j;
                break;
            }
        }
        r--;
    }

    cout << c << ' ' << m-r;
    return 0;
}

注意

  1. hp不能为0,因此不能从 t = 0 的状态转移
  2. 因为hp有1不是因为收服小精灵而记录的,因此要 r-1

原文地址:https://blog.csdn.net/m0_73669127/article/details/142299784

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!