HDU 5038 Grade

本文解析了HDU 5038题目的解题思路,介绍了如何根据蘑菇的重量计算其评分,并找出评分中的众数。讨论了三种不同情况的处理方法,并提供了完整的C++代码实现。

题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5038

题目:

Grade

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3292    Accepted Submission(s): 1193

Problem Description
Ted is a employee of Always Cook Mushroom (ACM). His boss Matt gives him a pack of mushrooms and ask him to grade each mushroom according to its weight. Suppose the weight of a mushroom is w, then it’s grade s is 

s = 10000 - (100 - w)^2

What’s more, Ted also has to report the mode of the grade of these mushrooms. The mode is the value that appears most often. Mode may not be unique. If not all the value are the same but the frequencies of them are the same, there is no mode. 
 
Input
 
The first line of the input contains an integer T, denoting the number of testcases. Then T test cases follow.

The first line of each test cases contains one integers N (1<=N<=10^6),denoting the number of the mushroom.

The second line contains N integers, denoting the weight of each mushroom. The weight is greater than 0, and less than 200.
 
Output
 
For each test case, output 2 lines.

The first line contains "Case #x:", where x is the case number (starting from 1) 

The second line contains the mode of the grade of the given mushrooms. If there exists multiple modes, output them in ascending order. If there exists no mode, output “Bad Mushroom”.
 
Sample Input
 
3
6
100 100 100 99 98 101
6
100 100 100 99 99 101
6
100 100 98 99 99 97
 
Sample Output
 
Case #1:
10000
Case #2:
Bad Mushroom
Case #3:
9999 10000
 
题意:
1.只有一种最高频的grade,直接输出
2.所有grade的出现的频率都相同,输出Bad Mushroom
3.有多个最高频grade,除去这些grade还有一些低频grade
 
代码:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <vector>
 4 #include <algorithm>
 5 using namespace std;
 6 int n;
 7 int visited[205];
 8 struct node{
 9     int v;
10     int t;
11 }num[205];
12 int getValue(int i){
13     return 10000-(100-i)*(100-i);
14 }
15 bool cmp(node x,node y){
16     return x.t>y.t;
17 }
18 vector<int>v;
19 int main(){
20     int t,a,b;
21     scanf("%d",&t);
22     for (int i=1; i<=t; i++) {
23         int flag=0;
24         for (int j=0; j<=200; j++){
25             num[j].t=0;
26             num[j].v=j;
27         }
28         v.clear();
29         memset(visited, 0, sizeof(visited));
30         printf("Case #%d:\n",i);
31         scanf("%d",&n);
32         for (int j=0; j<n; j++) {
33             scanf("%d",&a);
34             visited[a]++;//统计重量为a的蘑菇出现了几次
35         }
36         for (int j=0; j<100; j++)   num[j].t=visited[j]+visited[200-j];//重量为j的grade和重量为200-j的grade是一样的,就合并计算
37         num[100].t=visited[100];
38         sort(num,num+105,cmp);
39         int len=1,l;
40         for (l=1; l<=101 && num[l].t!=0; l++);//找总共有几个grade
41         for (len=1; len<=101 && num[len].t==num[len-1].t; len++);//找最高频grade的数量
42         if(len==1)  printf("%d\n",getValue(num[0].v));//情况1,只有一个最高配grade
43         else if(len==l) printf("Bad Mushroom\n");//情况2,最高配grade数量等于总数
44         else{//情况3,算出grade 并排序升序输出
45             for (int j=0; j<len; j++) {
46                 int x=getValue(num[j].v);
47                 v.push_back(x);
48             }
49             sort(v.begin(), v.end());
50             printf("%d",v[0]);
51             for (int j=1; j<v.size(); j++) {
52                 printf(" %d",v[j]);
53             }
54             printf("\n");
55         }
56     }
57     return 0;
58 }

 

 

转载于:https://www.cnblogs.com/uniles/p/7171187.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、付费专栏及课程。

余额充值