scut training 2 简单dp zoj3956

这道题居然是dp

虽然最后听到别人说是dp,,但是自己还是不会,,,而且最后跟着敲得时候居然要longlong。

这道题属于01背包,, dp[i]表示c为i时最多的h,

然后根据公式:   

因为当c固定时,这个式子只需要看前面 h*h-h*c=h*(h-c)

h大于c时,肯定h越大越好

h小于c时,表达式的值必定小于零,肯定是不考虑的



这个地方的通过使 c固定的方式来处理这个公式方式应该是这种题的套路吧。。做题太少了。。。不知道。。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int INF=10000000;
const int maxn=50050;
ll h[maxn],c[maxn];
ll dp[maxn];
ll cal(ll c,ll h){
    return h*h-h*c-c*c;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        memset(c,0,sizeof(c));
        memset(h,0,sizeof(h));
        scanf("%d",&n);
        int tot=0;
        for(int i=1;i<=n;++i)
            scanf("%d%d",&h[i],&c[i]),tot+=c[i];
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;++i){
            for(int j=tot;j>=c[i];--j){
                dp[j]=max(dp[j],dp[j-c[i]]+h[i]);
            }
        }
        ll ans=0;
        for(int i=1;i<=tot;++i){
            ans=max(ans,cal(i,dp[i]));
        }
        cout<<ans<<'\n';
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值