Alien Crop Triangles(动态规划)

What a day! From reports received by the police department, a strange, triangular shaped object was seen flying and beaming strong light across the sky of Nlogonia for a few minutes this morning. Was it Triangolonian aliens? A secret government project? Someone playing a prank? Either way, you noticed your property now has several triangular patches that are missing its grass... time to buy some grass seeds to fix those.

Grass seeds come in bags of a certain kilogram weight. Each kg of seeds can be used to cover 30m2 of grass. A local supplier has given you a quote for all B seed bags it sells. Each bag has a weight of Wi kg and costs Pi Nlogonian coins.

You went ahead and measured all the N triangles missing its grass that you need to cover with seeds. As they have different shapes, you decided to measure their sides (Ai, Bi, Ci) in meters.

Given your local supplier quote, and the measurements of each triangle. Can you find the minimum number of coins you need to buy enough seeds to fix up your property?

 这个题本质上是一个完全背包问题,解决这一问题的关键是解决同一物品多次挑选更新答案的问题,所以,此题在一维状态下遍历平方米数的时候需要从小到大遍历。但注意,题目给出一个限制,一公斤最少就能覆盖30平方米的草坪,所以在当剩余面积数小于每一袋可以覆盖的面积时,我们可以将其更新在dp[0]的数组上,当然dp[0]需要提前赋值为零,因为要求最小金币数,我们需要在开始前将数组全部更新为最大。自此我们可以推出dp[ j ]=min(dp[j],dp[max(j-g[i]*30),0]+w[i] );

代码如下:

#include<iostream>
#include<cmath>
using namespace std;
int g[110],w[110];
const int N = 115100;
int dp[N];
int main()
{
    int b,n;cin>>b>>n;
    for(int i=1;i<=b;i++)
    {
        cin>>g[i]>>w[i];
    }
    dp[0]=0;
    for(int i=1;i<=N;i++)dp[i]=0x3f3f3f3f;
    double sum=0;
    for(int i=1;i<=n;i++)
    {
        int a,b,c;cin>>a>>b>>c;
        double p=1.0*(a+b+c)/2;
        double sq=sqrt(p*(p-a)*(p-b)*(p-c));
        sum+=sq;
    }
    for(int i=1;i<=b;i++)
    {
        for(int j=0;j<=(int)ceil(sum);j++)
        {
            dp[j]=min(dp[j],dp[max(j-g[i]*30,0)]+w[i]);     
        }
    }
    if(dp[(int)ceil(sum)]==0x3f3f3f3f)cout<<-1;
    else cout<<dp[(int)ceil(sum)];
     
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值