博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
uva 12325 枚举暴力 b
阅读量:6504 次
发布时间:2019-06-24

本文共 2411 字,大约阅读时间需要 8 分钟。

 

Some brave warriors come to a lost village. They are very lucky and find a lot of treasures and a big treasure chest, but with angry zombies.

The warriors are so brave that they decide to defeat the zombies and then bring all the treasures back. A brutal long-drawn-out battle lasts from morning to night and the warriors find the zombies are undead and invincible.

Of course, the treasures should not be left here. Unfortunately, the warriors cannot carry all the treasures by the treasure chest due to the limitation of the capacity of the chest. Indeed, there are only two types of treasures: emerald and sapphire. All of the emeralds are equal in size and value, and with infinite quantities. So are sapphires.

Being the priest of the warriors with the magic artifact: computer, and given the size of the chest, the value and size of each types of gem, you should compute the maximum value of treasures our warriors could bring back.

Input

There are multiple test cases. The number of test cases T (T$ \le$200) is given in the first line of the input file. For each test case, there is only one line containing five integers NS1, V1, S2, V2, denoting the size of the treasure chest is N and the size and value of an emerald isS1 and V1, size and value of a sapphire is S2, V2. All integers are positive and fit in 32-bit signed integers.

Output

For each test case, output a single line containing the case number and the maximum total value of all items that the warriors can carry with the chest.

Sample Input

2 100 1 1 2 2 100 34 34 5 3

Sample Output

Case #1: 100 Case #2: 86
题目大意:

                     知道背包的容量N,有两件物品占用空间分别为s1和s2,物品价值为v1和v2,,数量有无数个。求背包里能装的物品的最大价值是多少。

解题思路:

           这道题和完全背包的问题类似,但是又有差距,利用完全背包的解法来解明显是行不通的。确定s1和s2 的最小公倍数s,那么在装的时候我们把 N/s 个s都装这种物品,N%s部分枚举。有一点值得注意我们在求N%s部分时,需要拿出一个s和N%s加起来做为枚举的对象(想想就明白)

 

 

#include<iostream>

#include<stdio.h>
using namespace std;
long long  gcd(long long x,long long y)
{
   long long xy=x*y;
   long long   r=x%y;
    while(r)
    {
        x=y;
        y=r;
        r=x%y;
    }
    xy/=y;
    return xy;
}
int main()
{
    long long   n,s1,v1,s2,v2,t,s;
    int T;
    scanf("%d",&T);
    int kase=0;
    while(T--)
    {
        scanf("%lld%lld%lld%lld%lld",&n,&s1,&v1,&s2,&v2);
        s=gcd(s1,s2);
        t=n/s;
        n%=s;
        if(t)
        {
            n+=s;
            t--;
        }
        long long  v = t*max(s/s1*v1,s/s2*v2),vv=0;
        if(s1<s2)
        {swap(s1,s2),swap(v1,v2);}
        for(int i=0;i<=n/s1;i++)
            vv=max((n-i*s1)/s2*v2+i*v1,vv);
        v+=vv;
        printf("Case #%d: %lld\n",++kase,v);
    }
    return 0;
}

转载于:https://www.cnblogs.com/552059511wz/p/6657714.html

你可能感兴趣的文章
Spring cloud 安全部署与性能优化
查看>>
querySelector 和 querySelectorAll区别
查看>>
Linux系统_Centos7下安装Nginx
查看>>
《PHP和MySQL Web 开发》 第12章 MySQL高级管理
查看>>
android:supportsRtl="true"
查看>>
数据库设计 Step by Step (6) —— 提取业务规则
查看>>
深入理解java异常处理机制
查看>>
centos安装redis环境
查看>>
Redis客户端redisson实战
查看>>
连接到 JasperReports Server
查看>>
java处理高并发高负载类网站问题
查看>>
使用C#生成随机密码(纯数字或字母)和随机卡号(数字与字母组合)
查看>>
CAS服务器端集群
查看>>
分布式事务学习笔记
查看>>
Android内存泄漏的常见场景及解决方案
查看>>
设计模式 之 访问者模式
查看>>
EasyUI datagrid隐藏<thead>表头
查看>>
用JS获取地址栏参数的方法
查看>>
JAVA Collections框架
查看>>
更改Windwos server 2003 域用户密码策略默认配置
查看>>