NYOJ747 蚂蚁的难题(三)(贪心+DP)

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=747

每种食材只有两种操作,即取与不取, 又有在T时间内, 所以很容易想到0-1背包, 但是我们发现即使选的食材一样, 但选的顺序不一样获得美味指数也不样, 所以要先贪心求一下选的先后顺序, 然后在DP, 就AC了。

贪心的公式就是主要是一个算式:
a1-b1c1+a2-(c1+c2)b2 > a2-b2c2+a1-(c1+c2)b1
化简
b2c1<b1c2

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int M = 100000 + 10;
const int N = 50 + 5;
#define LL long long
struct Info
{
    int a, b, c;
} sa[N];
int cmp(Info x, Info y)
{
    return x.c*y.b < y.c*x.b;
}
LL dp[M];
int main()
{
    int T, n;
    while(~scanf("%d%d", &T, &n))
    {
        for(int i = 1; i <= n; i++)
            scanf("%d%d%d", &sa[i].a, &sa[i].b, &sa[i].c);
        sort(sa+1, sa+n+1, cmp);
        memset(dp, -0x3f, sizeof(dp));
        dp[0] = 0;
        for(int i = 1; i <= n; i++)
            for(int j = T; j-sa[i].c >= 0; j--)
                dp[j] = max(dp[j], dp[j-sa[i].c]+sa[i].a-j*sa[i].b);
        LL Max = 0;
        for(int i = 1; i <= T; i++)
            Max = max(Max, dp[i]);
        printf("%lld\n", Max);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值