The secret code

The secret code

Input file: stdinOutput file: stTime limit: 1 sec

Memory limit: 256 Mb
After returning from the trip, Alex was unpleasantly surprised: his porch door had a new combination lock. Alex
can not get into his house! Code lock contains N disks, each of which can be in one of M positions. There
is only one correct position. Alex thoroughly inspected discs and by fingerprints and scratches determined the
probability of each position for each disk. Now Alex has K attempts to guess the correct code: if he fails, his
vigilant neighbors will call the police, and Alex will have a hard time persuading cops that he is not a thief, but
tried to get home. Help Alex to calculate the maximum probability of getting home, not to the police.
Limit
1 ≤ N ≤ 100
1 ≤ M ≤ 20
1 ≤ K ≤ 100
0 ≤ P ij ≤ 100
Input
The first line of input file contains three integers: N, M Рё K.
Next N lines contain M integers each: j-th number of the i-th line (P ij ) — the probability of a situation where
i-th disc’s correct position is j. Given that
P M
j=1 P ij
= 100.
Output
Print a single number — Alex’s chances to guess the correct code in time. Output result should have an absolute
percentage error not grater than 10 −7 .
Example
stdin

2 2 1
50 50
10 90
3 5 4
10 15 20 25 30
1 2 3 4 90
100 0 0 0 0

stdout

0.45
0.81

 

题目大意:

   n个数列,每个数列取一个数,得到这些数的乘积,求前k个最大的乘积。

 

多谢syx的指导!

解题思路:

  对每个数列排序后。

  首先,第一大的自然是所有数列最大值乘积。

  接着,将最大乘积加到ans上,然后将这个乘积能够达到的所有下一个状态均加入优先队列中。至于保存状态,每个状态只需保存其在每个数列的取到第几个数的指针即可,显然下一个状态是在该状态之后,并且出现在某一个指针向后移位。故将所有下一个状态加入优先队列。

  然后,当找到第k个或者是空队列时退出循环,否则返回上一步。

  最后,输出答案。

 

实现方法,直接使用STL中的priority_queue,若数据量加大,可惜优先队列不能删除尾节点,不过可利用最大最小堆保存前k个即可。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <deque>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))

using namespace std;

struct State{
    int head[105];
    double x;
    bool operator<(const State &b) const
    {
        return x<b.x;
    }
};

double a[105][21];
int n,m,k;
priority_queue<State> q;
bool cmp(double x, double y)
{
    return x>y;
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            scanf("%lf",&a[i][j]);
            a[i][j]/=100;
        }
        sort(a[i],a[i]+m,cmp);
    }

    State tmp;
    tmp.x=1.0;
    for(int i=0; i<n; i++)
    {
        tmp.head[i]=0;
        tmp.x*=a[i][0];
    }
    q.push(tmp);

    double ans=0.0;
    for(int i=0; i<k&&!q.empty(); i++)
    {
        tmp=q.top();
        q.pop();
        ans+=tmp.x;
        for(int j=0; j<n; j++)
            if(tmp.head[j]<m-1) 
            {
                if(a[j][tmp.head[j]+1]==0) continue;
                tmp.x=tmp.x / a[j][tmp.head[j]] * a[j][tmp.head[j]+1];
                tmp.head[j]++;
                q.push(tmp);
                tmp.x=tmp.x / a[j][tmp.head[j]] * a[j][tmp.head[j]-1];
                tmp.head[j]--;
            }
    }

    printf("%.8f\n",ans);
    return 0;
}
View Code

 

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

余额充值