hlg1551Assemble--暴力求解

本文介绍了一道编程题目,目标是在预算范围内通过选择合适的电脑配件来最大化整机性能。文章提供了详细的解析过程及代码实现。
Assemble
Time Limit: 2000 MSMemory Limit: 65535 K
Total Submit: 17(10 users)Total Accepted: 11(10 users)Rating: Special Judge: No
Description
    Recently your team noticed that the computer you use to practice for programming contests
is not good enough anymore. Therefore, you decide to buy a new computer.
    To make the ideal computer for your needs, you decide to buy separate components and
assemble the computer yourself. You need to buy exactly one of each type of component.
    The problem is which components to buy. As you all know, the quality of a computer is
equal to the quality of its weakest component. Therefore, you want to maximize the quality
of the component with the lowest quality, while not exceeding your budget.
Input

On the first line one positive number: the number of testcases, at most 100. After that per
testcase:
• One line with two integers:1≤n≤1 000, the number of available components and1≤b≤1 000 000 000, your budget.
• n lines in the following format: “type name price quality”, wheretypeis a string with the type of the component,nameis a string with the unique name of the com-ponent,priceis an integer (0≤price≤1 000 000) which represents the price of the
component andqualityis an integer (0≤quality≤1 000 000 000) which represents
the quality of the component (higher is better). The strings contain only letters, digits

and underscores and have a maximal length of20characters.
It will always possible to construct a computer with your budget.
Output

Per testcase:
• One line with one integer: the maximal possible quality.

 

Sample Input
1
18 800
processor 3500_MHz 66 5
processor 4200_MHz 103 7
processor 5000_MHz 156 9
processor 6000_MHz 219 12
memory 1_GB 35 3
memory 2_GB 88 6
memory 4_GB 170 12
mainbord all_onboard 52 10
harddisk 250_GB 54 10
harddisk 500_FB 99 12
casing midi 36 10
monitor 17_inch 157 5
monitor 19_inch 175 7
monitor 20_inch 210 9
monitor 22_inch 293 12
mouse cordless_optical 18 12
mouse microsoft 30 9
keyboard office 4 10
Sample Output
9
Source
NWERC2007

大意:给你一部分电脑零件

每种零件有一定的价格和性能值

每种零件需要一个

电脑的整体性能为所选的所有零件中性能最差的

给你一笔预算和一些零件问你不超过预算的求情况下电脑的最大性能

 

分析:

这是黄老师家上的一道题

比赛的时候硬是半天没有做出来

其实直接暴力就可以了

枚举每种可能出现的答案  然后判断预算是否超支   对于每个枚举的答案  同一类型如果没有一件物品的性能值大于等于该值说明不符合条件

对于多件符合的物品   选择价格最便宜的即可

代码: 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <map>
 6 using namespace std;
 7 
 8 const int maxn = 1005;
 9 
10 struct point
11 {
12     string s;
13     int a, b;
14 }poin[maxn];
15 
16 int n, sum_b;
17 bool check(int x)
18 {
19     int sum = 0;
20     string s1 = poin[0].s;
21     int st = 0, en = 0;//每一类的最开始的下标和结束的下标
22     for(int i = 0; i < n; i++)//需要注意的是最后一类的判断
23     {
24         if(poin[i].s != s1)//不属于上一类
25         {
26             en = i - 1;
27             bool flag = false;
28             int max_num = 0xffffff;
29             for(int i = st; i <= en; i++)//判断
30             {
31                 if(poin[i].b >= x)
32                 {
33                     flag = true;
34                     if(max_num > poin[i].a)//在该类寻找最便宜的即可
35                     {
36                         max_num = poin[i].a;
37                     }
38                 }
39             }
40             if(!flag)//该类中没有一件物品的品质大于等于x
41                 return false;
42             sum += max_num;
43             st = i;
44             s1 = poin[i].s;
45         }
46     }
47     //printf("*%d\n",sum);
48     if(sum <= sum_b)
49         return true;
50     else
51         return false;
52 }
53 
54 int main()
55 {
56     int t;
57     //freopen("1511.txt","r",stdin);
58     scanf("%d",&t);
59     while(t--){
60         scanf("%d %d",&n, &sum_b);
61         string str;
62         int num[maxn] = { 0 };//num数组用于存储需要枚举的值
63         int k = 0;
64         for(int i = 0; i < n; i++)
65         {
66             cin>>poin[i].s>>str>>poin[i].a>>poin[i].b;
67             num[k++] = poin[i].b;//将需要枚举的值加入数组中
68         }
69         poin[n].s = "asdf";//这一步非常重要  wrong一次
70         n++;
71         sort(num, num + k);
72         num[k] = 0;
73         int ans = 0;
74         for(int i = k - 1; i >= 0; i--)//从大到小
75         {
76              if(num[i] == num[i+1])
77                 continue;//枚举过的话就直接忽略
78              if(check(num[i]))//check 用来检测预算是否超支
79              {
80                  ans = num[i];
81                  break;//如果满足条件的话直接跳出,因为要求的就是求得最大值
82              }
83             // printf("%d %d\n",num[i],check(num[i]));
84         }
85         printf("%d\n",ans);
86     }
87     return 0;
88 }
View Code

 

转载于:https://www.cnblogs.com/zhanzhao/p/3716855.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值